Skip to content

Commit cd39356

Browse files
authored
Update I Best Practices.py
1 parent 5aae9a8 commit cd39356

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Writing Functions in Python/I Best Practices.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,62 @@ def build_tooltip(function):
117117
/ Use functions to avoid repetition
118118
- Refactor: improving code by changing it a little bit at a time
119119
**************************************************************************************************************************************************************"""
120+
## Extract a function
121+
122+
def standardize(column):
123+
"""Standardize the values in a column.
124+
125+
Args:
126+
column (pandas Series): The data to standardize.
127+
128+
Returns:
129+
pandas Series: the values as z-scores
130+
"""
131+
# Finish the function so that it returns the z-scores
132+
z_score = (column - column.mean()) / column.std()
133+
return z_score
134+
135+
# Use the standardize() function to calculate the z-scores
136+
df['y1_z'] = standardize(df.y1_gpa)
137+
df['y2_z'] = standardize(df.y2_gpa)
138+
df['y3_z'] = standardize(df.y3_gpa)
139+
df['y4_z'] = standardize(df.y4_gpa)
140+
#`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
141+
## Split up a function
142+
143+
def mean(values):
144+
"""Get the mean of a sorted list of values
145+
146+
Args:
147+
values (iterable of float): A list of numbers
148+
149+
Returns:
150+
float
151+
"""
152+
# Write the mean() function
153+
mean = sum(values) / len(values)
154+
return mean
155+
156+
## Split up a function 2
157+
158+
def median(values):
159+
"""Get the median of a sorted list of values
160+
161+
Args:
162+
values (iterable of float): A list of numbers
163+
164+
Returns:
165+
float
166+
"""
167+
# Write the median() function
168+
midpoint = int(len(values) / 2)
169+
if len(values) % 2 == 0:
170+
median = (values[midpoint - 1] + values[midpoint]) / 2
171+
else:
172+
median = values[midpoint]
173+
return median
174+
"""**************************************************************************************************************************************************************
175+
Pass by assignment
176+
===================
177+
178+
**************************************************************************************************************************************************************"""

0 commit comments

Comments
 (0)