Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1

2 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 2016CISC101 - Prof. McLeod2

3 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. 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. McLeod3Winter 2016

4 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 2016CISC101 - Prof. McLeod4

5 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 2016CISC101 - Prof. McLeod5

6 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 2016CISC101 - Prof. McLeod6

7 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 2016CISC101 - Prof. McLeod7

8 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. CISC101 - Prof. McLeod8Winter 2016

9 CISC101 - Prof. McLeod9 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 2016

10 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'} CISC101 - Prof. McLeod10Winter 2016

11 CISC101 - Prof. McLeod11 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 2016

12 CISC101 - Prof. McLeod12 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 2016

13 CISC101 - Prof. McLeod13 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 2016

14 CISC101 - Prof. McLeod14 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 2016

15 CISC101 - Prof. McLeod15 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 2016

16 CISC101 - Prof. McLeod16 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 2016

17 CISC101 - Prof. McLeod17 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 2016

18 CISC101 - Prof. McLeod18 Operators Used with Lists Slice operator [ : ] + can be used to concatenate lists. * is used to generate multiples of lists. The slice operator can be used on either side of an assignment operator! For +, must have a list on both sides or a tuple on both sides, you cannot mix types. For *, must have an int after the *. Works with tuples or lists. Winter 2016

19 CISC101 - Prof. McLeod19 Slice Operator You can extract single elements or a set of elements from a collection using the “slice” operator: [ # ] or [ # : # ] Where # is an int number. Locations are numbered from zero. The slice operator without the “ : ” gets a single element. The other one can get a range of elements.

20 Winter 2016CISC101 - Prof. McLeod20 Slice Operator, Cont. When using [ : ], you can supply one or two numbers. If you omit the first number the slice starts at the start of the collection. If you omit the second number the slice ends at the end of the collection. The second number is one past the element you want.

21 Winter 2016CISC101 - Prof. McLeod21 Slice Operator, Cont. If the second number is too large, then the slice defaults to the end of the list. You can also number the elements backwards, where -1 is the last number in the list…

22 CISC101 - Prof. McLeod22 Slice Operator on a List, Examples >>> test = [2, 1, 3, -1, 4, 6] >>> test[3] >>> test[-1] 6 >>> test[4 :] [4, 6] >>> test[ : 3] [2, 1, 3] >>> test[1 : 3] [1, 3] Winter 2016

23 CISC101 - Prof. McLeod23 Slice Operator on a List, Examples, Cont. >>> test[1 : 3] = [10, 30] >>> test [2, 10, 30, -1, 4, 6] >>> test[-1] = 600 >>> test [2, 10, 30, -1, 4, 600] >>> test[1 : 3] = [10, 20, 30, 40, 50] >>> test [2, 10, 20, 30, 40, 50, -1, 4, 600] >>> test[2] = [-10, -5, -3] >>> test [2, 10, [-10, -5, -3], 30, 40, 50, -1, 4, 600] Winter 2016

24 CISC101 - Prof. McLeod24 Other Operators, Examples >>> test [2, 10, 30, -1, 4, 600] >>> testTwo = [5, 10, 15] >>> test + testTwo [2, 10, 30, -1, 4, 600, 5, 10, 15] >>> testTwo * 3 [5, 10, 15, 5, 10, 15, 5, 10, 15] Winter 2016


Download ppt "Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google