SlideShare une entreprise Scribd logo
1  sur  97
Télécharger pour lire hors ligne
Python 
Lists 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.
Loops let us do things many times 
Python Lists
Loops let us do things many times 
Collections let us store many values together 
Python Lists
Loops let us do things many times 
Collections let us store many values together 
Most popular collection is a list 
Python Lists
Create using [value, value, ...] 
Python Lists
Create using [value, value, ...] 
Get/set values using var[index] 
Python Lists
Create using [value, value, ...] 
Get/set values using var[index] 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print gases 
['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
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 
Python Lists
Index from 0, not 1 
Python Lists
Index from 0, not 1 
Reasons made sense for C in 1970... 
Python Lists
Index from 0, not 1 
Reasons made sense for C in 1970... 
It's an error to try to access out of range 
Python Lists
Index from 0, not 1 
Reasons made sense for C in 1970... 
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 
Python Lists
Use len(list) to get length of list 
Python Lists
Use len(list) to get length of list 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print len(gases) 
4 
Python Lists
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 
Python Lists
Some negative indices work 
Python Lists
Some negative indices work 
values[-1] is last element, values[-2] next-to-last, ... 
Python Lists
Some negative indices work 
values[-1] is last element, values[-2] next-to-last, ... 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
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 
Python Lists
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)-Python Lists
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)-less error prone 
Python Lists
Mutable : can change it after it is created 
Python Lists
Mutable : can change it after it is created 
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled 
Python Lists
Mutable : can change it after it is created 
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled 
gases[3] = 'Kr' 
Python Lists
Mutable : can change it after it is created 
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled 
gases[3] = 'Kr' 
print gases 
['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
Mutable : can change it after it is created 
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled 
gases[3] = 'Kr' 
print gases 
['He', 'Ne', 'Ar', 'Kr'] 
Location must exist before assignment 
Python Lists
Mutable : can change it after it is created 
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'] 
Python Lists
Mutable : can change it after it is created 
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 
Python Lists
Heterogeneous : can store values of many kinds 
Python Lists
Heterogeneous : can store values of many kinds 
helium = ['He', 2] 
neon = ['Ne', 8] 
Python Lists
Heterogeneous : can store values of many kinds 
helium = ['He', 2] 
[string, int] 
neon = ['Ne', 8] 
Python Lists
Heterogeneous : can store values of many kinds 
helium = ['He', 2] 
neon = ['Ne', 8] 
'He 
' 
2 
'Ne' 
8 
helium 
neon 
Python Lists
Heterogeneous : can store values of many kinds 
helium = ['He', 2] 
neon = ['Ne', 8] 
gases = [helium, neon] 
Python Lists
Heterogeneous : can store values of many kinds 
helium = ['He', 2] 
neon = ['Ne', 8] 
gases = [helium, neon] 
'He 
' 
2 
'Ne' 
8 
helium 
gases 
neon 
Python Lists
Heterogeneous : can store values of many kinds 
helium = ['He', 2] 
neon = ['Ne', 8] 
gases = [helium, neon] 
'He 
' 
2 
'Ne' 
8 
helium 
gases 
neon 
Devote a whole 
episode to this 
Python Lists
Loop over elements to "do all" 
Python Lists
Loop over elements to "do all" 
Use while to step through all possible indices 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
Use a for loop to access each value in turn 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
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 
Python Lists
Can delete entries entirely (shortens the list) 
Python Lists
Can delete entries entirely (shortens the list) 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
Can delete entries entirely (shortens the list) 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
del gases[0] 
Python Lists
Can delete entries entirely (shortens the list) 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
del gases[0] 
print gases 
['Ne', 'Ar', 'Kr'] 
Python Lists
Can delete entries entirely (shortens the list) 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
del gases[0] 
print gases 
['Ne', 'Ar', 'Kr'] 
del gases[2] 
Python Lists
Can delete entries entirely (shortens the list) 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
del gases[0] 
print gases 
['Ne', 'Ar', 'Kr'] 
del gases[2] 
print gases 
['Ne', 'Ar'] 
Python Lists
Can delete entries entirely (shortens the list) 
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 
Python Lists
Appending values to a list lengthens it 
Python Lists
Appending values to a list lengthens it 
gases = [] 
Python Lists
Appending values to a list lengthens it 
gases = [] 
gases.append('He') 
Python Lists
Appending values to a list lengthens it 
gases = [] 
gases.append('He') 
gases.append('Ne') 
Python Lists
Appending values to a list lengthens it 
gases = [] 
gases.append('He') 
gases.append('Ne') 
gases.append('Ar') 
Python Lists
Appending values to a list lengthens it 
gases = [] 
gases.append('He') 
gases.append('Ne') 
gases.append('Ar') 
print gases 
['He', 'Ne', 'Ar'] 
Python Lists
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 
Python Lists
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 
Python Lists
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) 
Python Lists
Some useful list methods 
Python Lists
Some useful list methods 
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated 
Python Lists
Some useful list methods 
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated 
print gases.count('He') 
2 
Python Lists
Some useful list methods 
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated 
print gases.count('He') 
2 print gases.index('Ar') 
2 
Python Lists
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') 
Python Lists
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'] 
Python Lists
Two that are often used incorrectly 
Python Lists
Two that are often used incorrectly 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
Two that are often used incorrectly 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print gases.sort() 
None 
Python Lists
Two that are often used incorrectly 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print gases.sort() 
None 
print gases 
['Ar', 'He', 'Kr', 'Ne'] 
Python Lists
Two that are often used incorrectly 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print gases.sort() 
None 
print gases 
['Ar', 'He', 'Kr', 'Ne'] 
print gases.reverse() 
None 
Python Lists
Two that are often used incorrectly 
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'] 
Python Lists
Two that are often used incorrectly 
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 
Python Lists
Two that are often used incorrectly 
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 
Python Lists
Use in to test for membership 
Python Lists
Use in to test for membership 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
Use in to test for membership 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print 'He' in gases 
True 
Python Lists
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.' 
Python Lists
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. 
Python Lists
Use range to construct lists of numbers 
Python Lists
Use range to construct lists of numbers 
print range(5) 
[0, 1, 2, 3, 4] 
Python Lists
Use range to construct lists of numbers 
print range(5) 
[0, 1, 2, 3, 4] 
print range(2, 6) 
[2, 3, 4, 5] 
Python Lists
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] 
Python Lists
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) 
[] 
Python Lists
So range(len(list)) is all indices for the list 
Python Lists
So range(len(list)) is all indices for the list 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
Python Lists
So range(len(list)) is all indices for the list 
gases = ['He', 'Ne', 'Ar', 'Kr'] 
print len(gases) 
4 
Python Lists
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] 
Python Lists
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] 
Python Lists
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 
Python Lists
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 
Python Lists
narrated by 
Dominique Vuvan 
October 2010 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.

