# Set up packages for lecture. Don't worry about understanding this code, but
# make sure to run it if you're following along.
import numpy as np
import babypandas as bpd
import pandas as pd
from matplotlib_inline.backend_inline import set_matplotlib_formats
import matplotlib.pyplot as plt
%reload_ext pandas_tutor
%set_pandas_tutor_options {'projectorMode': True}
set_matplotlib_formats("svg")
plt.style.use('fivethirtyeight')
np.set_printoptions(threshold=20, precision=2, suppress=True)
pd.set_option("display.max_rows", 7)
pd.set_option("display.max_columns", 8)
pd.set_option("display.precision", 2)
if
-statements).for
-loops).Note:
bool
is a data type in Python, just like int
, float
, and str
. True
or False
.capstone = 'finished'
units = 123
units >= 180
False
type(units >= 180)
bool
not
¶There are three operators that allow us to perform arithmetic with Booleans – not
, and
, and or
.
not
flips True
↔️ False
.
capstone
'finished'
capstone == 'finished'
True
not capstone == 'finished'
False
and
operator¶The and
operator is placed between two bool
s. It is True
if both are True
; otherwise, it's False
.
capstone
'finished'
units
123
capstone == 'finished' and units >= 180
False
capstone == 'finished' and units >= 120
True
or
operator¶The or
operator is placed between two bool
s. It is True
if at least one is True
; otherwise, it's False
.
capstone
'finished'
units
123
capstone == 'finished' or units >= 180
True
# Both are True!
capstone == 'finished' or units >= 0
True
# Both are False!
capstone == 'not started' or units >= 180
False
capstone
'finished'
units
123
capstone == 'finished' or (capstone == 'in progress' and units >= 180)
True
# Different meaning!
(capstone == 'finished' or capstone == 'in progress') and units >= 180
False
# "and" has precedence.
capstone == 'finished' or capstone == 'in progress' and units >= 180
True
For instance, not (a and b)
is different than not a and not b
! If you're curious, read more about De Morgan's Laws.
capstone
'finished'
units
123
not (capstone == 'finished' and units >= 180)
True
(not capstone == 'finished') and (not units >= 180)
False
&
and |
vs. and
and or
¶&
and |
operators between two Series. Arithmetic will be done element-wise (separately for each row).df[(df.get('capstone') == 'finished') & (df.get('units') >= 180)]
.and
and or
operators between two individual Booleans.capstone == 'finished' and units >= 180
.Suppose we define a = True
and b = True
. What does the following expression evaluate to?
not (((not a) and b) or ((not b) or a))
A. True
B. False
C. Could be either one
in
operator¶Sometimes, we'll want to check if a particular element is in a list/array, or a particular substring is in a string. The in
operator can do this for us:
3 in [1, 2, 3]
True
'hey' in 'hey my name is'
True
'dog' in 'hey my name is'
False
if
-statements¶True
.if <condition>:
<body>
capstone = 'finished'
capstone
'finished'
if capstone == 'finished':
print('Looks like you are ready to graduate!')
Looks like you are ready to graduate!
else
¶else
: Do something else if the specified condition is False
.
capstone = 'finished'
capstone
'finished'
if capstone == 'finished':
print('Looks like you are ready to graduate!')
else:
print('Before you graduate, you need to finish your capstone project.')
Looks like you are ready to graduate!
elif
¶elif
.elif
: if the specified condition is False
, check the next condition.False
, check the next condition, and so on, until we see a True
condition.True
condition, it evaluates the indented code and stops.True
, the else
body is run.capstone = 'in progress'
units = 123
if capstone == 'finished' and units >= 180:
print('Looks like you are ready to graduate!')
elif capstone != 'finished' and units < 180:
print('Before you graduate, you need to finish your capstone project and take', 180 - units, 'more units.')
elif units >= 180:
print('Before you graduate, you need to finish your capstone project.')
else:
print('Before you graduate, you need to take', 180 - units, 'more units.')
Before you graduate, you need to finish your capstone project and take 57 more units.
What if we use if
instead of elif
?
if capstone == 'finished' and units >= 180:
print('Looks like you are ready to graduate!')
if capstone != 'finished' and units < 180:
print('Before you graduate, you need to finish your capstone project and take', 180 - units, 'more units.')
if units >= 180:
print('Before you graduate, you need to finish your capstone project.')
else:
print('Before you graduate, you need to take', 180 - units, 'more units.')
Before you graduate, you need to finish your capstone project and take 57 more units. Before you graduate, you need to take 57 more units.
Below, complete the implementation of the function, grade_converter
, which takes in a percentage grade (grade
) and returns the corresponding letter grade, according to this table:
Letter | Range |
---|---|
A | [90, 100] |
B | [80, 90) |
C | [70, 80) |
D | [60, 70) |
F | [0, 60) |
Your function should work on these examples:
>>> grade_converter(84)
'B'
>>> grade_converter(60)
'D'
def grade_converter(grade):
if grade >= 90:
return 'A'
elif grade >= 80:
return 'B'
elif grade >= 70:
return 'C'
elif grade >= 60:
return 'D'
else:
return 'F'
grade_converter(84)
'B'
grade_converter(60)
'D'
def mystery(a, b):
if (a + b > 4) and (b > 0):
return 'bear'
elif (a * b >= 4) or (b < 0):
return 'triton'
else:
return 'bruin'
Without running code:
mystery(2, 2)
return?mystery
will produce 'bruin'
.def mystery(a, b):
if (a + b > 4) and (b > 0):
return 'bear'
elif (a * b >= 4) or (b < 0):
return 'triton'
else:
return 'bruin'
for
-loops¶import time
print('Launching in...')
for x in [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]:
print('t-minus', x)
time.sleep(0.5) # Pauses for half a second
print('Blast off! 🚀')
Launching in... t-minus 10 t-minus 9 t-minus 8 t-minus 7 t-minus 6 t-minus 5 t-minus 4 t-minus 3 t-minus 2 t-minus 1 Blast off! 🚀
for
-loops¶for
-loop is one of them.for
-loop is as follows:for <element> in <sequence>:
<for body>
if
-statements, indentation matters!num = 4
print(num, 'squared is', num ** 2)
num = 2
print(num, 'squared is', num ** 2)
num = 1
print(num, 'squared is', num ** 2)
num = 3
print(num, 'squared is', num ** 2)
4 squared is 16 2 squared is 4 1 squared is 1 3 squared is 9
# The loop variable can be anything!
list_of_numbers = [4, 2, 1, 3]
for num in list_of_numbers:
print(num, 'squared is', num ** 2)
4 squared is 16 2 squared is 4 1 squared is 1 3 squared is 9
The line print(num, 'squared is', num ** 2)
is run four times:
num
is 4.num
is 2.num
is 1.num
is 3.This happens, even though there is no num =
anywhere.
Using the array colleges
, write a for
-loop that prints:
Revelle College
John Muir College
Thurgood Marshall College
Earl Warren College
Eleanor Roosevelt College
Sixth College
Seventh College
for college in colleges: print(college + ' College')
colleges = np.array(['Revelle', 'John Muir', 'Thurgood Marshall',
'Earl Warren', 'Eleanor Roosevelt', 'Sixth', 'Seventh'])
...
Ellipsis
for
-loop that accesses each element in an array by using its position.np.arange
will come in handy.actions = np.array(['ate', 'slept', 'exercised'])
feelings = np.array(['content 🙂', 'energized 😃', 'exhausted 😩'])
len(actions)
3
for i in np.arange(len(actions)):
print(i)
0 1 2
for i in np.arange(len(actions)):
print('I', actions[i], 'and I felt', feelings[i])
I ate and I felt content 🙂 I slept and I felt energized 😃 I exercised and I felt exhausted 😩
We don't have to use the loop variable!
for i in np.arange(3):
print('🐻')
print('👧🏼')
🐻 🐻 🐻 👧🏼
for
-loop.int
or an array.int
, we define an int
variable (usually to 0
) before the loop, then use +
to add to it inside the loop.np.append
to add to it inside the loop.np.append
¶name_of_array = np.append(name_of_array, element_to_add)
some_array = np.array([])
np.append(some_array, 'hello')
array(['hello'], dtype='<U32')
some_array
array([], dtype=float64)
# Need to save the new array!
some_array = np.append(some_array, 'hello')
some_array
array(['hello'], dtype='<U32')
some_array = np.append(some_array, 'there')
some_array
array(['hello', 'there'], dtype='<U32')
The function flip(n)
flips n
fair coins and returns the number of heads it saw. (Don't worry about how it works for now.)
def flip(n):
'''Returns the number of heads in n simulated coin flips, using randomness.'''
return np.random.multinomial(n, [0.5, 0.5])[0]
# Run this cell a few times – you'll see different results!
flip(10)
1
Let's repeat the act of flipping 10 coins, 10000 times.
flip
function to flip 10 coins and compute the number of heads we saw.heads_array
.flip
function to flip 10 coins, we'll add an element to the end of heads_array
.# heads_array starts empty – before the simulation, we haven't flipped any coins!
heads_array = np.array([])
for i in np.arange(10000):
# Flip 10 coins and count the number of heads.
num_heads = flip(10)
# Add the number of heads seen to heads_array.
heads_array = np.append(heads_array, num_heads)
Now, heads_array
contains 10000 numbers, each corresponding to the number of heads in 10 simulated coin flips.
heads_array
array([6., 6., 6., ..., 5., 3., 6.])
len(heads_array)
10000
(bpd.DataFrame().assign(num_heads=heads_array)
.plot(kind='hist', density=True, bins=np.arange(0, 12), ec='w', legend=False,
title = 'Distribution of the number of heads in 10 coin flips')
);
for
-loops in DSC 10¶Almost every for
-loop in DSC 10 will use the accumulator pattern.
Do not use for
-loops to perform mathematical operations on every element of an array or Series.
Helpful video 🎥: For Loops (and when not to use them) in DSC 10.
String are sequences, so we can iterate over them, too!
for letter in 'uc san diego':
print(letter.upper())
U C S A N D I E G O
'california'.count('a')
2
Below, complete the implementation of the function vowel_count
, which returns the number of vowels in the input string s
(including repeats). Example behavior is shown below.
>>> vowel_count('king triton')
3
>>> vowel_count('i go to uc san diego')
8
def vowel_count(s):
# We need to keep track of the number of vowels seen so far. Before we start, we've seen zero vowels.
number = 0
# For each of the 5 vowels:
for vowel in 'aeiou':
# Count the number of occurrences of this vowel in s.
count = s.count(vowel)
# Add this count to the variable number.
number = number + count
# Once we've gotten through all 5 vowels, return the answer.
return number
vowel_count('king triton')
3
vowel_count('i go to uc san diego')
8
if
-statements allow us to run pieces of code depending on whether certain conditions are True
.for
-loops are used to repeat the execution of code for every element of a sequence.