functions./15110/slides/week2-2-functions.pdf · functions can require as many (or as few)...

40

Upload: others

Post on 26-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 2: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

Page 3: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

pet1 = "Spot"pet2 = "Stella"pet3 = "Kimchee"

print("See " + pet1 + ". See " + pet1 + " run. Run, " + pet1 + ", run!")

print("See " + pet2 + ". See " + pet2 + " run. Run, " + pet2 + ", run!")

print("See " + pet3 + ". See " + pet1 + " run. Run, " + pet3 + ", run!")

Page 4: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 5: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

def tellStory(pet): print("See " + pet + ". See " + pet + " run. Run, " + pet + ", run!")

tellStory("Spot")tellStory("Stella")tellStory("Kimchee")

•••

Page 6: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 7: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

functionName(input1, input2, ...)

Page 8: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

print(type(int("4") + float(3)))

int("4") float(3) typeprint

Page 9: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

abs(-2) # absolute value

type(2) # the type of the value

str(2) # converts the value to a string

int(3.5) # converts the value to an integer

float(5) # converts the value to floating point

print("Hello World") # displays the value

print(1, 2, 3) # print can take any number of inputs

Page 10: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 11: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

pow(x, y)pow(2,3)

pow(3,2)

Page 12: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

pow(2,3) 8

Page 13: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

print()

Page 14: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

None

None

None TrueFalse

print()None

Page 15: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 16: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

int(0.07)

print("15", "-", "110")

Page 18: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

math.

import math

print(math.ceil(6.5)) # ceiling of a float number

print(math.log(64, 2)) # finds the log of 64 with base 2

print(math.radians(90)) # converts degrees to radians

print(math.pi)) # it's π!

Page 19: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 20: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

import tkinter

Page 21: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

import tkinter

root = tkinter.Tk()canvas = tkinter.Canvas(root, width=400, height=400)canvas.pack()

# your code goes here

root.mainloop()

rootcanvas

Page 22: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 23: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

canvas.create_rectangle

canvas.create_rectangle(10, 50, 110, 100)

Page 24: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 25: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 26: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 27: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

def helloWorld(): print("Hello World!") print("How are you?")

helloWorld()

def

helloWorld

Page 28: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

def hello(name): print("Hello, " + name + "!") print("How are you?")

hello("Scotty")hello("Dippy")

Page 29: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

None

def makeHello(name):

return "Hello, " + name + "! How are you?"

s = makeHello("Scotty")

Page 30: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

None

def drawDonut(canvas): # call drawDonut in graphics

# create_oval makes an oval in the given bounding box

canvas.create_oval(100, 100, 300, 300)

canvas.create_oval(180, 180, 220, 220)

Page 31: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

None

def singHappyBirthday(name):

print("Happy birthday to you")

print("Happy birthday to you")

print("Happy birthday dear " + name)

print("Happy birthday to you!")

singHappyBirthday("Dippy")

Page 32: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

def addTip(cost, percentToTip):

return cost + cost * percentToTip

total = addTip(20.00, 0.17)

Page 33: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

def distance(x1, y1, x2, y2): xPart = (x2 - x1)**2 yPart = (y2 - y1)**2 print("Partial Work:", xPart, yPart) return (xPart + yPart) ** 0.5

Page 34: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in
Page 35: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

def addItUp(x, y, z): answer = x + y answer = answer + z

print(answer) # NameError!

answeraddItUp

Page 36: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

x = 5

def test(): y = x + 2 return y

print(test() - x)

Page 37: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

x = "bar"

def test(): x = "foo" print("A", x)

test()print("B", x)

xx

Page 38: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

name = "Farnam"

def greet(day): punctuation = "!" print("Hello, " + name + punctuation) print("Today is " + day + punctuation)

def leave(): punctuation = "." print("Goodbye, " + name + punctuation)

greet("Wednesday")leave()

Page 39: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in

frog = "King Harold"

def greet1(): print("Hello", frog)

greet1()Hello King Harold

frog = "King Harold"

def greet2(): frog = "Kermit" print("Hello", frog)

greet2()Hello Kermit

frog = "King Harold"

def greet3(): print("Hello", frog) frog = "Kermit"

Error: local variable 'frog' referenced before assignment.

Page 40: Functions./15110/slides/week2-2-functions.pdf · Functions can require as many (or as few) arguments as needed. The positions of the arguments usually have meaning. In the built-in