def ready_to_graduate(year, units):
return (year == 'senior') and (units >= 120)
ready_to_graduate('junior', 121)
False
ready_to_graduate('senior', 121)
True
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)
not ready yet.
ready_greeting('senior', 121)
ready to graduate!
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)
5
fancy_print(23)
I love this number! 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)
This is not my favorite number. 5
fancier_print(23)
I love this number! 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)
0
safe_divide(5, 2)
2.5
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):
if a > 0 and a > b and a > c:
return 1
else:
return -1
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)
'B'
grade_converter(70)
'C'
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)
B C D
grade_converter_print(70)
C D
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):
if a > 0 and b > 0:
print(2)
elif a > 0 or b > 0:
print(1)
else:
print(0)
It's worth mentioning that if we were returning instead of printing here, we could have used if
both times instead of if
and elif
.
bool(15) # True
True
bool(None) # False
False
def weird(x):
if x:
print('x is not 0')
else:
print('x is 0')
weird(5)
x is not 0
weird(0)
x is 0
def is_empty(s):
return not s
is_empty('')
True
is_empty('zebra')
False
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
state | capital | latitude | longitude | federal vote |
---|---|---|---|---|
Alabama | Montgomery | 32.3777 | -86.3006 | R |
Alaska | Juneau | 58.3016 | -134.42 | R |
Arizona | Phoenix | 33.4481 | -112.097 | D |
Arkansas | Little Rock | 34.7466 | -92.289 | R |
California | Sacramento | 38.5767 | -121.494 | D |
Colorado | Denver | 39.7392 | -104.985 | D |
Connecticut | Hartford | 41.764 | -72.6822 | D |
Delaware | Dover | 39.1573 | -75.5197 | D |
Florida | Tallahassee | 30.4381 | -84.2813 | R |
Georgia | Atlanta | 33.749 | -84.3882 | D |
... (40 rows omitted)
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'))