ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology
Course A201: Introduction to Programming 10/28/2010.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Container Types in Python
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
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.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Q and A for Sections 2.3 – 2.5 CS 106 Professor Victor T. Norman, DDS.
Lists in Python.
Lists Victor Norman CS104. Reading Quiz Q1: What is printed by the following statements? alist = [3, 67, “cat”, [56, 57, “dog”], [ ], 3.14, False] print(3.14.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Let’s get started!.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
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.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
© 2016 Pearson Education, Ltd. All rights reserved.
Containers and Lists CIS 40 – Introduction to Programming in Python
CS-104 Final Exam Review Victor Norman.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
CSC 108H: Introduction to Computer Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Topics Introduction to File Input and Output
Bryan Burlingame 03 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 6.
For loop Using lists.
Topics Introduction to File Input and Output
Introduction to Computer Science
Presentation transcript:

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman

Creating a list Q: Write code to create a variable cheeses referring to a list containing these strings: cheddar, edam, limberger. A: cheeses = [cheddar, edam, limberger] Q: Must a list contain items all be of the same type? A: No. For lo!, you can do this: weird = [1, hi, None, True]

Nested Lists Q: If we nest a list within another list, is there a way to access an element from the nested list? bigMess = [ 1, 3, 5, [ 7, 9 ], [ 44, 22, 11 ] ] How would we print the last item of the first embedded list? A: print bigMess[3][1]

Adding off the end of a list Q: Is it possible to assign an element to a list where the index is much greater than the index of the last element in the list? For example, numbers = [56, 112, 67] numbers[7] = 234 If you can do this, what would the elements of the list between indices 3 and 6 be? A: You cannot do this. You can use append() to add to the end of a list, but you cant skip indices.

Lists are mutable Q: Can you please go over examples that show how lists are mutable? A: OK… You can make a list of floats 1.0, 1.1, 1.2, 1.3, …, 9.9 this way vals = range(10, 100) # integers 10, 11, 12, … 99 for i in range(len(vals)): vals[i] = vals[i] / 10.0

A list is a mapping Q: I dont understand this mapping stuff. A: A list is a sequence, which means elements of the list are ordered, indexed from 0 up to n – 1. Indices are integers. Therefore, the list contains a mapping from integers 0..n-1 to elements of the list.

Mutating a list: lost data? Q: In section 10.2, I dont understand why numbers[1] = 5 ? Where did that come from? Where does the 123 go?? A: From the book: numbers = [17, 123] numbers[1] = 5 Now, numbers holds: [17, 5]

Differences between lists and strings Q: Apart from strings not being mutable, are there any other distinct differences between strings and lists? At least in terms of how they can be manipulated. A: 1) Strings only hold characters. Lists hold anything. 2) Because strings are immutable there are far fewer things you can do to a string than a list. You cant alter a string (append to it, delete from it, change elements in it, sort it, etc.)

Traversing a list Q: In section 10.3 I get confused when it shows the for loop and says print 'This never happens' so, that is a syntax error then, to have an empty list in a for loop? A: Remember the for loop pattern: for in : So, if the sequence is empty, the body isnt executed. No syntax error.

range(len(somelist)) Q: I didn't understand the use of range(len(somelist)). A: range() can be used for many things, but it is best for creating a list of indices: for i in range(len(numbers)): numbers[i] = numbers[i] * 2 numbers is a list. len(numbers) is an integer, say, n. range(n) produces a list from 0 to n – 1, which is exactly what we need for indices to traverse the list! Lo!

Index-based vs. element-based traversal A sequence can be traversed either way. index-based means using indexing with indices to access elements: – for i in range(len(aList)): print aList[i] element-based means accessing each element directly: – for elem in aList: print elem Use element-based when you dont need to know where the element is in the list. index-based otherwise.

10.7 Summary reduce: analyze a sequence to produce one value. E.g., sum or average of numbers, or concatenation of all strings, or max of all numbers. map: make a new list from elements of an existing list. E.g., capitalize all strings, create list of floats from list of ints, create list of userIds from list of ints… filter: create new list from some elements of a list. E.g., find all prime numbers in a list, find all 7-letter words in a list, etc. Q from student: Do we have to know this stuff? A from me: Yes.

Lists and strings Q: For this class, can we assume it is always true that a method performed on a list modifies the original argument and returns None, while that method performed on a string returns a new string that can be used in the future? (The book says that's true for "most list methods", not all.) A: All string methods that modify a string return a new string. All list methods that modify a list return None, except pop() which returns the removed element.

split() Q: What does split() do? E.g., soliloquy = To be, or not 2 b. That is the ? words = soliloquy.split() A: words is a list of strings, created by removing whitespace in soliloquy: >>> print words [ To, be,, or, not, 2, b., That, is, the, ? ] Very useful also for processing data files.

Objects and values Q: Can you go over the difference between objects and values, and the distinctions we need to know between the two? A: An object is really a location in memory, and a value is what is stored in that location. Thats why they say a list is an object that holds a sequence of values. And, why two objects can be different but hold the same values. Soon, well create our own types and make objects that can store multiple values in them.

Aliasing Q: Could you give a better description of what aliasing is? A: Sure! Aliasing is when two identifiers point to the same object (the same location in memory). Python does aliasing automatically when multiple variables are created that refer to the same immutable object. This saves memory. Python does not do aliasing for mutable objects unless you use assignment statement.

Note about equivalent and identical I dont care if you understand that stuff.

Is aliasing useful? Yes, very useful. Suppose you are modeling a cave system with rooms connected by hallways. If you leave something in one room and walk around a different way back to that room, you should still find that item. A room object in memory could keep track of that item. No matter how you get to that room object (via different hallway objects), it will be that location in memory holding that item.