Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
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.
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.
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
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.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Files and Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1 >>> while b < 10:... print b... a, b = b, a+b WHILE.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
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.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Tuples Chapter 10 Python for Informatics: Exploring Information
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Compsci 101.2, Fall Plan for FWON l Review current assignments and APTs  Review Dictionaries and how to use them  Code and APT walk-through.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Python Programing: An Introduction to Computer Science
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
Python - Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Section 6: Sequences Chapter 4 and 5.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Bryan Burlingame 03 October 2018
CS190/295 Programming in Python for Life Sciences: Lecture 6
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Reference semantics, variables and names
Topics Basic String Operations String Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Data Structures & Algorithms
And now for something completely different . . .
Python Review
Topics Basic String Operations String Slicing
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009

Status Today: Writing functions that work with lists Current workshop: Merge Sort Tuesday: Assignment 7: Literary Analysis Get new partners in lab 11 days to complete Wednesday: another kind of data collection: dictionaries 2

Data Types in Python Simple data types: Integers Floating point numbers Strings Objects Combine data with methods We used in graphics assignment Lists Sequence of data elements Elements can be of any type (simple, objects, lists) Called "arrays" in some language The length and contents can vary dynamically 3

Creating Lists Writing lists explicitly: >>> [1, 2, 3] >>> ['Henry', 'Kautz', 708] Creating a list using variables: >>> FirstName = 'Henry' >>> LastName = 'Kautz' >>> Room = 708 >>> Data = [FirstName, LastName, Room] Data ['Henry', 'Kautz', 708] >>> FirstName = 'Sam' >>> Data ['Henry', 'Kautz', 708] 4 The list does not contain the variables, it contains the values the variables have at that moment!

Creating Lists using Functions & Operations range(start, end, increment): list of integers >>> range(2,12,2) [2, 4, 6, 8, 10] Concatenation +: creates a new list >>> X = ['dog', 'cat'] >>> Y = ['mouse', 'dog'] >>> Z = X + Y >>> Z ['dog', 'cat', 'mouse', 'dog'] >>> X ['dog', 'cat'] 5

Accessing Elements of Lists Indexing >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> Z[0] 'dog' Slicing >>> Z[1:3] ['cat', 'mouse'] 6

Changing Contents of Lists Lists in Python are "mutable": you can change elements inside a list without creating a new one >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> Z[2] = 'horse' >>> Z ['dog', 'cat', 'horse', 'dog'] Strings are not mutable! >>> S = 'Henry' >>> S[0] 'H' >>> S[0] = 'B' ERROR! 7

Lists and Aliasing Two variables might refer to the same list This is an example of aliasing >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = Z >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'horse', 'dog'] Why does this happen? Because an assignment statement puts a pointer to the list into the variable 8

Aliasing Example >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = Z >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'horse', 'dog'] 9 'mouse''dog''cat''dog' Z W 'horse'

Avoiding Aliasing If you are not modifying a list, don't worry about aliasing You can use the list( ) function to create a new copy of a list >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = list(Z) >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'mouse', 'dog'] 10

Searching Lists There is a built-in operation for searching a list for an element in is True if the item is in the list >>> 'cat' in ['dog', 'cat', 'mouse', 'dog'] True >>> 'horse' in ['dog', 'cat', 'mouse', 'dog'] False 11

Looping Through a List Combining for with in lets up loop through a list >>> Z = ['dogs', 'cats', 'mice', 'snails'] >>>for A in Z: >>> print 'I like ' + A I like dogs I like cats I like mice I like snails 12

Looping Through a Slice You can loop through part of a list using slicing: >>> Z = ['dogs', 'cats', 'mice', 'snails'] >>> S = 'I like ' + Z[0] >>> for A in Z[1:] >>> S = S + ' and ' + A >>> print S I like dogs and cats and mice and snails 13

List Methods Surprise: Lists are actually a special kind of object There are built-in methods for lists These methods modify their list Here are some useful ones (see more on page 343) >>> X = ['dog', 'ant', 'cat'] >>> X.sort() >>> X ['ant', 'cat', 'dog'] >>> X.reverse() >>> X ['dog', 'cat', 'ant'] 14

Writing Functions on Lists When you write your own function that works on a list, you can choose to use iteration or recursion Example: adding up a list of numbers iteratively def AddUp(L): R = 0 for N in L: R = R + N return R 15

Recursive Algorithm Example: adding up a list of numbers recursively: def AddUp(L): if len(L) == 0: return 0 return L[0] + AddUp(L[1:]) Why does this work? The empty list is the base case The slice L[1:] is everything after the first element in the list 16

Ways to Recurse Through Lists Recursing through a list one element at a time (e.g. slicing away the first element) is just like iteration There are other ways you might want to process a list For example: split the list in halves Recursively process the first half Recursively process the second half You'll do this (or have done this) in workshop this week 17

Next Class Workshop solution One more data collection: Dictionaries How to implement word counting We give you this function for use in your next programming assignment 18