Defining Functions source

Creating Functions


In [1]:
a = 23
b = -23
 
if a < 0:
    a = -a
if b < 0:
    b = -b
if a == b:
    print("The absolute values of", a, "and", b, "are equal.")
else:
    print("The absolute values of", a, "and", b, "are different.")


The absolute values of 23 and 23 are equal.

In [2]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=a+%3D+23%0D%0Ab+%3D+-23%0D%0A+%0D%0Aif+a+%3C+0%3A%0D%0A++++a+%3D+-a%0D%0Aif+b+%3C+0%3A%0D%0A++++b+%3D+-b%0D%0Aif+a+%3D%3D+b%3A%0D%0A++++print(%22The+absolute+values+of%22,+a,+%22and%22,+b,+%22are+equal.%22)%0D%0Aelse%3A%0D%0A++++print(%22The+absolute+values+of%22,+a,+%22and%22,+b,+%22are+different.%22)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [4]:
def hello():
    print("Hello")
 
def area(width, height):
    return width * height
 
def print_welcome(name):
    print("Welcome", name)
 
hello()
hello()
 
print_welcome("Fred")
w = 4
h = 5
print("width =", w, " height =", h, " area =", area(w, h))


Hello
Hello
Welcome Fred
width = 4  height = 5  area = 20

In [5]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=def+hello()%3A%0D%0A++++print(%22Hello%22)%0D%0A+%0D%0Adef+area(width,+height)%3A%0D%0A++++return+width+*+height%0D%0A+%0D%0Adef+print_welcome(name)%3A%0D%0A++++print(%22Welcome%22,+name)%0D%0A+%0D%0Ahello()%0D%0Ahello()%0D%0A+%0D%0Aprint_welcome(%22Fred%22)%0D%0Aw+%3D+4%0D%0Ah+%3D+5%0D%0Aprint(%22width+%3D%22,+w,+%22+height+%3D%22,+h,+%22+area+%3D%22,+area(w,+h))&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>


Variables in functions


In [11]:
a = 4
 
def print_func():
    a = 17
    print("in print_func a = ", a)
 
print_func()
print("a = ", a)


in print_func a =  17
a =  4

In [7]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=a+%3D+4%0D%0A+%0D%0Adef+print_func()%3A%0D%0A++++a+%3D+17%0D%0A++++print(%22in+print_func+a+%3D+%22,+a)%0D%0A+%0D%0Aprint_func()%0D%0Aprint(%22a+%3D+%22,+a)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [8]:
a_var = 10
b_var = 15
e_var = 25
 
def a_func(a_var):
    print("in a_func a_var = ", a_var)
    b_var = 100 + a_var
    d_var = 2 * a_var
    print("in a_func b_var = ", b_var)
    print("in a_func d_var = ", d_var)
    print("in a_func e_var = ", e_var)
    return b_var + 10
 
c_var = a_func(b_var)
 
print("a_var = ", a_var)
print("b_var = ", b_var)
print("c_var = ", c_var)
print("d_var = ", d_var)


in a_func a_var =  15
in a_func b_var =  115
in a_func d_var =  30
in a_func e_var =  25
a_var =  10
b_var =  15
c_var =  125
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-f60d74b980a5> in <module>()
     17 print("b_var = ", b_var)
     18 print("c_var = ", c_var)
---> 19 print("d_var = ", d_var)

NameError: name 'd_var' is not defined

In [9]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=a_var+%3D+10%0D%0Ab_var+%3D+15%0D%0Ae_var+%3D+25%0D%0A+%0D%0Adef+a_func(a_var)%3A%0D%0A++++print(%22in+a_func+a_var+%3D+%22,+a_var)%0D%0A++++b_var+%3D+100+%2B+a_var%0D%0A++++d_var+%3D+2+*+a_var%0D%0A++++print(%22in+a_func+b_var+%3D+%22,+b_var)%0D%0A++++print(%22in+a_func+d_var+%3D+%22,+d_var)%0D%0A++++print(%22in+a_func+e_var+%3D+%22,+e_var)%0D%0A++++return+b_var+%2B+10%0D%0A+%0D%0Ac_var+%3D+a_func(b_var)%0D%0A+%0D%0Aprint(%22a_var+%3D+%22,+a_var)%0D%0Aprint(%22b_var+%3D+%22,+b_var)%0D%0Aprint(%22c_var+%3D+%22,+c_var)%0D%0Aprint(%22d_var+%3D+%22,+d_var)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [ ]:

Temperature


In [10]:
def print_options():
    print("Options:")
    print(" 'p' print options")
    print(" 'c' convert from Celsius")
    print(" 'f' convert from Fahrenheit")
    print(" 'q' quit the program")
 
def celsius_to_fahrenheit(c_temp):
    return 9.0 / 5.0 * c_temp + 32
 
def fahrenheit_to_celsius(f_temp):
    return (f_temp - 32.0) * 5.0 / 9.0
 
choice = "p"
while choice != "q":
    if choice == "c":
        c_temp = float(input("Celsius temperature: "))
        print("Fahrenheit:", celsius_to_fahrenheit(c_temp))
        choice = input("option: ")
    elif choice == "f":
        f_temp = float(input("Fahrenheit temperature: "))
        print("Celsius:", fahrenheit_to_celsius(f_temp))
        choice = input("option: ")
    elif choice == "p": #Alternatively choice != "q": so that print when anything unexpected inputed
        print_options()
        choice = input("option: ")


Options:
 'p' print options
 'c' convert from Celsius
 'f' convert from Fahrenheit
 'q' quit the program
option: c
Celsius temperature: 18
Fahrenheit: 64.4
option: q

In [ ]:


In [ ]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=def+print_options()%3A%0D%0A++++print(%22Options%3A%22)%0D%0A++++print(%22+'p'+print+options%22)%0D%0A++++print(%22+'c'+convert+from+Celsius%22)%0D%0A++++print(%22+'f'+convert+from+Fahrenheit%22)%0D%0A++++print(%22+'q'+quit+the+program%22)%0D%0A+%0D%0Adef+celsius_to_fahrenheit(c_temp)%3A%0D%0A++++return+9.0+/+5.0+*+c_temp+%2B+32%0D%0A+%0D%0Adef+fahrenheit_to_celsius(f_temp)%3A%0D%0A++++return+(f_temp+-+32.0)+*+5.0+/+9.0%0D%0A+%0D%0Achoice+%3D+%22p%22%0D%0Awhile+choice+!%3D+%22q%22%3A%0D%0A++++if+choice+%3D%3D+%22c%22%3A%0D%0A++++++++c_temp+%3D+float(input(%22Celsius+temperature%3A+%22))%0D%0A++++++++print(%22Fahrenheit%3A%22,+celsius_to_fahrenheit(c_temp))%0D%0A++++++++choice+%3D+input(%22option%3A+%22)%0D%0A++++elif+choice+%3D%3D+%22f%22%3A%0D%0A++++++++f_temp+%3D+float(input(%22Fahrenheit+temperature%3A+%22))%0D%0A++++++++print(%22Celsius%3A%22,+fahrenheit_to_celsius(f_temp))%0D%0A++++++++choice+%3D+input(%22option%3A+%22)%0D%0A++++elif+choice+%3D%3D+%22p%22%3A+%23Alternatively+choice+!%3D+%22q%22%3A+so+that+print+when+anything+unexpected+inputed%0D%0A++++++++print_options()%0D%0A++++++++choice+%3D+input(%22option%3A+%22)++++++++&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: