# Fill in based on questions
#Confusions from lab
#How does a function work?
#How do you compose functions? f(f(x))
#Scoping and multiple arguments, especially mult(x,y)
#common mistakes
#e.g. def concat("x")
def double (x):
return 2*x
def sumnumbers(timnit,gebru):
tomato=double(timnit)+double(gebru)
return tomato
double('timnit', 'gebru')
apple=sumnumbers('timnit','gebru')
print(apple)
#def f(5x+4)
#naming functions
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-3ea2f71441ac> in <module> 12 tomato=double(timnit)+double(gebru) 13 return tomato ---> 14 double('timnit', 'gebru') 15 apple=sumnumbers('timnit','gebru') 16 print(apple) TypeError: double() takes 1 positional argument but 2 were given
# Every function has a name
# Once you define a function, it can be "called"
def this_is_the_function_name(x):
return x
this_is_the_function_name(100)
100
# Every function takes 0 or more arguments
def func(argument):
return argument
func(10)
10
# Functions can have code inside the function
def function_with_body(x):
y = x + 10
return y
function_with_body(10)
20
x=15
def addTwo(debebe):
print(x)
x+=15
debebe+=5
return debebe
x = 5
print(addTwo(x))
print(x)
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-16-c0ab70155ef3> in <module> 6 return debebe 7 x = 5 ----> 8 print(addTwo(x)) 9 print(x) <ipython-input-16-c0ab70155ef3> in addTwo(debebe) 1 x=15 2 def addTwo(debebe): ----> 3 print(x) 4 x+=15 5 debebe+=5 UnboundLocalError: local variable 'x' referenced before assignment
# The function body must be indented
def function_with_error(lema):
print(lema)
return lema+10
lema=3
x=5
print(function_with_error(x))
print(lema)
5 15 3
# Write a function that returns the argument x plus two
def plusTwo(x):
return x + 2
plusTwo(5)
7
def complicatedFunction(x):
print(x)
x += 5
x *= 3
x -= 2
x %= 100
return x
x=5
mike=complicatedFunction(100)
print(mike)
print(x)
100 13 5
# indentation is critical
# You can call functions within functions
def addTwo(x):
return x + 2
def mystery(x):
y = addTwo(x)
z = addTwo(y)
return z
print(mystery(10))
print(z)
14
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-21-c905c1496718> in <module> 9 10 print(mystery(10)) ---> 11 print(z) NameError: name 'z' is not defined
# Comparisions: since I forgot
1 > 0
True
1 < 0
False
1 >= 1
True
1 <= 1
True
# This is condtional syntax
if CONDITION:
DO_THING_HERE
elif CONDITION: # Optional
DO_THING_HERE
else: # Optional
DO_THING_HERE
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-26-71dd40102c09> in <module> 1 # This is condtional syntax ----> 2 if CONDITION: 3 DO_THING_HERE 4 elif CONDITION: # Optional 5 DO_THING_HERE NameError: name 'CONDITION' is not defined
# Here is an example of an if statement
if True==True:
print('Hello world!')
Hello world!
if False==True:
print('Hello world!')
# And with a conditional
if 1 == 1:
print('Hello world!')
Hello world!
# Else is a catch-all
if 1 == 1:
print('Hello world!')
else:
print('Goodbye world!')
Hello world!
if 1 == 2:
print('Hello world!')
else:
print('Goodbye world!')
Goodbye world!
# elif only triggers on the condition
x = 1
if x == 0:
print('x = 0')
elif x == 2:
print('x = 2')
# They can be combined
x = 1
if x == 0:
print('x = 0')
elif x == 2:
print('x = 2')
else:
print('x is neither 0 nor 2')
x is neither 0 nor 2
# Here's an example of a conditional in a function
def passingGrade(grade):
if grade > 70.0:
return True
else:
return False
daniel_grade = 60.0
if passingGrade(daniel_grade):
print('Daniel passed!')
else:
print('Daniel failed! :(')
Daniel failed! :(
# List indexing review
x = [0, 1, 2, 4, 5]
print(x[1])
print(x[-1])
print(x[-3])
print(x[2])
1 5 2 2
# List slicing review
x = [1, 'hello', 0, 'world']
print(x[0:2])
[1, 'hello']
x = [1, 'hello', 0, 'world']
print(x[:4])
[1, 'hello', 0, 'world']
x = [1, 'hello', 0, 'world']
print(x[-2:])
print(x[:-1])
[0, 'world'] [1, 'hello', 0]
# List assignment review
x = [1, 'hello', 0, 'world']
x[2] = 2
print(x)
[1, 'hello', 2, 'world']
# List len review
x = [1, 'hello', 0, 'world']
print(len(x))
4
# Review of strings vs lists
x = [0, 1, 2, 3, 4, 5]
y = '012345'
print(type(x))
print(type(y))
<class 'list'> <class 'str'>
# Review of printing strings
y = '012345'
print(y)
012345
# Differences in indexing
x = [0, 1, 2, 3, 4, 5]
y = '012345'
x[0]
0
x = [0, 1, 2, 3, 4, 5]
y = '012345'
y[0]
'0'
x = [0, 1, 2, 3, 4, 5]
y = '012345'
type(y[0])
str
x = [0, 1, 2, 3, 4, 5]
y = '012345'
print(x[0])
print(y[0])
0 0
# Differences in slicing
x = [0, 1, 2, 3, 4, 5]
y = '012345'
print(x[0:2])
print(y[0:2])
[0, 1] 01
# Assignment for lists
x = [0, 1, 2, 3, 4, 5]
y = '012345'
x[3] = -1
x
[0, 1, 2, -1, 4, 5]
# Assignmenet for strings fails
x = [0, 1, 2, 3, 4, 5]
y = '012345'
y[3] = -1
y
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-49-9c6a92d71805> in <module> 2 x = [0, 1, 2, 3, 4, 5] 3 y = '012345' ----> 4 y[3] = -1 5 y TypeError: 'str' object does not support item assignment