Contenu connexe

Tendances

F# delight
F# delightF# delight
F# delightpriort
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 
Argparse: Python command line parser
Argparse: Python command line parserArgparse: Python command line parser
Argparse: Python command line parserTimo Stollenwerk
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line toolsEric Wilson
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processingAnton Arhipov
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awkYogesh Sawant
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)teach4uin
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administrationvceder
 
What Shazam doesn't want you to know
What Shazam doesn't want you to knowWhat Shazam doesn't want you to know
What Shazam doesn't want you to knowRoy van Rijn
 
Python workshop intro_string (1)
Python workshop intro_string (1)Python workshop intro_string (1)
Python workshop intro_string (1)Karamjit Kaur
 
Ruby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と HashRuby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と Hashhigaki
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 

Tendances (20)

F# delight
F# delightF# delight
F# delight
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
Argparse: Python command line parser
Argparse: Python command line parserArgparse: Python command line parser
Argparse: Python command line parser
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
 
Practical unix utilities for text processing
Practical unix utilities for text processingPractical unix utilities for text processing
Practical unix utilities for text processing
 
Grep Introduction
Grep IntroductionGrep Introduction
Grep Introduction
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
What Shazam doesn't want you to know
What Shazam doesn't want you to knowWhat Shazam doesn't want you to know
What Shazam doesn't want you to know
 
Python
PythonPython
Python
 
