Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

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?
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
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.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
I Power Int 2 Computing Software Development High Level Language Constructs.
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.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lists CS303E: Elements of Computers and Programming.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
Python Programing: An Introduction to Computer Science
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Copyright 2011 CreativeChemistryLessons.comCreativeChemistryLessons.com O Oxygen Atomic # 8 Atomic Mass 16 8 Protons 8 Neutrons 8 Electrons O
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.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
Python Aliasing Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Aliasing
Sequences and Indexing
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
From Think Python How to Think Like a Computer Scientist
Engineering Computing
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists in Python.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
String and Lists Dr. José M. Reyes Álamo.
Ruth Anderson UW CSE 160 Winter 2017
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CS1110 Today: collections.
CISC101 Reminders Assignment 2 due today.
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Ruth Anderson UW CSE 160 Spring 2018
Sample lecture slides.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Python

Lists Loops let us do things many times

PythonLists Loops let us do things many times Collections let us store many values together

PythonLists Loops let us do things many times Collections let us store many values together Most popular collection is a list

PythonLists Create using [value, value,...]

PythonLists Create using [value, value,...] Get/set values using var[index]

PythonLists Create using [value, value,...] Get/set values using var[index] gases = ['He', 'Ne', 'Ar', 'Kr'] print gases ['He', 'Ne', 'Ar', 'Kr']

PythonLists Create using [value, value,...] Get/set values using var[index] gases = ['He', 'Ne', 'Ar', 'Kr'] print gases ['He', 'Ne', 'Ar', 'Kr'] print gases[1] Ne

PythonLists Index from 0, not 1

PythonLists Index from 0, not 1 Reasons made sense for C in

PythonLists Index from 0, not 1 Reasons made sense for C in It's an error to try to access out of range

PythonLists Index from 0, not 1 Reasons made sense for C in It's an error to try to access out of range gases = ['He', 'Ne', 'Ar', 'Kr'] print gases[4] IndexError: list index out of range

PythonLists Use len(list) to get length of list

PythonLists Use len(list) to get length of list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4

PythonLists Use len(list) to get length of list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 Returns 0 for the empty list etheric = [] print len(etheric) 0

PythonLists Some negative indices work

PythonLists Some negative indices work values[-1] is last element, values[-2] next-to-last,...

PythonLists Some negative indices work values[-1] is last element, values[-2] next-to-last,... gases = ['He', 'Ne', 'Ar', 'Kr']

PythonLists Some negative indices work values[-1] is last element, values[-2] next-to-last,... gases = ['He', 'Ne', 'Ar', 'Kr'] print gases[-1], gases[-4] Kr He

PythonLists Some negative indices work values[-1] is last element, values[-2] next-to-last,... gases = ['He', 'Ne', 'Ar', 'Kr'] print gases[-1], gases[-4] Kr He values[-1] is much nicer than values[len(values)-1]

PythonLists Some negative indices work values[-1] is last element, values[-2] next-to-last,... gases = ['He', 'Ne', 'Ar', 'Kr'] print gases[-1], gases[-4] Kr He less error prone values[-1] is much nicer than values[len(values)-1]

PythonLists Mutable : can change it after it is created

PythonLists gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled Mutable : can change it after it is created

PythonLists gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' Mutable : can change it after it is created

PythonLists gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' print gases ['He', 'Ne', 'Ar', 'Kr'] Mutable : can change it after it is created

PythonLists gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' print gases ['He', 'Ne', 'Ar', 'Kr'] Location must exist before assignment Mutable : can change it after it is created

PythonLists gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' print gases ['He', 'Ne', 'Ar', 'Kr'] Location must exist before assignment gases = ['He', 'Ne', 'Ar', 'Kr'] Mutable : can change it after it is created

PythonLists gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' print gases ['He', 'Ne', 'Ar', 'Kr'] Location must exist before assignment gases = ['He', 'Ne', 'Ar', 'Kr'] gases[4] = 'Xe' IndexError: list assignment index out of range Mutable : can change it after it is created

PythonLists Heterogeneous : can store values of many kinds

PythonLists Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8]

PythonLists Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] [string, int]

PythonLists Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] 'He' 2 'Ne' 8 helium neon

PythonLists Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] gases = [helium, neon]

PythonLists Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] gases = [helium, neon] 'He' 2 'Ne' 8 helium neon gases

PythonLists Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] gases = [helium, neon] 'He' 2 'Ne' 8 helium neon gases Devote a whole episode to this

PythonLists Loop over elements to "do all"

PythonLists Loop over elements to "do all" Use while to step through all possible indices

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1 First legal index

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1 Next index

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1 Defines set of legal indices

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1 He Ne Ar Kr

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1 He Ne Ar Kr Tedious to type in over and over again

PythonLists Loop over elements to "do all" Use while to step through all possible indices gases = ['He', 'Ne', 'Ar', 'Kr'] i = 0 while i < len(gases): print gases[i] i += 1 He Ne Ar Kr Tedious to type in over and over again And it's easy to forget the "+= 1" at the end

PythonLists Use a for loop to access each value in turn

