Download presentation
Presentation is loading. Please wait.
Published byOwen McLaughlin Modified over 8 years ago
1
Function Abstraction Black Box Container for a sequence of actions Call, execute, invoke the function by name Good for abstraction of key operations Good for useful functions, such as summation, etc.
2
Olympic Rings import turtle myT = turtle.Turtle() myT.color("blue”) myT.goto(-110,-25) myT.circle(50) a = turtle.Turtle() a.color ("black”) a.goto(0,-25) a.circle(50) b = turtle.Turtle() b.color ("red”) b.goto(110,-25) b.circle(50) ….. import turtle aaa=turtle.Turtle() aaa.width(5) aaa.color("blue”) aaa.goto(0,0) aaa.circle(50) aaa.penup() aaa.color("yellow”) aaa.goto(45,-45) aaa.pendown() aaa.circle(50) aaa.penup() ……
3
Avg computation myList = [20,30,40,5,55] # get the sum of the list total = 0 numStu = len(myList) for n in range(numStu): total = total + myList[n) avg = total/numStu print(avg)
4
Avg computation def sum( ): myList = [20,30,40,5,55] avg = sum(myList)/numStu print(avg) myList = [20,30,40,5,55] # get the sum of the list total = 0 numStu = len(myList) for n in range(numStu): total = total + myList[n) avg = total/numStu print(avg)
5
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2 So, how do we marry Ben and Jo ?
6
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2
7
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2
8
Values A number or a string Python has a set of recognized value types Integer (int) Real (float) String (str) Try in command window type(‘c’) type(‘3.2’) Operators Int, float : arithmetic operations +, -, *, /, **, % String: concatenation (+)
9
Name a value and re-use it >>> x = 2*8 >>> x/2 >>> x Whenever a variable name appears, it is substituted by its value A variable name can be reused, redefined Variables
10
MUST start with a letter followed by alphanumeric aVar, cs100, … CASE matters ‘Print’ is not the same as ‘print’ ‘makePicture’ is not the same as ‘makepicture’, nor as ‘Makepicture’ Multi-word name First letter is capitalized Convention, not absolute rule Use sensible, meaningful name Naming Convention for both Variables and Functions
11
Variable vs. Function Variables have fixed values until reassigned Functions can have different values each time it is called Function has () for parameters
12
ViralSpiral.py (p.72) import turtle t=turtle.Pen() colors = ["red", "green", "purple", "blue"] sides = 4 for m in range(50): t.forward(4*m) position=t.position() direction = t.heading() for n in range(int(m/2)): t.pendown() t.pencolor(colors[n%sides]) t.forward(2*n) t.right(360/sides - 2) t.penup() t.setx(position[0]) t.sety(position[1]) t.setheading(direction) t.left(360/sides + 2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.