# Fill in based on questions
#difference betwee print and return
#writing stuff after return
def multiply (a,b):
print(a*b)
return a*b
timnit = multiply(2,3)
print(timnit)
l=['apple', 'banana', 'mango', 'avocado', 'tomato']
for fruit in l:
print(fruit)
# This is the generic syntax for loops
# The loop body is not optional
for VARIABLE in {LIST, STRING, range}:
FOR_LOOP_BODY
# 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 timnit in [0, 1, 2]:
print(timnit)
# or a variable
y = [0, 1, 2, 'timnit']
for x in y:
print(x)
print(y)
# Lists can contain any type
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# You can put control flow in a loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == 'apple':
print(x)
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x != 'apple':
print(x)
banana cherry
# 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
#Review from lab
#fill in based on questions
#for loops
#elif vs else vs
#many if statements
#return means function ends
#return vs print
#confused about scope (jupyter still remembers last variable)
#how to call functions (assign output to variable)
# 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
# What will this do?
for x in range(0):
print(x)
# 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
# What will this do?
for x in range(3, 3):
print(x)
# 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, 3):
print(x)
3 6
# 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)
# 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
# Remebmer that you can have control flow in functions
def passingGrade(score):
x=True
if score > 50.0:
x=True
if score != 61.0:
x=False
print(x)
print(passingGrade(60.0))
print(passingGrade(25.0))
False None False None
# You can also have loops in functions
def loopInFunction(x):
for i in range(x):
print(i)
print(loopInFunction(4))
# What's the difference between this function and the one above?
def loopInFunction2(x):
for i in x:
print(i)
t=[0, 1, 10, -2, 5]
y=loopInFunction2(t)
0 1 10 -2 5
print(y)
None
# Remember how loops over strings work
def loopInFunction2(x):
for i in x:
print(i)
loopInFunction2('5637291')
# 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]))
# 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]))