#!/usr/bin/env python # coding: utf-8 # In[ ]: #We are going to go over concepts from yesterday and things #That people might be confused about. #How to define a function and what it means #Calling functions within functions #Calling functions confused when to define functions #and when to define variables #Intuition behind functions #e.g. Def inches to centimeters x #Make connection that x is input in a function #The word return #Go through lab 1 #Indentation, columns #Different brackets # In[130]: #Write a function called myMultiplier #that takes in 3 variables and returns the #multiplication of those 3 variables. And those #Variables can be int, float, long, def myMultiplier(a,b,c): return a*b*c x=3 y=4 z=5 timnit= myMultiplier(x,y,z) #timnit = myMultiplier(3,4,5) print timnit # In[254]: def timnit_multiplier(a,b,c,d,e,f): return myMultiplier(a,b,c)*myMultiplier(d,e,f) #what does this return? # In[115]: def passing_grade(h): if h>50: print 'good'#True return True else: print 'bad'#False return False # In[ ]: #One function can call another function. What does this function do? def candy_for_grade(g): if passing_grade(g): return 'candy' else: return 'no_candy' # In[119]: y=candy_for_grade(100) print 'y='+str(y) # In[123]: #if x is None it is evaluated as False. x=None if x: print 'good' else: print 'bad' # In[255]: candy_for_grade(51) # In[59]: y=timnit_multiplier(3,4,5,6,7,8) print x # In[ ]: #How to define a function (review from lab 1) def inchesToCentimeters(x): return x*2.54 def doubleIt(x): return 2*x def timeFromSeconds(x): hour = x/60/60 minutes = x/60%60 seconds = x%60 return str(hour) + ':' + str(minutes) + ':' + str(seconds) # In[241]: #for loops fruits=['orange', 'pineapple','banana','mango'] x='timnit' x='meseret' for x in fruits: if x=='pineapple': print x #break #continue print x # In[217]: x=[] str='a b c d e' y=range(0,len(str)/2) for y in range(0,len(str)/2): x += [y] print x # In[223]: y=range(0,len(str)/2) print y y[0] type(y[0]) print [y[0]] print (type([y[0]])) # In[195]: numbers=[1,2,3,4,5,6] for x in xrange(7): if x==3: continue #break print x print x # In[ ]: my_fruits=[] len(my_fruits) my_fruits += ['my_'+fruits[0]] my_fruits += ['my_'+fruits[1]] my_fruits += ['my_'+fruits[2]] my_fruits += ['my_'+fruits[3]] print my_fruits x=67 x=([67]) y=[] y += [x[0]] print y # In[253]: fruits=['orange', 'pineapple','banana','mango'] #my_fruits=['my_orange', 'my_pineapple','my_banana','my_mango'] #my_favorite_fruits=['pineapple','mango'] #best_fruit = 'banana' # In[158]: #what does this print? fruits=['orange', 'pineapple','banana','mango'] x='timnit' x='meseret' for x in fruits: print x print x # In[ ]: #what does this print? fruits=['orange', 'pineapple','banana','mango'] x='timnit' x='meseret' for y in fruits: if y=='pineapple': break print y print x # In[ ]: #what does this print? fruits=['orange', 'pineapple','banana','mango'] x='timnit' x='meseret' for y in fruits: if y=='pineapple': break print y print x # In[156]: #what does this print? fruits=['orange', 'pineapple','banana','mango'] x='timnit' x='meseret' for y in fruits: if y=='pineapple': break print y print x # In[256]: #what does this print? fruits=['orange', 'pineapple','banana','mango'] my_fruits=[] for x in fruits: print my_fruits # In[257]: #Example of for loop fruits = ['orange', 'pineapple', 'banana', 'mango'] my_fruits=[] for x in fruits: print x my_fruits += ['my_'+x] my_fruits # In[258]: #Other useful functions: #It will be helpful for today's lab to know the #following functions. #len(x) returns the length of an iterable data #type (such as a str or list) as an int. For example, print len('abc') print len(['a', 'b', 'c']) print len(['a', ['b', 'c', 'd']]) # In[259]: #range(x) returns a list of ints from 0 to x − 1. #example, print range(5) print range(2, 5) #Start at 2 print range(0, 10, 2) #give every 2 values back # In[ ]: #What does this do # Example 1 for x in range(1000): if x == 6: break print x # In[ ]: #Convert ints to strings #Say I have 3 variables hours=0 minutes=1 seconds=0 #I want to output '0:1:0' #The code below does that str(hours)+':'+str(minutes)+':'+str(seconds) #str(x) converts x to string from int