In [ ]:
from __future__ import print_function


We will see more examples of functions and other things that you might have forgotten¶

You can have a list of lists¶

In [2]:
# e.g.
x=[1,2,3,4,5]
x=['a','b','c','d']
x=[[1,2],3,[4,5,6,7]]
#what is len(x[0][0])? Answer: error because 1 is an int


Assigning variables¶

In [5]:
a=5
b=6

a=b #now what is a? Answer=6
b=new_a #now what is b? Answer=6


What if you wanted to have a=6 and b=5? How would you do this?¶

In [11]:
a=5
b=6
a,b=b,a
print('-----Method 1-----')
print(a)
print(b)
print('-----Method 2-----')
a=5
b=6
new_a=b
b=a
a=new_a
print(a)
print(b)

-----Method 1-----
6
5
-----Method 2-----
6
5

In [13]:
#Defining a function

def myFunction(a): #a=5
return 10*a

a=5
print(myFunction(a))
#What does this print?

50

In [15]:
#Defining a function

def myFunction(a): #a=5
print(10*a)

a=5
print(myFunction(a))
#What does this print?

50
None

Why does it print 50 and then None?¶
In [16]:
#Defining a function

def myFunction(a): #a=5
print(10*a)
return a

a=5
print(myFunction(a))
#What does this print?

50
5

Why does it print 50 and then 5?¶
In [17]:
#Defining a function

def myFunction(a): #a=5
print(10*a)
return a
print ('My name is Timnit')

a=5
print(myFunction(a))
#What does this print?

50
5


Nothing gets executed after the return function so we don't print My name is Timnit¶

In [19]:
#Defining a function

def myFunction(a): #a=5
print(10*a)
return a
print ('My name is Timnit')

a=5
z=myFunction(a)
print(z)
#What does this print?

50
5

In [20]:
#Defining a function

def myFunction(a): #a=5
print(10*a)
return a  #What is wrong here?
print ('My name is Timnit') #What is wrong here?

a=5
z=myFunction(a)
print(z)
#What does this print?

  File "<ipython-input-20-3828dbcd6605>", line 6
print ('My name is Timnit')
^
IndentationError: unexpected indent


We cannot have a return statement outside of a function or we will get an error like below¶

In [22]:
#Defining a function

def myFunction(a): #a=5
print(10*a)
return a
print ('My name is Timnit')

a=5
z=myFunction(a)
print(z)
#What does this print?

  File "<ipython-input-22-d6c827817718>", line 5
return a
SyntaxError: 'return' outside function