def ready_to_graduate(year, units):
return (year == 'senior') and (units >= 120)
ready_to_graduate('junior', 121)
ready_to_graduate('senior', 121)
def ready_greeting(year, units):
is_ready = ready_to_graduate(year, units)
if is_ready:
print('ready to graduate!')
else:
print('not ready yet.')
ready_greeting('junior', 121)
ready_greeting('senior', 121)
The most basic if-statements only include an if.
def fancy_print(n):
if n == 23:
print('I love this number!')
print(n)
fancy_print(5)
fancy_print(23)
def fancier_print(n):
if n == 23:
print('I love this number!')
else:
print('This is not my favorite number.')
print(n)
fancier_print(5)
fancier_print(23)
You don't always need to use else
.
def safe_divide(a, b):
if b == 0:
return 0
return a / b
safe_divide(5, 0)
safe_divide(5, 2)
The above is equivalent to this; I personally prefer the above.
def safe_divide(a, b):
if b == 0:
return 0
else:
return a / b
def a_max(a, b, c):
...
Be concise!
def is_23(n):
if n == 23:
return True
else:
return False
def is_23(n):
return n == 23
Let's build a grades calculator, according to this table:
Letter | Range |
---|---|
A | [90, 100] |
B | [80, 90) |
C | [70, 80) |
D | [60, 70) |
F | [0, 60) |
Note, $[a, b)$ refers to the set of numbers that are greater than or equal to $a$, but less than $b$.
# if grade is between 90 and 100: return 'A'
# if grade is between 80 and 90: return 'B'
# if grade is between 70 and 80: return 'C'
# ...
# Takes in grade as number, computes letter grade, and prints 'Grade: letter'
def grade_converter(grade):
if grade >= 90:
return 'A'
elif grade >= 80:
return 'B'
elif grade >= 70:
return 'C'
elif grade >= 70:
return 'D'
else:
return 'F'
grade_converter(89)
grade_converter(70)
Here it's worth illustrating the difference between using elif
and many if
s.
def grade_converter_print(grade):
if grade >= 90:
print('A')
if grade >= 80:
print('B')
if grade >= 70:
print('C')
if grade >= 70:
print('D')
else:
print('F')
grade_converter_print(89)
grade_converter_print(70)
The fact that we're using return
and elif
changes the behavior of our code dramatically. Watch out for this!
def num_pos(a, b):
...
def indent_example(x):
if x > 5:
print('somewhat big!')
if x % 2 == 0:
print('and even too!')
else:
print('tiny.')
indent_example(15)
indent_example(14)
indent_example(4)
bool(15) # True
bool(None) # False
def weird(x):
if x:
print('x is not 0')
else:
print('x is 0')
weird(5)
weird(0)
def is_empty(s):
return not s
is_empty('')
is_empty('zebra')
You should ignore all of the following code, except for the cell containing the definition of the function state_color
.
import folium
from datascience import *
data = Table.read_table('data/states_elections.csv')
state_capitals = Table.read_table('data/us-state-capitals.csv')
data = data.join('State', state_capitals, 'name').select(['State', 'description', 'latitude', 'longitude', '2020']) \
.relabeled(['State', 'description', '2020'], ['state', 'capital', 'federal vote'])
data
def make_label(r):
return r[1] + ', ' + r[0]
data = data.with_columns('labels', data.apply(make_label))
Marker.map_table(data.select('latitude', 'longitude', 'labels'))
Now, don't ignore this cell!
def state_color(state, vote):
if state == 'California':
return 'green'
elif vote == 'D':
return 'blue'
else:
return 'red'
data = data.with_columns('colors', ['']*data.num_rows)
data = data.with_columns('colors', data.apply(lambda r: state_color(r[0], r[-3])))
Marker.map_table(data.select('latitude', 'longitude', 'labels', 'colors'))