Music as data
Music as dataMusic as data
Music as data
 
Python :variable types
Python :variable typesPython :variable types
Python :variable types
 
Python workshop intro_string (1)
Python workshop intro_string (1)Python workshop intro_string (1)
Python workshop intro_string (1)
 
Linux intro 5 extra: awk
Linux intro 5 extra: awkLinux intro 5 extra: awk
Linux intro 5 extra: awk
 
Ruby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と HashRuby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と Hash
 
Five
FiveFive
Five
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 

En vedette

Python List Comprehensions
Python List ComprehensionsPython List Comprehensions
Python List ComprehensionsYos Riady
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionariesMarc Gouw
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplAnkur Shrivastava
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Jaganadh Gopinadhan
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of RESTYos Riady
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 

En vedette (10)

Python List Comprehensions
Python List ComprehensionsPython List Comprehensions
Python List Comprehensions
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionaries
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of REST
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 

Similaire à Lists (20)

GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
Word games in c
Word games in cWord games in c
Word games in c
 
Module-3.pptx
Module-3.pptxModule-3.pptx
Module-3.pptx
 
Sixth session
Sixth sessionSixth session
Sixth session
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
Python data handling notes
Python data handling notesPython data handling notes
Python data handling notes
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Beginner's Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet.pdfBeginner's Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet.pdf
 
Python list
Python listPython list
Python list
 
Python lists
Python listsPython lists
Python lists
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
PROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In PrologPROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In Prolog
 
Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In Prolog
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
Python List.ppt
Python List.pptPython List.ppt
Python List.ppt
 
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
 

