Lists
-
Upload
marieswaran-ramasamy -
Category
Technology
-
view
97 -
download
0
Transcript of Lists
![Page 1: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/1.jpg)
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.
Python
![Page 2: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/2.jpg)
Python Lists
Loops let us do things many times
![Page 3: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/3.jpg)
Python Lists
Loops let us do things many times
Collections let us store many values together
![Page 4: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/4.jpg)
Python Lists
Loops let us do things many times
Collections let us store many values together
Most popular collection is a list
![Page 5: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/5.jpg)
Python Lists
Create using [value, value, ...]
![Page 6: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/6.jpg)
Python Lists
Create using [value, value, ...]
Get/set values using var[index]
![Page 7: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/7.jpg)
Python Lists
Create using [value, value, ...]
Get/set values using var[index]
gases = ['He', 'Ne', 'Ar', 'Kr']print gases['He', 'Ne', 'Ar', 'Kr']
![Page 8: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/8.jpg)
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
![Page 9: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/9.jpg)
Python Lists
Index from 0, not 1
![Page 10: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/10.jpg)
Python Lists
Index from 0, not 1
Reasons made sense for C in 1970...
![Page 11: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/11.jpg)
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
![Page 12: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/12.jpg)
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
![Page 13: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/13.jpg)
Python Lists
Use len(list) to get length of list
![Page 14: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/14.jpg)
Python Lists
Use len(list) to get length of list
gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4
![Page 15: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/15.jpg)
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
![Page 16: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/16.jpg)
Python Lists
Some negative indices work
![Page 17: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/17.jpg)
Python Lists
Some negative indices work
values[-1] is last element, values[-2] next-to-last, ...
![Page 18: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/18.jpg)
Python Lists
Some negative indices work
values[-1] is last element, values[-2] next-to-last, ...
gases = ['He', 'Ne', 'Ar', 'Kr']
![Page 19: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/19.jpg)
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
![Page 20: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/20.jpg)
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)-1]
![Page 21: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/21.jpg)
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
less error prone
values[-1] is much nicer than values[len(values)-1]
![Page 22: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/22.jpg)
Python Lists
Mutable : can change it after it is created
![Page 23: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/23.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled
Mutable : can change it after it is created
![Page 24: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/24.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[3] = 'Kr'
Mutable : can change it after it is created
![Page 25: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/25.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[3] = 'Kr'print gases['He', 'Ne', 'Ar', 'Kr']
Mutable : can change it after it is created
![Page 26: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/26.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[3] = 'Kr'print gases['He', 'Ne', 'Ar', 'Kr']
Location must exist before assignment
Mutable : can change it after it is created
![Page 27: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/27.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[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
![Page 28: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/28.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[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
![Page 29: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/29.jpg)
Python Lists
Heterogeneous : can store values of many kinds
![Page 30: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/30.jpg)
Python Lists
Heterogeneous : can store values of many kinds
helium = ['He', 2]neon = ['Ne', 8]
![Page 31: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/31.jpg)
Python Lists
Heterogeneous : can store values of many kinds
helium = ['He', 2]neon = ['Ne', 8]
[string, int]
![Page 32: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/32.jpg)
Python Lists
Heterogeneous : can store values of many kinds
helium = ['He', 2]neon = ['Ne', 8]
'He'2
'Ne'
8
helium
neon
![Page 33: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/33.jpg)
Python Lists
Heterogeneous : can store values of many kinds
helium = ['He', 2]neon = ['Ne', 8]gases = [helium, neon]
![Page 34: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/34.jpg)
Python Lists
Heterogeneous : can store values of many kinds
helium = ['He', 2]neon = ['Ne', 8]gases = [helium, neon]
'He'2
'Ne'
8
helium
neon
gases
![Page 35: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/35.jpg)
Python Lists
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
![Page 36: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/36.jpg)
Python Lists
Loop over elements to "do all"
![Page 37: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/37.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
![Page 38: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/38.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1
![Page 39: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/39.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1
First legal index
![Page 40: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/40.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1 Next index
![Page 41: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/41.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1
Defines set of legal indices
![Page 42: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/42.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1HeNeArKr
![Page 43: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/43.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1HeNeArKr
Tedious to type in over and over again
![Page 44: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/44.jpg)
Python Lists
Loop over elements to "do all"
Use while to step through all possible indices
gases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1HeNeArKr
Tedious to type in over and over again
And it's easy to forget the "+= 1" at the end
![Page 45: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/45.jpg)
Python Lists
Use a for loop to access each value in turn
![Page 46: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/46.jpg)
Python Lists
Use a for loop to access each value in turn
gases = ['He', 'Ne', 'Ar', 'Kr']for gas in gases: print gasHeNeArKr
![Page 47: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/47.jpg)
Python Lists
Use a for loop to access each value in turn
gases = ['He', 'Ne', 'Ar', 'Kr']for gas in gases: print gasHeNeArKr
Loop variable assigned each value in turn
![Page 48: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/48.jpg)
Python Lists
Use a for loop to access each value in turn
gases = ['He', 'Ne', 'Ar', 'Kr']for gas in gases: print gasHeNeArKr
Loop variable assigned each value in turn
Not each index
![Page 49: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/49.jpg)
Python Lists
Use a for loop to access each value in turn
gases = ['He', 'Ne', 'Ar', 'Kr']for gas in gases: print gasHeNeArKr
Loop variable assigned each value in turn
Not each index
Because that's the most common case
![Page 50: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/50.jpg)
Python Lists
Can delete entries entirely (shortens the list)
![Page 51: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/51.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']
Can delete entries entirely (shortens the list)
![Page 52: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/52.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']del gases[0]
Can delete entries entirely (shortens the list)
![Page 53: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/53.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']del gases[0]print gases['Ne', 'Ar', 'Kr']
Can delete entries entirely (shortens the list)
![Page 54: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/54.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']del gases[0]print gases['Ne', 'Ar', 'Kr']del gases[2]
Can delete entries entirely (shortens the list)
![Page 55: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/55.jpg)
Python Lists
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)
![Page 56: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/56.jpg)
Python Lists
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)
![Page 57: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/57.jpg)
Python Lists
Appending values to a list lengthens it
![Page 58: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/58.jpg)
Python Lists
Appending values to a list lengthens it
gases = []
![Page 59: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/59.jpg)
Python Lists
Appending values to a list lengthens it
gases = []gases.append('He')
![Page 60: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/60.jpg)
Python Lists
Appending values to a list lengthens it
gases = []gases.append('He')gases.append('Ne')
![Page 61: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/61.jpg)
Python Lists
Appending values to a list lengthens it
gases = []gases.append('He')gases.append('Ne')gases.append('Ar')
![Page 62: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/62.jpg)
Python Lists
Appending values to a list lengthens it
gases = []gases.append('He')gases.append('Ne')gases.append('Ar')print gases['He', 'Ne', 'Ar']
![Page 63: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/63.jpg)
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
![Page 64: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/64.jpg)
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
![Page 65: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/65.jpg)
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)
![Page 66: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/66.jpg)
Python Lists
Some useful list methods
![Page 67: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/67.jpg)
Python Lists
Some useful list methods
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated
![Page 68: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/68.jpg)
Python Lists
Some useful list methods
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicatedprint gases.count('He')2
![Page 69: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/69.jpg)
Python Lists
Some useful list methods
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicatedprint gases.count('He')2print gases.index('Ar')2
![Page 70: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/70.jpg)
Python Lists
Some useful list methods
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicatedprint gases.count('He')2print gases.index('Ar')2gases.insert(1, 'Ne')
![Page 71: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/71.jpg)
Python Lists
Some useful list methods
gases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicatedprint gases.count('He')2print gases.index('Ar')2gases.insert(1, 'Ne')print gases['He', 'Ne', 'He', 'Ar', 'Kr']
![Page 72: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/72.jpg)
Python Lists
Two that are often used incorrectly
![Page 73: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/73.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']
Two that are often used incorrectly
![Page 74: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/74.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()None
Two that are often used incorrectly
![Page 75: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/75.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()Noneprint gases['Ar', 'He', 'Kr', 'Ne']
Two that are often used incorrectly
![Page 76: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/76.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()Noneprint gases['Ar', 'He', 'Kr', 'Ne']print gases.reverse()None
Two that are often used incorrectly
![Page 77: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/77.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()Noneprint gases['Ar', 'He', 'Kr', 'Ne']print gases.reverse()Noneprint gases['Ne', 'Kr', 'He', 'Ar']
Two that are often used incorrectly
![Page 78: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/78.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()Noneprint gases['Ar', 'He', 'Kr', 'Ne']print gases.reverse()Noneprint gases['Ne', 'Kr', 'He', 'Ar']
A common bug
Two that are often used incorrectly
![Page 79: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/79.jpg)
Python Lists
gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()Noneprint gases['Ar', 'He', 'Kr', 'Ne']print gases.reverse()Noneprint gases['Ne', 'Kr', 'He', 'Ar']
A common bug
gases = gases.sort() assigns None to gases
Two that are often used incorrectly
![Page 80: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/80.jpg)
Python Lists
Use in to test for membership
![Page 81: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/81.jpg)
Python Lists
Use in to test for membership
gases = ['He', 'Ne', 'Ar', 'Kr']
![Page 82: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/82.jpg)
Python Lists
Use in to test for membership
gases = ['He', 'Ne', 'Ar', 'Kr']print 'He' in gasesTrue
![Page 83: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/83.jpg)
Python Lists
Use in to test for membership
gases = ['He', 'Ne', 'Ar', 'Kr']print 'He' in gasesTrueif 'Pu' in gases: print 'But plutonium is not a gas!'else: print 'The universe is well ordered.'
![Page 84: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/84.jpg)
Python Lists
Use in to test for membership
gases = ['He', 'Ne', 'Ar', 'Kr']print 'He' in gasesTrueif 'Pu' in gases: print 'But plutonium is not a gas!'else: print 'The universe is well ordered.'The universe is well ordered.
![Page 85: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/85.jpg)
Python Lists
Use range to construct lists of numbers
![Page 86: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/86.jpg)
Python Lists
Use range to construct lists of numbers
print range(5)[0, 1, 2, 3, 4]
![Page 87: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/87.jpg)
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]
![Page 88: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/88.jpg)
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]
![Page 89: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/89.jpg)
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)[]
![Page 90: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/90.jpg)
Python Lists
So range(len(list)) is all indices for the list
![Page 91: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/91.jpg)
Python Lists
So range(len(list)) is all indices for the list
gases = ['He', 'Ne', 'Ar', 'Kr']
![Page 92: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/92.jpg)
Python Lists
So range(len(list)) is all indices for the list
gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4
![Page 93: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/93.jpg)
Python Lists
So range(len(list)) is all indices for the list
gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4print range(len(gases))[0, 1, 2, 3]
![Page 94: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/94.jpg)
Python Lists
So range(len(list)) is all indices for the list
gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4print range(len(gases))[0, 1, 2, 3]for i in range(len(gases)): print i, gases[i]
![Page 95: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/95.jpg)
Python Lists
So range(len(list)) is all indices for the list
gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4print range(len(gases))[0, 1, 2, 3]for i in range(len(gases)): print i, gases[i]0 He1 Ne2 Ar3 Kr
![Page 96: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/96.jpg)
Python Lists
So range(len(list)) is all indices for the list
gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4print range(len(gases))[0, 1, 2, 3]for i in range(len(gases)): print i, gases[i]0 He1 Ne2 Ar3 Kr
A very common idiom in Python
![Page 97: Lists](https://reader033.fdocuments.us/reader033/viewer/2022052602/55af35c91a28ab76028b482a/html5/thumbnails/97.jpg)
October 2010
narrated by
Dominique Vuvan
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.