30
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? 5 + 4 / 2 + 1 7//3 -7//3 5*2**3 7%2 18%4 3 + 2**3 (3 + 2) ** 3 12/2 ** 2

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,

Embed Size (px)

Citation preview

Page 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,

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?

• 5 + 4 / 2 + 1• 7//3• -7//3• 5*2**3• 7%2• 18%4• 3 + 2**3

• (3 + 2) ** 3

• 12/2 ** 2

Page 2: 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,

Files1. Open a file

• In IDLE, go to File->New Window

2. In New Window:• Type:3 + 2

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

Page 3: 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,

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) = 6f(5) = 10

g(x) = x3 + 1g(2) = 9g(5) = 126

Page 4: 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,

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-> outputin the example, the integer 9 or 126

g(x) = x3 + 1g(2) = 9g(5) = 126

Page 5: 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,

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

g(x) = x3 + 1g(2) = 9g(5) = 126

Page 6: 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,

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

Page 7: 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,

Input Values: Parameters values into the function are known as parameters

3,2 addfunc 57,4 addfunc 119,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.

Page 8: 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,

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

Page 9: 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,

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?

Page 10: 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,

1. Function name? • f_to_c

2. Input? • An integer (the fahrenheit temperature)

3. Output?• A float (the celsius temperature)

4. Calculations?• (ftemp – 32) / 1.8

5. Test Cases?• f_to_c(68) -> 20.0• f_to_c(22)->-5.5556

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

Page 11: 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,

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

Page 12: 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,

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.

Page 13: 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,

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?

Page 14: 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,

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

Page 15: 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,

Using functions:def add2(x,y):

return(x + y)

def add(x,y): return(add2(x,y) + add2(x,y))

print(add(7,3))

Page 16: 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,

Using functions:def add2(x,y):

return(x + y)

def div(x,y,z): return(add2(x,y) / z)

print(div(7,3,2))

Page 17: 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,

Using functions:

def add2(x,y): return(x + y)

def div(x,z): return(add2(x,3) / z)

print(div(7,2))

Page 18: 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,

Using functions:def add2(x,y):

return(x + y)

def div(y,x): return(add2(y,3) / x)

print(div(7,2))

Page 19: 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,

Using functions:def add2(x,y):

return(x + y)

def add3(y,x): return(add2(y,3) + add2(11,x))

print(add3(7,2))

Page 20: 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,

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

Page 21: 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,

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 functiondef Squr(par1): return(par1 ** 2)

def dbl(par2):return(par2 + par2)

Page 22: 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,

Piecewise functions

32 if x > 0

f(x) =

0 otherwise

Page 23: 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,

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?

32 if x > 0_f(x) = x

0 otherwise

Page 24: 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,

Piecewise functions

x3 + 2x if x > 2

f(x) = -x3 + 2x if x < 0

-1 otherwise

Page 25: 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,

If /else (branching)

def f(x):if x > 2:

return (x ** 3 + 2 * x)elif x < 0:

return(-x ** 3 + 2 * x)else:

return (-1)

f(3) # this equals?f(0) # this equals?f(-2) # this equals?

x3 + 2x if x > 2f(x) = -x3 + 2x if x < 0

-1 otherwise

Page 26: 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,

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

Page 27: 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,

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

Page 28: 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,

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

Page 29: 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,

Example

def f(x):if x != 10:

return (x * 2)else:

return (x ** 2) print(f(6)) print(f(10))

Page 30: 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,

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