How was lab? Was anything confusing?
We covered:
x = [1, True, "hello"]
x
x = [1, 2.0, True, "hello", [1, 2]]
x
x[0]
x[4]
x[-3]
x[5]
x(0)
x
x[1] = False
x
x
# list_name[start:end]
# IGNORE end
x[1:2]
x[1:1]
x[2:4] # [x[2], x[3]]
len(x)
x
# DO NOT USE x[1:] -> x[1:len(x)]
# DO NOT USE x[:2] -> x[0:2]
y = x[1:len(x)]
y
x
# list_name[start:end:step]
y = x[1:4:2] # [x[1], x[3]]
y
x[1:4:1] # [x[1], x[2], x[3]]
# DO NOT USE x[:3:]
x[:3:]
x[0:3]
x
y = 1
z = 3
x[y:z]
x[y + 1:z]
x
x[-3:-1]
x[-1:-3]
x[0:2:-1]
x
x[2:0:-1] # [x[2], x[1]]
x[-1:-3:-1] # [x[-1], x[-2]]
x[4:2]
len(x)
y = "hello"
y
y = "hello !"
len(y)
y[1:3] # y[1] y[2]
y[1] = "a"
x = {
1: 2,
3: 4,
4: 5,
6: 7,
"hi": "bye",
8: [1, 2],
}
x
x[10]
x["hi"]
x[1] = 4
x
x[1]
x[10] = 10
x
x = {
'banana': 1,
'apple': 2,
}
x['banana'] + x['apple']
x = [1, 2, 3]
x[0] + x[2]
x = {
'banana': 1,
'apple': 2,
}
x['banana'] = 3
x['banana']
x = {
'banana': 1,
'apple': 2,
}
del x['banana']
x
x = {
0: 1,
1: "hello",
2: True,
}
x = [1, "hello", True]
x[2]
Lists can contain any Python "thing", including other lists!
x = [1, 2, 3]
x
y = [x, x]
y
dictionary = {
1: 2,
3: 4
}
x = [dictionary, 1, "hi"]
x
This is a confusing topic. Basic "things" in Python (int
, float
, str
, bool
) are passed by value, which means they are copied when they are assigned to a variable.
x = 1
y = x
x = 2
print(x) # x changes
print(y) # y stays as is
Lists and dictionaries are not copied. Instead, variables assigned to a list or dictionary store a reference to the list/dictionary. Let's see this through examples
x = [1, 2, 3]
y = x
print(x)
print(y)
x = [1, 2, 3]
y = x
print(x)
print(y)
print()
x[0] = 5
print(x)
print(y)
x = [1, 2, 3]
y = x
print(x)
print(y)
print()
x = [4, 5, 6]
print(x)
print(y)
x = [1, 2, 3]
y = x
print(x)
print(y)
print()
y = [4, 5, 6]
x[0] = 5
print(x)
print(y)
x = [1, 2, 3]
y = [1, 2, 3]
print(x)
print(y)
print()
x[0] = 5
print(x)
print(y)
x = [1, 2, 3]
y = x
z = y
print(x)
print(y)
print(z)
print()
x[0] = 5
print(x)
print(y)
print(z)
The general syntax for conditionals is
if <condition>:
<expression>
elif <condition>:
<expression>
else:
<expression>
A <condition>
is an <expression that evaluates to a boolean>
.
numeric_grade = 75
if numeric_grade > 90:
letter_grade = 'A'
elif numeric_grade > 80:
letter_grade = 'B'
elif numeric_grade > 70:
letter_grade = 'C'
elif numeric_grade > 60:
letter_grade = 'D'
else:
letter_grade = 'F'
letter_grade
Now that we know what a conditional is, we will use boolean expressions in a different way.
Often times, we want to repeat a comptuation a number of times.
x = [32587, 18, 43, 985, 274398]
print(x[0])
print(x[1])
print(x[2])
print(x[3])
print(x[4])
We'll start with some examples before defining the syntax
x = [32587, 18, 43, 985, 274398]
index = 0
while index < len(x): # len(x) = 5
print(x[index])
index += 1
print("Finished while loop")
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("Finished while loop!")
# num is 4 at this point
while num < 4:
print(num)
num += 1
print("Finished the second while loop!")
# 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)
num = 0
while num < 5:
if num % 2 == 0:
print(num)
num += 1
num = 0
if num % 2 == 0:
while num < 3:
print(num)
num += 1
num = 1
while num < 20:
print(num)
num = num * 2
# Sum numbers from 1 to n
sum_of_nums = 0
n = 1000
index = 1
# Sum to 5
sum_of_nums += 1
sum_of_nums += 2
sum_of_nums += 3
sum_of_nums += 4
sum_of_nums += 5
sum_of_nums
sum_of_nums = 0
n = 125
index = 1
while index <= n:
sum_of_nums += index
index += 1
sum_of_nums
a = 1
b = 1
num = 0
while num < 15:
print(a)
c = a + b
a = b
b = c
num += 1
Many times, we want to do the same computation on many different inputs.
Right now, it's quite annoying
n = 3
sum_of_nums = 0
index = 1
# index = 1
# sum_of_nums = 1
# index = 2
# sum_of_nums = 3
# index = 3
# sum_of_nums = 6
# index = 4
# sum_of_nums = 10
# index = 5
# sum_of_nums = 15
while index <= n:
sum_of_nums += index
index += 1
sum_of_nums
x = [[1, 2], [3, 4]]
x[0][0] = x[0][0] * -1
x[0]
x
n = 4728
sum_of_nums = 0
index = 1
while index <= n:
sum_of_nums += index
index += 1
sum_of_nums
# Compute the 8th fibonacci number
a = 1
b = 1
num = 0
while num < 8:
print(a)
c = a + b
a = b
b = c
num += 1
# Compute the 15th fibonacci number
a = 1
b = 1
num = 0
while num < 15:
print(a)
c = a + b
a = b
b = c
num += 1
Can we avoid repeating this kind of computation? Yes we can!
We will use function.
As before, we'll start with examples, describe the syntax, and go through more examples.
def sum_numbers_to_n(n):
sum_of_nums = 0
index = 1
while index <= n:
sum_of_nums += index
index += 1
return sum_of_nums
sum_numbers_to_n(125)
def fibonacci(nth):
a = 1
b = 1
num = 0
while num < nth - 1:
c = a + b
a = b
b = c
num += 1
return a
fibonacci(8)
fibonacci(15)
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)
# You also need the indentation
def add_two_numbers(x, y):
x + y
add_two_numbers(20, 20)
Pay close attention to the exercises. Think of interesting functions to write!
def fahrenheit_to_celsius(f):
# ???
return 0
def celsius_to_fahrenheit(c):
# ???
return 0
int("a")