How was lab? Was anything confusing?
if <condition>:
<expression>
elif <condition>:
<expression>
else:
<expression>
while <condition>:
<expression>
# list, string, dictionary, or range
for <iterable expression>:
<expressions>
The syntax for while loops is:
while <condition>:
<expression>
As with if
statements, the <condition>
can be anything that evaluates to a boolean. The <expression>
can be complex.
num = 0
while num < 4:
print(num)
num += 1
print("Done!")
0 1 2 3 Done!
a = 1
b = 1
num = 0
while num < 8:
print(a)
c = a + b
a = b
b = c
num += 1
# print(a, b, c, num)
# print()
for
loops¶The basic syntax for for
loops is
for <variable> in <iterable expression>:
<expression>
Don't worry too much about what an <iterable expression>
is for the time being. For this lecture, it will always be a list, string, dictionary, or range
. We will cover range
later.
As with if
s and whiles
, the <expression>
can be complex.
for x in [10, 9]: # [10, 9]
print(x)
10 9
x = [1, 2, 3, 4, 5, 6]
for y in x: # [x[0]]
print(y)
1 2 3 4 5 6
x = [4, 3, 8]
sum_of_squares = 0
for y in x:
sum_of_squares += y * y # 25 + 64 = 89
sum_of_squares
89
y = ""
z = "hello"
for s in z:
y += s # y = "hello"
print(y)
hello
x = {
"key": "value",
4: 10,
}
for key in x: # key = 4
print(x[key])
value 10
print(1, 4)
1 4
for x in [1, 2, 3]: # x = 3
for y in [4, 5]: # y = 4
print(x, y)
print("Done!")
# 1 4
# 1 5
# 2 4
# 2 5
# 3 4
# 3 5
# Done!
1 4 1 5 2 4 2 5 3 4 3 5 Done!
for x in range(3): # [0, 1, 2]
print(x)
0 1 2
# x = [0, 1, 2, 3, 4, ....]
# x[0:3] -> [x[0], x[1], x[2]] -> [0, 1, 2]
# x[3:5] -> [x[3], x[4]] -> [3, 4]
# x[3:8:2] -> [x[3], x[5], x[7]] -> [3, 5, 7]
list(range(3)) # [0, 1, 2]
list(range(3, 8)) # [3, 4, 5, 6, 7]
list(range(3, 8, 2)) # [3, 5, 7]
# [0, 2, 4, 6]
list(range(0, 7, 2))
# [0, 2, 4, ..., 100]
n = 101
list(range(0, n, 2))
# [1, 3, 5, ..., 99]
n = 100
list(range(1, n, 2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
for x in [1, 2, 3]:
y = 0
while y < x:
print(y)
y += 1
print()
for x in range(5):
print(x)
x = [4, 3, 2, 1]
# len(x) = 4
# list(range(len(x)))
# list(range(4))
# [0, 1, 2, 3]
for ind in range(len(x)): # ind = 0
print(ind, x[ind]) # ind = 0, x[0] = 4
0 4 1 3 2 2 3 1
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
mul_sum = 0
# list(range(len(x)))
# [0, 1, 2, 3]
for ind in range(len(x)): # ind = 3
# x[3] -> 4
# y[3] -> 8
# x[3] * y[3] -> 32
# mul_sum = 38 + 32 = 70
mul_sum += x[ind] * y[ind]
mul_sum
70
x = [1, 4, 7, 9, 11, 15]
# Compute the squared sum of the values
# of x until we reach 7
squared_sum = 0
for y in x: # y = x[2] = 7
if y == 7: # (7 == 7) = True
break
# squared_sum = 1 + 16 = 17
squared_sum += y * y
squared_sum
17
# Breaks can come anywhere in the
# body of a for loop
for x in [1, 2, 3]: # x = 1
break
print(x)
print("Done!")
Done!
x = [1, 4, 7, 9, 11, 15]
# Compute the squared sum of
# the _even_ values of x
squared_sum = 0
for y in x: # y = x[1] = 4
if y % 2 == 1: # False
continue
# squared_sum = 0 + 4 * 4 = 16
squared_sum += y * y
squared_sum
16
The general syntax for functions is:
def function_name(<arguments>):
<body>
return <expression>
There are several things to note about the syntax (we will go through examples):
:
after the functiondef fahrenheit_to_celsius(f):
# ???
return 0
def celsius_to_fahrenheit(f):
# ???
return 0
def add_two(x, y): # x = 6, y = 3
# add x and y
z = x * y # z = 6 * 3 = 18
z += x # z = z + 6 = 18 + 6 = 24
z /= y # z = z / y = 24 / 3 = 8.0
return x + y # 9
# Calling a function
a = add_two(6, 3) # 9
a
9
def mystery(x, y): # x = 6, y = 3
return x + y
x = 1
y = 2
return x + y
a = mystery(6, 3) # 9
a
9
def mystery(x, y): # x = 7, y = 4
if x % 2 == 0:
return x + y
else:
return x - y
mystery(7, 4)
3
int("1")
1
We can use other concepts we learned within functions, including if
s, while
s, and for
s.
def minimum_two(x, y): # x = 1, y = 2
if x < y: # (1 < 2) -> True
return x
else:
return y
minimum_two(1, 2)
1
def min_list(l):
# l = [57, 391, 21, 458]
minimum = l[0] # minimum = 57
# l[1:len(l)] = [391, 21, 458]
for x in l[1:len(l)]:
# x = 458
# minimum_two(21, 458)
minimum = minimum_two(minimum, x)
# minimum = 21
return minimum
# min_list([6, 5, 4, 3, 2, 1])
min_list([57, 391, 21, 458])
21
def contains_pineapple(dictionary):
if 'pineapple' in dictionary:
return True
else:
return False
x = {
'apple': 1,
'cherry': 2,
'pineapple': 3,
}
contains_pineapple(x)
True
contains_pineapple(x)
True
Once we define a function once, we can use it anywhere. We can:
if 'apple' in x:
y = contains_pineapple(x)
else:
y = not contains_pineapple(x)
y
True
def minimum_two(x, y):
if x < y:
return x
else:
return y
minimum_two(1, 2)
1
# minimum_two(1, 2)
minimum_two(1, minimum_two(2, 3))
1
def minimum_three(x, y, z):
# x = 1, y = 2, z = 3
min_xy = minimum_two(x, y)
# min_xy = 1
return minimum_two(min_xy, z)
minimum_three(1, 2, 3)
1
So far (up until functions), we've used variables that can be used anywhere in Jupyter. With functions, we will start to use variables that can only be used in certain places.
Where variables can be used is called variable scope.
def variable_scope_example():
scoped_variable = 0
variable_scope_example()
scoped_variable
# This is called using a global variable. Avoid using global variables whenever possible!
x = 0
def variable_scope_example_two():
print(x)
variable_scope_example_two()
Python provides us built in functions, which are functions that we can use for free!
We've already seen some examples: type
, int
, etc.
type(1)
int(True)
min(1, 2)
max(1, 2)