Review from yesterday¶

In [ ]:
# Fill in based on questions


For loops¶

In [ ]:
# This is the generic syntax for loops
# The loop body is not optional
for VARIABLE in {LIST, STRING, range}:
FOR_LOOP_BODY

In [36]:
# Looping over a LIST will go through the elements of a list in order
# and perform the loop body.
# The list can be a constant
for x in [0, 1, 2]:
print(x)

0
1
2

In [37]:
# or a variable
y = [0, 1, 2]
for x in y:
print(x)

0
1
2

In [38]:
# Lists can contain any type
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

apple
banana
cherry

In [39]:
# You can put control flow in a loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == 'apple':
print(x)

apple

In [40]:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x != 'apple':
print(x)

banana
cherry

In [6]:
# The rules for looping over strings are the same as lists,
# except with the string being taken one character at a time
for x in 'apple':
print(x)

a
p
p
l
e

In [42]:
# range is special. Range can take 1, 2, or 3 arguments
# In this course, you will only use range in for loops
# When range takes 1 argument (call it n), it is like looping over the list from 0 to n-1
for x in range(5):
print(x)

0
1
2
3
4

In [2]:
# What will this do?
for x in range(0):
print(x)

In [44]:
# range with two arguments (call them n and m) is like looping over the list from n to m-1
for x in range(3, 6):
print(x)

3
4
5

In [3]:
# What will this do?
for x in range(3, 3):
print(x)

In [45]:
# Range with three arguments (call them n, m, and step)
# is like looping over the list from n to m-1, but incrementing by step
for x in range(3, 8, 2):
print(x)

3
5
7


In [4]:
# You can put loops inside loops
# ALWAYS make sure the variables for the loops are different
for i in range(3):
for j in range(3): # DO NOT USE i HERE
print(i, j)

0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

In [5]:
# This is super tricky. Walk through the loop one step at a time
for i in range(3):
for j in range(i):
print(i, j)

1 0
2 0
2 1


More on functions¶

In [47]:
# Remebmer that you can have control flow in functions
if score > 50.0:
return True
else:
return False


True
False

In [49]:
# You can also have loops in functions
def loopInFunction(x):
for i in range(x):
print(i)

loopInFunction(4)

0
1
2
3

In [50]:
# What's the difference between this function and the one above?
def loopInFunction2(x):
for i in x:
print(i)

loopInFunction2([0, 1, 10, -2, 5])

0
1
10
-2
5

In [51]:
# Remember how loops over strings work
def loopInFunction2(x):
for i in x:
print(i)

loopInFunction2('5637291')

5
6
3
7
2
9
1

In [52]:
# Functions can call functions
def func1(x):
if len(x) > 5:
return True
else:
return False

def func2(x):
return func1(x)

print(func2([0, 1, 2]))
print(func2([0, 1, 2, 3, 4, 5, 6, 7]))

False
True

In [54]:
# More complicated example of functions calling functions
def func1(x):
if len(x) > 5:
return True
else:
return False

def func2(x):
if func1(x):
print('len(x) > 5')
else:
print('len(x) <= 5')

func2([0, 1, 2])
func2([0, 1, 2, 3, 4, 5, 6, 7])
print(func2([0]))

len(x) <= 5
len(x) > 5
len(x) <= 5
None