Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


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:

1 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

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.

3 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

4 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

5 Function (in Python) def g(x): return(x ** 3 + 1) g(x) = x g(2) = 9 g(5) = 126

6 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

7 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.

8 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

9 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?

10 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) =

11 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

12 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.

13 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?

14 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

15 Using functions: def add2(x,y): return(x + y) def add(x,y): return(add2(x,y) + add2(x,y)) print(add(7,3))

16 Using functions: def add2(x,y): return(x + y) def div(x,y,z): return(add2(x,y) / z) print(div(7,3,2))

17 Using functions: def add2(x,y): return(x + y) def div(x,z): return(add2(x,3) / z) print(div(7,2))

18 Using functions: def add2(x,y): return(x + y) def div(y,x): return(add2(y,3) / x) print(div(7,2))

19 Using functions: def add2(x,y): return(x + y) def add3(y,x): return(add2(y,3) + add2(11,x)) print(add3(7,2))

20 def f1(par1, par2): return(par2 - par1) print(f1(2,4)) #2 def f2(x1,x2): return(x1**2 + x2) print(f2(3,6)) #15 def f3(p1,p2): return(f2(p1,p2) + f1(p1,p2)) print(f3(3,2)) 10 def f4(p1,p2): return(f2(p2,p2) - f1(p1,p1)) print(f4(4,2)) 6 def f5(q1,q2): return(f2(q2,q1)) print(f5(17,5)) 42 def f6(par1,par2): return( 3 + f1(par1, 17+par1)) print(f6(4,26)) 20

21 def Func1(p1,p2): return(Squr(p1) - Squr(p2)) print(Func1(4,3)) >>7 def Func2(p1,p2,p3): return(Squr(p1) * Func1(p2,p3)) print(Func2(2,3,2)) >>20 def Func3(p1,p2): return(dbl(Squr(p1))) print(Func3(4)) >>AACH CRASH BURN def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) >> 70 def Func5(p1): return(dbl(dbl(dbl(p1)))) print(Func5(4)) >>32 def Func6(p1): return(dbl(dbl(dbl(Squr(p1)+1))-Squr(3))) print(Func6(-2)) >>22 Given the function def Squr(par1): return(par1 ** 2) def dbl(par2): return(par2 + par2)

22 def Func1(p1,p2): return(Squr(p1) - Squr(p2)) print(Func1(4,3)) >>7 def Func2(p1,p2,p3): return(Squr(p1) * Func1(p2,p3)) print(Func2(2,3,2)) >>20 def Func3(p1,p2): return(dbl(Squr(p1))) print(Func3(4)) >>AACH CRASH BURN def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) >> 70 def Func6(p1): return(dbl(dbl(dbl(Squr(p1)+1))-Squr(3))) print(Func6(-2)) >>22 Given the function def Squr(par1): return(par1 ** 2) def dbl(par2): return(par2 + par2)

23 Piecewise functions

24 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

25 Piecewise functions x 3 + 2x if x > 2 f(x) = -x 3 + 2x if x < 0 -1 otherwise

26 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

27 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

28 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

29 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?

30 Example def f(x): if x != 10: return (x * 2) else: return (x ** 2) print(f(6)) print(f(10))

31 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)) ?


Download ppt "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,"

Similar presentations


Ads by Google