Download presentation

Presentation is loading. Please wait.

Published byJahiem Joynt Modified over 2 years ago

1
**Order 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 % y Multiplication, division, floor division, modulo x + y, x - y Addition, subtraction Shall we try this? 5 + 4 / 2 + 1 7//3 -7//3 5*2**3 7%2 18%4 3 + 2**3 (3 + 2) ** 3 12/2 ** 2 Class 2

2
**Files Open a file In New Window: File->Save As Run the file:**

In IDLE, go to File->New Window In New Window: Type: 3 + 2 File->Save As Save the file as first.py 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. Class 1

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) = x3 + 1 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**3 + 1 -> output in the example, the integer 9 or 126 g(x) = x3 + 1 g(2) = 9 g(5) = 126

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

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) = x3 + 1 g(2) = 9 g(5) = 126

7
**Input Values: Parameters**

values into the function are known as parameters 3, addfunc 7, addfunc 11 9, addfunc 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?**

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*y func(2,7) = 14 func(5,4) = 20

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

10
**Function name? Input? Output? func(x) = (x-32)/1.8 Calculations?**

f_to_c Input? An integer (the fahrenheit temperature) Output? A float (the celsius temperature) Calculations? (ftemp – 32) / 1.8 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) = Class 3 stopped here

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

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 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 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 Class 3 stopped after first example on Wed

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) Class 2 stopped (Wed)

22
**Given the function def Squr(par1): return(par1 ** 2) def dbl(par2):**

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) Class 2 stopped (Wed)

23
Piecewise functions 32 if x > 0 𝑥 f(x) = 0 otherwise

24
**If /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 > 0 f(x) = x _ 0 otherwise

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

26
**If /else (branching) def f(x): if x > 2: return (x ** 3 + 2 * x)**

x3 + 2x if x > 2 f(x) = x3 + 2x if x < 0 otherwise 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?

27
**Comparators (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 to Class 3 stopped here

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)) ? Class 2 stopped here

Similar presentations

OK

CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.

CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on postal life insurance Ppt on acid base indicators Ppt on panel discussion images Ppt only output devices Free ppt on bullying Ppt on 60 years of indian parliamentary Ppt on electricity distribution system in india Ppt on power line communication scanner Ppt on articles of association definition Ppt on real numbers for class 9th maths