Download presentation
Presentation is loading. Please wait.
1
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/12/2018 CISC101 Reminders Quiz 2 this week. Next Assignment (Assn 2) due Friday, the 16th. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Intro to the turtle. More loop examples using the turtle.
Start Collections (if we have time). Winter 2018 CISC101 - Prof. McLeod
3
Turtle Graphics The “Turtle” is an easy way of drawing to the screen.
CISC101 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 * Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
4
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. Winter 2018 CISC101 - Prof. McLeod
5
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 Winter 2018 CISC101 - Prof. McLeod
6
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. Winter 2018 CISC101 - Prof. McLeod
7
Regular Polygons Modify how circle() is invoked to draw regular polygons instead of circles. See TurtleDemo4.py Draw text, too! See TurtleDemo5.py Fun! Winter 2018 CISC101 - Prof. McLeod
8
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. Winter 2018 CISC101 - Prof. McLeod
9
while Loop Demo Draw 10 squares with turtle, one inside the other.
CISC101 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 Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
10
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. Winter 2018 CISC101 - Prof. McLeod
11
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. Convert to a for loop? Winter 2018 CISC101 - Prof. McLeod
12
Turtle Squares Again Alternate red and blue squares.
See TurtleLoopDemo3.py Uses for loops. Shows an if statement inside the loop. Every time the outer loop iterates once, the if statement executes once. Winter 2018 CISC101 - Prof. McLeod
13
Random Walk See “Random Walk” in Wikipedia.
Think of a drunk turtle staggering through a regular grid of city streets. Used to model any sort of random population movement. Brownian Motion, for example. See RandomWalk.py Advanced features include the use of a tuple to store possible direction choices. Winter 2018 CISC101 - Prof. McLeod
14
Spiral of Theodorus Supposedly invented by the ancient Greek philosopher Theodorus as a geometric means of estimating square roots. It consists of a set of contiguous right angle triangles, drawn as shown below: Winter 2018 CISC101 - Prof. McLeod
15
Spiral of Theodorus, Cont.
See FilledTheodorusSpiral.py Draws the spiral and fills the triangles with a gradation of colour. Uses a tkinter dialog to allow the user to choose colours for the start and end of the gradation. More tkinter towards the end of this course! Winter 2018 CISC101 - Prof. McLeod
16
Collections Allows you to access a whole bunch of “things”, one at a time or in groups through a single variable name. Collections can be huge – the limit is the amount of available RAM. A typical scenario: Read data from a file. Carry out whatever analysis you are interested in, accessing all the data in RAM. Save the results in another collection in RAM. Save the results collection to another file. Winter 2018 CISC101 - Prof. McLeod
17
Python’s Collections First an overview of the popular collection types, then: Focus on what is used with lists (and to some extent with other collections): Operators Keywords BIFs Methods A list is the most versatile collection type. Winter 2018 CISC101 - Prof. McLeod
18
Built-in Collection Types
Python has: Lists Tuples Sets Dictionaries (Strings are really just a kind of Tuple…) See Chapter 5 in the Python Tutorial and Section 4.6 in the Python Library Reference. Winter 2018 CISC101 - Prof. McLeod
19
Examples of Collection Literals
CISC101 Examples of Collection Literals [2, 3, 4, 6.78, 'abcd', -10] (4, 5, 'hi', 6.6) {4, 5, 7, 9, 11} 'hello class!' {'first':'Alan', 'age':25, 'last':'McLeod'} Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
20
Lists vs. Tuples A list literal is a set of items enclosed in [ ]
A tuple literal is a set of items enclosed in ( ) Items are separated by commas. You can change the items within a list and its length at any time after you have created it. A list is mutable. Winter 2018 CISC101 - Prof. McLeod
21
Lists vs. Tuples, Cont. You cannot change the items in a tuple or change its length after you have created it - it is “immutable” (like “read-only”). (Numbers and strings are also immutable. You cannot mess with the individual digits of a number or the individual characters of a string after you have created them. You can only re-assign variables that are numeric or string types.) Winter 2018 CISC101 - Prof. McLeod
22
aDict = {'first' : 'Alan', 'last' : 'McLeod'}
Dictionaries “Dicts” are enclosed in { }. They consist of key : value associations. For example: aDict = {'first' : 'Alan', 'last' : 'McLeod'} We will look at these more closely later… Winter 2018 CISC101 - Prof. McLeod
23
Sets Are new to Python 3. Items enclosed in { }.
Each item must be unique. If you try to create a set with duplicate items, the duplicates will be discarded. We will look at these more closely later too… Winter 2018 CISC101 - Prof. McLeod
24
Lists They can hold items of all the same type: [3, 2, -1, 10]
Or can hold a mixture of types: [4.2, ‘7abc’, 3, aVar] Yes, they can hold variables as well as literals! Winter 2018 CISC101 - Prof. McLeod
25
atuple = (4, 3.2, ‘abc’, 7, -3, ‘ding’)
Tuples Can be a mixture of types, just like lists: atuple = (4, 3.2, ‘abc’, 7, -3, ‘ding’) Since a tuple is immutable, you cannot do something like: atuple[1] = 7 Use code like (‘abc’,) to create a single element tuple. Why the comma? Winter 2018 CISC101 - Prof. McLeod
26
Empty Lists You can create an empty list as in: mtList = []
Probably, you will be using append() or the + operator with variables like these… You can create an empty tuple as in: mtTuple = () You can use + to add tuples, but not append(). Winter 2018 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.