Download presentation
Presentation is loading. Please wait.
Published byJocelyn Gibbs Modified over 6 years ago
1
While loop Get names up to five times count=0 while count< 5:
name=input(‘Guess my name.\n’) if name==‘kim’: break count= count+1
2
For loop Convenient when the number of iterations is known count=0
while count< 5: name=input(‘Guess my name.\n’) if name==‘kim’: break count= count+1 for count in range(5): name=input(‘Guess my name.\n’) if name==‘kim’: break
3
FOR loop “for i in range(max):”
“i” is a dummy variable keeping counts in the range “i” starts at 0, increments by 1, and ends at (max-1) max=4: i=0, 1, 2, 3 Block of statements (instructions) to be repeats is indented “:” at the end of “for”
4
General FOR loop “for i in range(min, max, interval):” “i”
“i” = min min+interval min+interval+inteval …. “inteval” is omitted if interval==1
5
More about ‘for’ loop What will be printed x=0 for i in range(3):
x=x+1 print(x) for x in range(0,3): x=x+1 print(x) for i in range(-2,3): print(i) for count in range(5,-5,-2): print(count)
6
“for” loop p=min true false for p in range(min, max, interval): block
p >= max false Do block p = p + interval Out of “for”
7
Importing modules Modules are pre-programmed package including useful tools/resources math bioPython pyGame
8
turtle module Simple graphics programming How to use it ?
Import ‘turtle’ module by including import turtle Need to create a turtle, and NAME it to manipulate Say, you named a turtle ‘xxx.’ xxx = turtle.Turtle() or turtle.Pen() Opens up a turtle window Can have more than one turtles at a time – tell them apart by names
9
turtle module >>> import turtle
>>> myT = turtle.Turtle() What is “.” between turtle and Turtle() ? turtle => class (generic object referring to turtle module) Turtle() => method (action, function, constructor) Rough interpretation: get ‘turtle’ type (class) and do ‘Turtle()’ >>> myT.color(“red”) Get ‘myT’ turtle and color it red Attribute Position, heading (direction), color, tail position
10
Turtle in Python Actions References With myT
myT.forward(100), myT.backward(100) myT.right(90), myT.left(45) myT.goto(-200,90), myT.circle(50), myT.color(“red”) myT.up(), myT.down() myT.write(“Hello!”) References Official Python turtle page
11
Figure 1.9
12
Which methods to use ? Suppose ‘myT’ turtle is created
Draw a circle myT.circle(100) Draw a thicker circle – myT.width(5) Find out the location (x,y) of myT – myT.position() Move myT to (100,-50) – myT.goto(100,-50) Stop drawing – myT.up() Find out the screen sizes screen=myT.getscreen() screen.window_width() and screen.window_height()
13
HW 2: due 9/23 (Fri) Draw olympics rings
penup() Goto a location pendown() Draw a circle Take the screen shot which includes your python codes and the drawing to
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.