Dernier

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Dernier (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Lists

  • 1. Python Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
  • 2. Loops let us do things many times Python Lists
  • 3. Loops let us do things many times Collections let us store many values together Python Lists
  • 4. Loops let us do things many times Collections let us store many values together Most popular collection is a list Python Lists
  • 5. Create using [value, value, ...] Python Lists
  • 6. Create using [value, value, ...] Get/set values using var[index] Python Lists
  • 7. Create using [value, value, ...] Get/set values using var[index] gases = ['He', 'Ne', 'Ar', 'Kr'] print gases ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 8. 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 Python Lists
  • 9. Index from 0, not 1 Python Lists
  • 10. Index from 0, not 1 Reasons made sense for C in 1970... Python Lists
  • 11. Index from 0, not 1 Reasons made sense for C in 1970... It's an error to try to access out of range Python Lists
  • 12. Index from 0, not 1 Reasons made sense for C in 1970... 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 Python Lists
  • 13. Use len(list) to get length of list Python Lists
  • 14. Use len(list) to get length of list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 Python Lists
  • 15. 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 Python Lists
  • 16. Some negative indices work Python Lists
  • 17. Some negative indices work values[-1] is last element, values[-2] next-to-last, ... Python Lists
  • 18. Some negative indices work values[-1] is last element, values[-2] next-to-last, ... gases = ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 19. 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 Python Lists
  • 20. 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)-Python Lists
  • 21. 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)-less error prone Python Lists
  • 22. Mutable : can change it after it is created Python Lists
  • 23. Mutable : can change it after it is created gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled Python Lists
  • 24. Mutable : can change it after it is created gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' Python Lists
  • 25. Mutable : can change it after it is created gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' print gases ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 26. Mutable : can change it after it is created gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled gases[3] = 'Kr' print gases ['He', 'Ne', 'Ar', 'Kr'] Location must exist before assignment Python Lists
  • 27. Mutable : can change it after it is created 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'] Python Lists
  • 28. Mutable : can change it after it is created 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 Python Lists
  • 29. Heterogeneous : can store values of many kinds Python Lists
  • 30. Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] Python Lists
  • 31. Heterogeneous : can store values of many kinds helium = ['He', 2] [string, int] neon = ['Ne', 8] Python Lists
  • 32. Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] 'He ' 2 'Ne' 8 helium neon Python Lists
  • 33. Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] gases = [helium, neon] Python Lists
  • 34. Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] gases = [helium, neon] 'He ' 2 'Ne' 8 helium gases neon Python Lists
  • 35. Heterogeneous : can store values of many kinds helium = ['He', 2] neon = ['Ne', 8] gases = [helium, neon] 'He ' 2 'Ne' 8 helium gases neon Devote a whole episode to this Python Lists
  • 36. Loop over elements to "do all" Python Lists
  • 37. Loop over elements to "do all" Use while to step through all possible indices Python Lists
  • 38. 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 Python Lists
  • 39. 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 Python Lists
  • 40. 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 Python Lists
  • 41. 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 Python Lists
  • 42. 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 Python Lists
  • 43. 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 Python Lists
  • 44. 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 Python Lists
  • 45. Use a for loop to access each value in turn Python Lists
  • 46. 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 Python Lists
  • 47. 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 Python Lists
  • 48. 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 Python Lists
  • 49. 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 Python Lists
  • 50. Can delete entries entirely (shortens the list) Python Lists
  • 51. Can delete entries entirely (shortens the list) gases = ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 52. Can delete entries entirely (shortens the list) gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] Python Lists
  • 53. Can delete entries entirely (shortens the list) gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] Python Lists
  • 54. Can delete entries entirely (shortens the list) gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] del gases[2] Python Lists
  • 55. Can delete entries entirely (shortens the list) gases = ['He', 'Ne', 'Ar', 'Kr'] del gases[0] print gases ['Ne', 'Ar', 'Kr'] del gases[2] print gases ['Ne', 'Ar'] Python Lists
  • 56. Can delete entries entirely (shortens the list) 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 Python Lists
  • 57. Appending values to a list lengthens it Python Lists
  • 58. Appending values to a list lengthens it gases = [] Python Lists
  • 59. Appending values to a list lengthens it gases = [] gases.append('He') Python Lists
  • 60. Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') Python Lists
  • 61. Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar') Python Lists
  • 62. Appending values to a list lengthens it gases = [] gases.append('He') gases.append('Ne') gases.append('Ar') print gases ['He', 'Ne', 'Ar'] Python Lists
  • 63. 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 Python Lists
  • 64. 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 Python Lists
  • 65. 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) Python Lists
  • 66. Some useful list methods Python Lists
  • 67. Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated Python Lists
  • 68. Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated print gases.count('He') 2 Python Lists
  • 69. Some useful list methods gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated print gases.count('He') 2 print gases.index('Ar') 2 Python Lists
  • 70. 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') Python Lists
  • 71. 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'] Python Lists
  • 72. Two that are often used incorrectly Python Lists
  • 73. Two that are often used incorrectly gases = ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 74. Two that are often used incorrectly gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None Python Lists
  • 75. Two that are often used incorrectly gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] Python Lists
  • 76. Two that are often used incorrectly gases = ['He', 'Ne', 'Ar', 'Kr'] print gases.sort() None print gases ['Ar', 'He', 'Kr', 'Ne'] print gases.reverse() None Python Lists
  • 77. Two that are often used incorrectly 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'] Python Lists
  • 78. Two that are often used incorrectly 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 Python Lists
  • 79. Two that are often used incorrectly 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 Python Lists
  • 80. Use in to test for membership Python Lists
  • 81. Use in to test for membership gases = ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 82. Use in to test for membership gases = ['He', 'Ne', 'Ar', 'Kr'] print 'He' in gases True Python Lists
  • 83. 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.' Python Lists
  • 84. 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. Python Lists
  • 85. Use range to construct lists of numbers Python Lists
  • 86. Use range to construct lists of numbers print range(5) [0, 1, 2, 3, 4] Python Lists
  • 87. Use range to construct lists of numbers print range(5) [0, 1, 2, 3, 4] print range(2, 6) [2, 3, 4, 5] Python Lists
  • 88. 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] Python Lists
  • 89. 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) [] Python Lists
  • 90. So range(len(list)) is all indices for the list Python Lists
  • 91. So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] Python Lists
  • 92. So range(len(list)) is all indices for the list gases = ['He', 'Ne', 'Ar', 'Kr'] print len(gases) 4 Python Lists
  • 93. 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] Python Lists
  • 94. 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] Python Lists
  • 95. 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 Python Lists
  • 96. 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 Python Lists
  • 97. narrated by Dominique Vuvan October 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.