PythonLists Use a for loop to access each value in turn gases = ['He', 'Ne', 'Ar', 'Kr'] for gas in gases: print gas He Ne Ar Kr

PythonLists Use a for loop to access each value in turn gases = ['He', 'Ne', 'Ar', 'Kr'] for gas in gases: print gas He Ne Ar Kr Loop variable assigned each value in turn

PythonLists Use a for loop to access each value in turn gases = ['He', 'Ne', 'Ar', 'Kr'] for gas in gases: print gas He Ne Ar Kr Loop variable assigned each value in turn Not each index

PythonLists Use a for loop to access each value in turn gases = ['He', 'Ne', 'Ar', 'Kr'] for gas in gases: print gas He Ne Ar Kr Loop variable assigned each value in turn Not each index Because that's the most common case

PythonLists Can delete entries entirely (shortens the list)

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] Can delete entries entirely (shortens the list)

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] Can delete entries entirely (shortens the list)

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] Can delete entries entirely (shortens the list)

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] del gases[2] Can delete entries entirely (shortens the list)

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] del gases[2] print gases ['Ne', 'Ar'] Can delete entries entirely (shortens the list)

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] del gases[2] print gases ['Ne', 'Ar'] Yes, deleting an index that doesn't exist is an error Can delete entries entirely (shortens the list)

PythonLists Appending values to a list lengthens it

PythonLists Appending values to a list lengthens it gases = []

PythonLists Appending values to a list lengthens it gases = [] gases.append('He')

PythonLists Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne')

PythonLists Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar')

PythonLists Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar') print gases ['He', 'Ne', 'Ar']

PythonLists Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar') print gases ['He', 'Ne', 'Ar'] Most operations on lists are methods

PythonLists Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar') print gases ['He', 'Ne', 'Ar'] Most operations on lists are methods A function that belongs to (and usually operates on) specific data

PythonLists Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar') print gases ['He', 'Ne', 'Ar'] Most operations on lists are methods A function that belongs to (and usually operates on) specific data thing. method (args)

PythonLists Some useful list methods

PythonLists Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated

PythonLists Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated print gases.count('He') 2

PythonLists Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated print gases.count('He') 2 print gases.index('Ar') 2

PythonLists Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated print gases.count('He') 2 print gases.index('Ar') 2 gases.insert(1, 'Ne')

PythonLists Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated print gases.count('He') 2 print gases.index('Ar') 2 gases.insert(1, 'Ne') print gases ['He', 'Ne', 'He', 'Ar', 'Kr']

PythonLists Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] print gases.reverse() None Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] print gases.reverse() None print gases ['Ne', 'Kr', 'He', 'Ar'] Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] print gases.reverse() None print gases ['Ne', 'Kr', 'He', 'Ar'] A common bug Two that are often used incorrectly

PythonLists gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] print gases.reverse() None print gases ['Ne', 'Kr', 'He', 'Ar'] A common bug gases = gases.sort() assigns None to gases Two that are often used incorrectly

PythonLists Use in to test for membership

PythonLists Use in to test for membership gases = ['He', 'Ne', 'Ar', 'Kr']

PythonLists Use in to test for membership gases = ['He', 'Ne', 'Ar', 'Kr'] print 'He' in gases True

PythonLists Use in to test for membership gases = ['He', 'Ne', 'Ar', 'Kr'] print 'He' in gases True if 'Pu' in gases: print 'But plutonium is not a gas!' else: print 'The universe is well ordered.'

PythonLists Use in to test for membership gases = ['He', 'Ne', 'Ar', 'Kr'] print 'He' in gases True if 'Pu' in gases: print 'But plutonium is not a gas!' else: print 'The universe is well ordered.' The universe is well ordered.

PythonLists Use range to construct lists of numbers

PythonLists Use range to construct lists of numbers print range(5) [0, 1, 2, 3, 4]

PythonLists Use range to construct lists of numbers print range(5) [0, 1, 2, 3, 4] print range(2, 6) [2, 3, 4, 5]

PythonLists Use range to construct lists of numbers print range(5) [0, 1, 2, 3, 4] print range(2, 6) [2, 3, 4, 5] print range(0, 10, 3) [0, 3, 6, 9]

PythonLists Use range to construct lists of numbers print range(5) [0, 1, 2, 3, 4] print range(2, 6) [2, 3, 4, 5] print range(0, 10, 3) [0, 3, 6, 9] print range(10, 0) []

PythonLists So range(len(list)) is all indices for the list

PythonLists So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr']

PythonLists So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4

PythonLists So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 print range(len(gases)) [0, 1, 2, 3]

PythonLists So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 print range(len(gases)) [0, 1, 2, 3] for i in range(len(gases)): print i, gases[i]

PythonLists So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 print range(len(gases)) [0, 1, 2, 3] for i in range(len(gases)): print i, gases[i] 0 He 1 Ne 2 Ar 3 Kr

PythonLists So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 print range(len(gases)) [0, 1, 2, 3] for i in range(len(gases)): print i, gases[i] 0 He 1 Ne 2 Ar 3 Kr A very common idiom in Python

October 2010 narrated by Dominique Vuvan Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.