How was lab? Was anything confusing?
x = [41, 2, 89, 50, 12, 13]
x
if x[0] % 2 == 0:
print(x[0])
if x[1] % 2 == 0:
print(x[1])
if x[2] % 2 == 0:
print(x[2])
if x[3] % 2 == 0:
print(x[3])
if x[4] % 2 == 0:
print(x[4])
if x[5] % 2 == 0:
print(x[5])
x = [41, 2, 89, 50, 12, 13]
x
x = [41, 2, 89]
x
index = 0
while index < len(x):
# How do I tell if x[index] is even?
if x[index] % 2 == 0:
print(x[index])
index += 1
print("Loop finished!")
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.
a = 1
b = 1
num = 0
while num < 4:
print(a)
c = a + b
a = b
b = c
num += 1
# print(a, b, c, num)
# print()
num = 0
while num < 10:
# print(num)
if num % 2 == 0:
print(num)
num += 1
# print(num)
# print()
# WARNING
# WARNING
# WARNING
# DO NOT RUN THIS
while True:
print('hi')
# WARNING
# WARNING
# WARNING
# DO NOT RUN THIS
num = 0
while num < 10:
print(num)
i = 1
summ = 0
while i <= 10:
summ += i**2
i += 1
print(summ)
385
# I want to add (and print) the first few numbers
# until their sum exceeds 100.
# but nothing is being printed
total = 0
i = 1
while total <= 100:
total += i
i += 1
print(i)
print("Done!")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 Done!
Indentation is critical. It tells Python what code is associated with which Python syntax we've seen so far (if
, while
, functions).
# A tab is four spaces
a = 1
b = 1
if a == 0: # Level 0
print(a) # Level 1
print("hi") # Level 1
if b == 0: # Level 0
print(b) # Level 1
print(b) # Level 0
1
# Do not use one, two, or three spaces.
# Always use four spaces for an indent
a = 0
b = 1
if a == 0:
print(a) # <-- RED IS BAD
print("hi")
print(b)
File "/tmp/ipykernel_12432/575933978.py", line 8 print("hi") ^ IndentationError: unexpected indent
# You can "nest" expressions
num = 0 # Level 0 = zero tabs
while num < 5: # Level 0
if num % 2 == 0: # Level 1 = one tab
print(num) # Level 2 = two tabs
num += 1 # Level 1
print("Done")
0 2 4 Done
# What's the difference?
num = 0
while num < 5:
if num % 2 == 0:
print(num)
num += 1
print("Done")
0
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) /tmp/ipykernel_12432/2903513911.py in <module> 3 num = 0 4 while num < 5: ----> 5 if num % 2 == 0: 6 print(num) 7 num += 1 KeyboardInterrupt:
a = 0
a
0
a = 0
a + 1 # a += 1
# NOT THE SAME THING
a
0
a = 0
a + 1
(a + 1) * 2
a
0
num = 0
while num < 5:
if num % 2 == 0:
print(num)
num += 1
0
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) /tmp/ipykernel_12432/654116488.py in <module> 1 num = 0 2 while num < 5: ----> 3 if num % 2 == 0: 4 print(num) 5 num += 1 KeyboardInterrupt:
You can print anywhere you want. It can help you understand code
sum_of_nums = 0
n = 5
index = 1
while index <= n:
sum_of_nums += index
index += 1
sum_of_nums
15
sum_of_nums = 0
n = 5
index = 1
while index <= n:
# print("Start of while loop")
# print(sum_of_nums)
# print(index)
sum_of_nums += index
index += 1
# print("End of loop")
# print(sum_of_nums)
# print(index)
# print()
sum_of_nums
15
x = 1
x == 1
True
x = 1
y = (x == 1)
y
True
if True: # True/False, comparison, variable with a boolean
print("Hi")
if x == 1:
print("hi")
if y:
print("Hi")
Hi hi Hi
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 add_two_numbers(x, y):
return x + y
add_two_numbers(10, 20)
def add_two_numbers(x, y)
return x + y
add_two_numbers(10, 20)
def add_two_numbers(x, y):
x + y
add_two_numbers(10, 20)
In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers, commonly denoted $F_n$ . The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1 or sometimes (as did Fibonacci) from 1 and 2. Starting from 0 and 1, the first few values in the sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.
def fibonacci(nth):
a = 0
b = 1
num = 0
while num < nth - 1:
c = a + b
a = b
b = c
num += 1
return a
fibonacci(8)
fibonacci(15)
In addition to while loops, there is another kind of loop called a for
loop. Like while
loops, they allow for repeated computation. The syntax and how they work (semantics) are different though.
As usual we will do examples, the syntax, and go in depth.
for x in [1, 2, 3]:
print(x)
1 2 3
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.
x = [41, 2, 89, 50, 12, 13]
for y in x:
print(y)
41 2 89 50 12 13
x = [41, 2, 89, 50, 12, 13]
for y in x: # Level 0
if y % 2 == 0: # Level 1
print(y) # Level 2
2 50 12
x = [1, 2, 3, 4, 5, 6]
sum_of_squares = 0
for y in x:
sum_of_squares += y * y
sum_of_squares
91
x = "hello"
for y in x:
print(y)
h e l l o
x = {
1: 2,
3: 4,
}
for y in x:
print(y)
1 3
x = {
1: 2,
3: 4,
}
for y in x:
print(y, x[y])
1 2 3 4
We've seen condtionals within a for loop. You can also have loops inside loops!
for x in [1, 2]: # Level 0
for y in [4, 5]: # Level 1
print(x, y) # Level 2
1 4 1 5 2 4 2 5
for x in [1, 2, 3]:
y = 0
while y < x:
print(y)
y += 1
print()
When using for
loops, we will sometimes use a special function called range
. Don't worry too much about what it returns for the time being - we will stick to using range in for
loops.
for x in range(3): # [0, 1, 2]
print(x)
0 1 2
for x in range(10):
print(x)
0 1 2 3 4 5 6 7 8 9
for x in range(3, 7): # [3, 4, 5, 6]
print(x)
3 4 5 6
for x in range(6, 9): # [6, 7, 8]
print(x)
6 7 8
for x in range(3, 10, 2): # [3, 5, 7, 9]
print(x)
3 5 7 9
for x in range(10, 3, -1): # [10, 9, 8, 7, 6, 5, 4]
print(x)
10 9 8 7 6 5 4
for x in range(10, 3, -2): # [10, 8, 6, 4]
print(x)
10 8 6 4
x = [1, 2, 3, 4]
for ind in range(len(x)):
print(x[ind])
print()
for y in x:
print(y)
1 2 3 4 1 2 3 4
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
mul_sum = 0
for ind in range(len(x)): # range(4) -> [0, 1, 2, 3]
mul_sum += x[ind] * y[ind]
mul_sum
70
while
loops vs for
loops¶Every while
loop can be written as a for
loop and vice versa. When to use which loop depends on the situation at hand. For the time being, we will practice both ways.
x = [1, 2, 3, 4]
for y in x:
print(y * y)
print()
i = 0
while i < len(x):
print(x[i] * x[i])
i += 1
for num in range(10, 30, 7):
print(num)
print()
num = 10
while num < 30:
print(num)
num += 7
Sometimes we want to end a loop early. We can do this using the break
keyword.
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], x[3] x[4] x[5]
if y == 7:
break
squared_sum += y * y
squared_sum
17
# Fun exercise: do the same with a while loop!
# Breaks can come anywhere in the body of a for loop
y = [1, 2, 3]
for x in y: # x = y[0]
break
print(x)
print("hi")
hi
Sometimes we want to skip the rest of the body of a loop. We can do this using the continue
keyword.
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[3]
if y % 2 == 1:
continue
squared_sum += y * y
squared_sum
16
x = [1, 4, 7, 9, 11, 15]
# Compute the squared sum of the _odd_ values of x
squared_sum = 0
for y in x:
if y % 2 == 0:
continue
squared_sum += y * y
squared_sum
x = [1, 4, 7, 9, 11, 15]
# Compute the squared sum of the _odd_ values of x. Stop at 9
squared_sum = 0
for y in x: # y = x[3]
if y % 2 == 0:
continue
if y == 9:
break
squared_sum += y * y
squared_sum
50
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
Let's say we had a meal with friends. We ordered multiple items.
Let's put the price of the items with a list:
The list might look like [100, 200, 150, 150]
Let's create a function that computes the total for the bill.
def sum_bill(bill):
total = 0
for item in bill:
total += item
return total
sum_bill([100, 200, 150, 150])
sum_bill([100, 200, 200, 150, 150])
But wait! We forgot the tax. Let's say there's a 5% tax rate. We can reuse functions we've created before. Let's try that now
def total_after_tax(bill):
sum_total = sum_bill(bill)
return sum_total * 1.05
total_after_tax([100, 200, 150, 150])
As we've seen, functions can call functions. This will become critical later!
Functions cannot have the same name as a variable and vice versa. For this class, do not name a function a variable you've already declared and vice versa.
There are specific reserved keywords in Python. Do not name functions and variables these reserved keywords:
Types:
int
float
str
bool
list
dict
Syntax:
if
while
for
break
continue
if = 1