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

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
String and Lists Dr. José M. Reyes Álamo.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
© 2016 Pearson Education, Ltd. All rights reserved.
Department of Computer Science,
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Bryan Burlingame 03 October 2018
CHAPTER THREE Sequences.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
4. sequence data type Rocky K. C. Chang 16 September 2018
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 Labs start this week in JEFF 155.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
Python Primer 1: Types and Operators
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assn 3 sample solution is posted.
JavaScript: Arrays.
For loops Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CISC101 Reminders Assignment 2 due this Friday.
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
For loop Using lists.
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Class code for pythonroom.com cchsp2cs
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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.

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…

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

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

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