#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
#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
60
def timnit_multiplier(a,b,c,d,e,f):
return myMultiplier(a,b,c)*myMultiplier(d,e,f)
#what does this return?
def passing_grade(h):
if h>50:
print 'good'#True
return True
else:
print 'bad'#False
return False
#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'
y=candy_for_grade(100)
print 'y='+str(y)
good y=no_candy
#if x is None it is evaluated as False.
x=None
if x:
print 'good'
else:
print 'bad'
bad
candy_for_grade(51)
good
'no_candy'
y=timnit_multiplier(3,4,5,6,7,8)
print x
#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)
#for loops
fruits=['orange', 'pineapple','banana','mango']
x='timnit'
x='meseret'
for x in fruits:
if x=='pineapple':
print x
#break
#continue
print x
orange pineapple pineapple banana mango
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
[0, 1, 2, 3]
y=range(0,len(str)/2)
print y
y[0]
type(y[0])
print [y[0]]
print (type([y[0]]))
[0, 1, 2, 3] [0] <type 'list'>
numbers=[1,2,3,4,5,6]
for x in xrange(7):
if x==3:
continue
#break
print x
print x
0 1 2 4 5 6 6
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
fruits=['orange', 'pineapple','banana','mango']
#my_fruits=['my_orange', 'my_pineapple','my_banana','my_mango']
#my_favorite_fruits=['pineapple','mango']
#best_fruit = 'banana'
#what does this print?
fruits=['orange', 'pineapple','banana','mango']
x='timnit'
x='meseret'
for x in fruits:
print x
print x
orange pineapple
#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
#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
#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
pineapple meseret
#what does this print?
fruits=['orange', 'pineapple','banana','mango']
my_fruits=[]
for x in fruits:
print my_fruits
[] [] [] []
#Example of for loop
fruits = ['orange', 'pineapple', 'banana', 'mango']
my_fruits=[]
for x in fruits:
print x
my_fruits += ['my_'+x]
my_fruits
orange pineapple banana mango
['my_orange', 'my_pineapple', 'my_banana', 'my_mango']
#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']])
3 3 2
#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
[0, 1, 2, 3, 4] [2, 3, 4] [0, 2, 4, 6, 8]
#What does this do
# Example 1
for x in range(1000):
if x == 6:
break
print x
#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