Presentation on theme: "Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y"— Presentation transcript:
1Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y Power (right associative)x * y, x / y, x // y, x % yMultiplication, division, floor division, modulox + y, x - yAddition, subtractionShall we try this?5 + 4 / 2 + 17//3-7//35*2**37%218%43 + 2**3(3 + 2) ** 312/2 ** 2Class 2
2Files Open a file In New Window: File->Save As Run the file: In IDLE, go to File->New WindowIn New Window:Type:3 + 2File->Save AsSave the file as first.pyRun the file:Run->Run ModuleNow you’ve saved your work so you can run it laterWhen saving a python program, it must have a .py extension!!!So interpreter knows it’s python code.Class 1
3Functions We have a file: we can save our work. Now, let’s create “functions” to name code we might want to use againMath: function takes a number or numbers and transforms it to another numberE.g., f(x) = 2x f(3) = 6 f(5) = 10g(x) = x3 + 1g(2) = 9g(5) = 126
4Creating 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 parameterin the example, integers 2 or 5-> instructions (code)in the example, x**3 + 1-> outputin the example, the integer 9 or 126g(x) = x3 + 1g(2) = 9g(5) = 126
6Function (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) = x3 + 1g(2) = 9g(5) = 126
7Input Values: Parameters values into the function are known as parameters3, addfunc7, addfunc 119, addfuncCode: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 correctlyPrint allows us to check the value that is coming out of the function.
8Function: Calculate the area of a rectangle? Name of function?Input?Output?Test cases?Calculations?Can we now write the function?def arearectangle(len,width):return(len*width)func(x,y) = x*yfunc(2,7) = 14func(5,4) = 20
9Other functions? Fahrenheit to celsius? Function name? Input? Output? Take the temperature in Fahrenheit and subtract 32.Divide by 1.8.The result is degrees Celsius.Function name?Input?Output?Calculations?Test cases?
11Comments#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, 2011def newfunc(par1):return(par1**par1) # returns the squareComments 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
12What we’ve learned about writing functions: We should come up with test cases firstHow 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 casesAfter 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.
13Functions: Math: f(x) = x3 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?
14Functions(how they work) def f(x): # code for a function thatreturn(x**3) # returns the cube of a numberf(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
23Piecewise functions32 if x > 0 𝑥 f(x) = 0 otherwise
24If /else (branching) def f(x): if x > 0: return (3**2/x) else: f(3) # this equals?f(0) # this equals?f(-2) # this equals?if x > 0f(x) = x_0 otherwise
25Piecewise functionsx3 + 2x if x > 2 f(x) = -x3 + 2x if x < 0 -1 otherwiseClass 3
26If /else (branching) def f(x): if x > 2: return (x ** 3 + 2 * x) x3 + 2x if x > 2f(x) = x3 + 2x if x < 0otherwisedef 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?
27Comparators (return T or F) == equal to 5==5 true!= not equal to 8!=5 true> greater than 3>10 false< less than 5<8 true>= greater than 6>=8 false or equal to<= less than 6<=8 true or equal toClass 3 stopped here
28Note: == 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, orFalse, this is not equal to that=We’ll see this in use shortly
29Exampledef 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?
30Exampledef f(x): if x != 10: return (x * 2) else: return (x ** 2) print(f(6)) print(f(10))
31Exampledef 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)) ?Class 2 stopped here