Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1

2 The Other Loop… The other kind of looping syntax uses the for keyword. It can be more powerful, particularly with collections – producing very compact code. But you could almost always use a while loop instead – it would just take more code to carry out the same operation. We’ll make more use of the for loop later in the course. CISC101 - Prof. McLeod2Winter 2016

3 CISC101 - Prof. McLeod3 The for Loop Syntax: for variable_name in iterable : You make up variable_name iterable can be a collection, such as a string, list, tuple, set or dict. It can also be an object produced by certain BIFs such as reversed() or range(). It can even be a file! Winter 2016

4 CISC101 - Prof. McLeod4 The for Loop, Cont. It is still interchangeable with a while loop. These two “snippets” act in the same way: testString = "I like watching videos!" i = 0 while i < len(testString) : print(testString[i]) i = i + 1 for aChar in testString : print(aChar) Winter 2016

5 Some New Syntax Two new things snuck their way into the code on the previous slide: len( ) BIF is used with collections and gives you the number of elements in the collection. For a string, it gives you the length of the string. The square brackets: [ ] is called the “slice operator” and returns a single element (or character) or even a range of elements or characters from a collection. CISC101 - Prof. McLeod5Winter 2016

6 The range() BIF This BIF generates an iterable and is often used with a for loop. Remember our while loop code to display 1 to 20?: i = 1 while i < 21 : print(i) i = i + 1 See RangeForLoopsDemo.py Winter 2016CISC101 - Prof. McLeod6

7 Aside - Slice Operator It is placed after a collection (a list, tuple, string, etc.) or a variable that is a collection type. Used as in: [ # ] or [ # : # ] The first way gives you the element at the location specified. The other way gives you all elements between the first location and one less than the last location. We will look at this operator again later. Winter 2016CISC101 - Prof. McLeod7

8 Turtle Graphics The “Turtle” is an easy way of drawing to the screen. One use is to illustrate how loops work. See section 24.1 in the Python Standard Library. To start you’ll need to import the turtle module: from turtle import * CISC101 - Prof. McLeod8Winter 2016

9 A Few turtle Commands To make the turtle look like a “turtle”: shape(“turtle”) To set the line thickness and line colour: pensize(5) pencolor(“green”) The turtle starts out at the “home” position, (0, 0), facing right or “east”. To draw, just issue commands to move the turtle. CISC101 - Prof. McLeod9Winter 2016

10 A Few More turtle Commands Turning: right(num_degrees)# clockwise left(num_degrees)# counter-clockwise Moving in the direction he is pointing; forward(length) Changing speed: speed(5)# 0 is no animation, 1 slow 10 fastest See TurtleDemo1.py CISC101 - Prof. McLeod10Winter 2016

11 Filling Commands Use begin_fill(), then draw, followed by end_fill() to fill a closed, drawn area. See TurtleDemo2.py See TurtleDemo3.py to see how to draw a filled circle. CISC101 - Prof. McLeod11Winter 2016

12 Regular Polygons Modify how circle() is invoked to draw regular polygons instead of circles. See TurtleDemo4.py Draw text, too! See TurtleDemo5.py Fun! CISC101 - Prof. McLeod12Winter 2016

13 Turtle Loop Demos The turtle can use loops to draw many similar figures one after the other without a lot of extra code. These demos will show loops, nested loops and the use of conditionals inside loops. CISC101 - Prof. McLeod13Winter 2016

14 while Loop Demo Draw 10 squares with turtle, one inside the other. See TurtleLoopDemo1.py In essence: size = 40 while size <= 400 : # drawing stuff size = size + 40 CISC101 - Prof. McLeod14Winter 2016

15 Nesting Loops, Example A loop is useful in preventing repetitious coding practice. Can you see any part of the previous example that is repetitive? How would you fix it? See TurtleLoopDemo2.py –Every time the outer loop iterates once, the inner loop iterates 3 times. CISC101 - Prof. McLeod15Winter 2016

16 Nesting Loops, Example Cont. See TurtleLoopDemo2Alt.py Uses goto(x, y) to move to a corner of the square. Uses setheading(angle) to make the turtle point South. Inner loop iterates four times now. More compact code. Winter 2016CISC101 - Prof. McLeod16

17 Turtle Squares Again Alternate red and blue squares. See TurtleLoopDemo3.py –Shows an if statement inside the loop. –Every time the outer loop iterates once, the if statement executes once. How can you change the code to have just the top side of the red square drawn in green? Draw the bottom side of the blue square in green? CISC101 - Prof. McLeod17Winter 2016


Download ppt "Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google