Presentation on theme: "Order of operators: 1.x ** y Power (right associative) 2.x * y, x / y, x // y, x % y Multiplication, division, floor division, modulo 3.x + y, x - y Addition,"— Presentation transcript:
Order of operators: 1.x ** y Power (right associative) 2.x * y, x / y, x // y, x % y Multiplication, division, floor division, modulo 3.x + y, x - y Addition, subtraction Shall we try this? / //3 -7//3 5*2**3 7%2 18% **3 (3 + 2) ** 3 12/2 ** 2
Files 1.Open a file In IDLE, go to File->New Window 2.In New Window: Type: File->Save As Save the file as first.py 4.Run the file: Run->Run Module Now you’ve saved your work so you can run it later When saving a python program, it must have a.py extension!!! So interpreter knows it’s python code.
Functions We have a file: we can save our work. Now, let’s create “functions” to name code we might want to use again Math: function takes a number or numbers and transforms it to another number E.g., f(x) = 2x f(3) = 6 f(5) = 10 g(x) = x g(2) = 9 g(5) = 126
Creating a function: Function (mathematical) Consists of 3 parts and a name: -> name: g (not a good name! Tells us nothing about what this function does) -> input parameter in the example, integers 2 or 5 -> instructions (code) in the example, x** > output in the example, the integer 9 or 126 g(x) = x g(2) = 9 g(5) = 126
Function (in Python) def g(x): return(x ** 3 + 1) g(x) = x g(2) = 9 g(5) = 126
Function (in Python) def g(x): return(x ** 3 + 1) To Call the Functions (to make them run): g(2) g(5) To see what the function calculates (returns): print (g(2)) print (g(5)) g(x) = x g(2) = 9 g(5) = 126
Input Values: Parameters values into the function are known as parameters 3,2 addfunc 5 7,4 addfunc11 9,8 addfunc 17 Code: def addfunc(value1,value2): return (value1 + value2) print(addfunc(3,2)) print(addfunc(7,4)) print(addfunc(9,8)) We should know what we want to come out of the function, so we can check to make sure the function is working correctly Print allows us to check the value that is coming out of the function.
Function: Calculate the area of a rectangle? 1.Name of function? 2.Input? 3.Output? 4.Test cases? 5.Calculations? Can we now write the function? def arearectangle(len,width): return(len*width) func(x,y) = x*y func(2,7) = 14 func(5,4) = 20
Other functions? Fahrenheit to celsius? Take the temperature in Fahrenheit and subtract 32. Divide by 1.8. The result is degrees Celsius. 1.Function name? 2.Input? 3.Output? 4.Calculations? 5.Test cases?
1.Function name? f_to_c 2.Input? An integer (the fahrenheit temperature) 3.Output? A float (the celsius temperature) 4.Calculations? (ftemp – 32) / Test Cases? f_to_c(68) -> 20.0 f_to_c(22)-> def f_to_c(ftemp): return((ftemp - 32 )/ 1.8) print(f_to_c(32,47)) func(x) = (x-32)/1.8 func(68) = 20.0 func(22) =
Comments #This function calculates the square of the input value #and returns that squared value #input: an integer #output: an integer #Test Cases: # print(newfunc(3)) -> 27 # print(newfunc(5)) -> 3125 # print(newfunc(2))-> 4 #Author: Debra Yarrington #Sept 6, 2011 def newfunc(par1): return(par1**par1) # returns the square Comments aren’t executed (aren’t converted to machine language). Python’s compiler ignores them. They’re for people who are reading your code. They also can be used to help you (and others) understand what you are doing and why
What we’ve learned about writing functions: We should come up with test cases first How many parameters go into the function in order to get the output? We should include comments that clearly describe how the function works. These comments should include our test cases After we’ve got the test cases and the function description, then we write the function. Basically, you have to think it through before you write the function.
Functions: Math: f(x) = x 3 Python:def f(x): return(x**3) Given a particular input to this function, will we ALWAYS get the same output? e.g. f(2) f(3) Could we say that f(2) is equivalent to 8? Could we say that f(3) is equivalent to 27?
Functions(how they work) def f(x): # code for a function that return(x**3) # returns the cube of a number f(2) # Calls the function. The function is now executed (i.e., calculated, # converted to machine language and instructions run by the CPU). # # After f(2) runs, all that remains is what is RETURNED
If /else (branching) def f(x): if x > 0: return (3**2/x) else: return (0) f(3) # this equals? f(0) # this equals? f(-2) # this equals? 3 2 if x > 0 _ f(x) = x 0 otherwise
Piecewise functions x 3 + 2x if x > 2 f(x) = -x 3 + 2x if x < 0 -1 otherwise
If /else (branching) def f(x): if x > 2: return (x ** * x) elif x < 0: return(-x ** * x) else: return (-1) f(3) # this equals? f(0) # this equals? f(-2)# this equals? x 3 + 2x if x > 2 f(x) = -x 3 + 2x if x < 0 -1 otherwise
Comparators (return T or F) ==equal to5==5true !=not equal to8!=5true >greater than3>10false =greater than 6>=8false or equal to <=less than6<=8true or equal to
Note: == if conditions MUST use == (equality) not = (assignment) == Asks a question: is this equal to that??? this == that ? Yes or No! True, this is equal to that, or False, this is not equal to that = We’ll see this in use shortly
Example def f(x): if x > 10: return (x+9) elif x < 7: return (x + 4) else: return(0) print(f(12)) # what is printed? print(f(6)) # what is printed? print(f(8)) # what is printed? print(f(7)) # what is printed?
Example def f(x): if x != 10: return (x * 2) else: return (x ** 2) print(f(6)) print(f(10))
Example def f(x): if x < 10: return (x+9) elif x == 5: return (x + 4) elif x >10: return (x) else: return(0) print(f(5)) ?