#!/usr/bin/env python # coding: utf-8 # In[12]: # Section 5.5 -- passing arguments to functions #Definition: An argument is any data that you pass # to a function for usage in the function. # Example: print("abc") -- "abc" is the argument of the print function # # Similar idea: a parameter of a function is a variable # local to that function that gets assigned # to an argument # # Both concepts are intimiately related. Just know the difference # in their usage. # # note: book calls parameters "parameter variables" # # # Example -- a doubling function def show_double(number): result=2*number print("The double of",number,"is",result) show_double(3) show_double(1.5) show_double("abc") # peculiar -- the action of "*2" on a string # # behaves as doubling the string # # # Example -- tripling function slightly modified def main(): value=5 show_triple(value) def show_triple(number): result=number*3 print("The triple of",number,"is",result) main() # In[19]: # This program will convert cups to fluid ounces # write a function cups_to_ounces that takes an input # integer number of cups and outputs the equivalent # number of fluid ounces def main(): intro_message() # Get the number of cups from the user number_of_cups=int(input("How many cups do you want to convert?")) cups_to_ounces(number_of_cups) def intro_message(): print("This program converts cups to fluid ounces. For your info:") print("1 cup = 8 fluid ounces") def cups_to_ounces(cups): total_ounces=cups*8 print(cups,"cups is",total_ounces,"fluid ounces") main() # In[28]: # Functions with multiple arguments # Write a function that multiplies two numbers def num_mult(num1,num2): result=num1*num2 print(num1,"multiplied with",num2,"is",result) num_mult(3,4) num_mult(1.2,2) num_mult(2,"abc") #<-- weird, Python interprets # # int*string as repeating string int times # num_mult("abc","def") <-- gives an error -- Python does not # # know how to interpret # # string*string #num_mult(2.0,"abc") #<-- error -- even though 2.0 can be # #typecast as string, Python doesn't do it num_mult(int(2.0),"abc") #<-- works b/c I did the typecast # # # First and last name program first_name=input("What is your first name?") last_name=input("What is your last name?") def first_last(firstname,lastname): print("Your last name is",lastname,"your first name is",firstname) first_last(first_name,last_name) # In[34]: # Keyword arguments def main(): show_interest(rate=0.01,periods=10,principal=10000) show_interest(0.01,10,10000) show_interest(10000,periods=10,rate=0.01) # show_interest(periods=10,10000,rate=0.01) <-- error b/c keyword arg came before pos. arg def show_interest(principal,rate,periods): interest=rate*principal*periods print("The rate is",rate) print("The principal is",principal) print("The simple interest is $",format(interest,'.2f'),sep='') main() # general rule: can mix keyword arguments with non-keyword # arguments, BUT all non-keyword arguments MUST come before # all keyword arguments # In[2]: # Section 5.6 -- Global Variables and Global Constants # Def: A global variable is one that can be accessed by all functions. # # Simple example my_val=10 # this is a global variable because it can accessed # # from within the function showvalue() below def showvalue(): print("my_val is",my_val) showvalue() # There is a keyword global that can make a variable global. def main(): global number number=int(input("Give me an integer.")) show_number() def show_number(): print("Your number is",number) main() # generally -- you want to avoid global variables # as much as possible -- this is because they can make # debugging difficult --- so avoid doing the above as much as possible # Global constants are same idea -- usually they appear # when you define magic numbers. # MAX=4 <-- this is used throughout the code