#!/usr/bin/env python # coding: utf-8 # 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] #what is x[0]? Answer: 1 x=['a','b','c','d'] #what is x[0]? Answer: a x=[[1,2],3,[4,5,6,7]] #what is x[0]? Answer: [1,2] #what is x[1]? Answer: 3 #what is x[2]? Answer: [4,5,6,7] #what is len(x)? Answer: [3] #what is x[0][0]? Answer: 1 #what is x[0][1]? Answer: 2 #what is len(x[0][0])? Answer: error because 1 is an int #what about len(x[0])? Answer: 2 # ### Assigning variables # In[5]: a=5 b=6 a=b #what is a? Answer=6 b=a #what is b? Answer=6 new_a=a #what is new_a? Answer=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) # In[13]: #Defining a function def myFunction(a): #a=5 return 10*a a=5 print(myFunction(a)) #What does this print? # In[15]: #Defining a function def myFunction(a): #a=5 print(10*a) a=5 print(myFunction(a)) #What does this print? # ##### 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? # ##### 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? # #### 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? # 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? # #### 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?