97
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution Lice See http://software-carpentry.org/license.html for more informati Python

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

Embed Size (px)

Citation preview

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Loops let us do things many times

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

Python Lists

Loops let us do things many times

Collections let us store many values together

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Create using [value, value, ...]

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

Python Lists

Create using [value, value, ...]

Get/set values using var[index]

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Index from 0, not 1

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

Python Lists

Index from 0, not 1

Reasons made sense for C in 1970...

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Use len(list) to get length of list

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

Python Lists

Use len(list) to get length of list

gases = ['He', 'Ne', 'Ar', 'Kr']print len(gases)4

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Some negative indices work

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

Python Lists

Some negative indices work

values[-1] is last element, values[-2] next-to-last, ...

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

Python Lists

Some negative indices work

values[-1] is last element, values[-2] next-to-last, ...

gases = ['He', 'Ne', 'Ar', 'Kr']

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Mutable : can change it after it is created

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelled

Mutable : can change it after it is created

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[3] = 'Kr'

Mutable : can change it after it is created

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[3] = 'Kr'print gases['He', 'Ne', 'Ar', 'Kr']

Location must exist before assignmentgases = ['He', 'Ne', 'Ar', 'Kr']

Mutable : can change it after it is created

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'K'] # last entry misspelledgases[3] = 'Kr'print gases['He', 'Ne', 'Ar', 'Kr']

Location must exist before assignmentgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Heterogeneous : can store values of many kinds

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

Python Lists

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

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

Python Lists

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

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

Python Lists

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

'He'2

'Ne'

8

helium

neon

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

Python Lists

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

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

Python Lists

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

'He'2

'Ne'

8

helium

neon

gases

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

Python Lists

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

'He'2

'Ne'

8

helium

neon

gases

Devote a whole

episode to this

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

Python Lists

Loop over elements to "do all"

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indices

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1

First legal index

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1 Next index

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1

Defines set of legal indices

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['He', 'Ne', 'Ar', 'Kr']i = 0while i < len(gases): print gases[i] i += 1HeNeArKr

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

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Loop over elements to "do all"

Use while to step through all possible indicesgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Use a for loop to access each value in turn

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

Python Lists

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

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

Python Lists

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

Loop variable assigned each value in turn

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

Python Lists

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

Loop variable assigned each value in turn

Not each index

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

Python Lists

Use a for loop to access each value in turngases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Can delete entries entirely (shortens the list)

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'Kr']

Can delete entries entirely (shortens the list)

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'Kr']del gases[0]

Can delete entries entirely (shortens the list)

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Appending values to a list lengthens it

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

Python Lists

Appending values to a list lengthens itgases = []

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

Python Lists

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

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

Python Lists

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

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

Python Lists

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

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

Python Lists

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

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

Python Lists

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

Most operations on lists are methods

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

Python Lists

Appending values to a list lengthens itgases = []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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Appending values to a list lengthens itgases = []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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Some useful list methods

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

Python Lists

Some useful list methodsgases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicated

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

Python Lists

Some useful list methodsgases = ['He', 'He', 'Ar', 'Kr'] # 'He' is duplicatedprint gases.count('He')2

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

Python Lists

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

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

Python Lists

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

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

Python Lists

Some useful list methodsgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Two that are often used incorrectly

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'Kr']

Two that are often used incorrectly

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

Python Lists

gases = ['He', 'Ne', 'Ar', 'Kr']print gases.sort()None

Two that are often used incorrectly

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

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Use in to test for membership

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

Python Lists

Use in to test for membershipgases = ['He', 'Ne', 'Ar', 'Kr']

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

Python Lists

Use in to test for membershipgases = ['He', 'Ne', 'Ar', 'Kr']print 'He' in gasesTrue

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

Python Lists

Use in to test for membershipgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Use in to test for membershipgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Use range to construct lists of numbers

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

Python Lists

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

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

Python Lists

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

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

Python Lists

Use range to construct lists of numbersprint 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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

Use range to construct lists of numbersprint 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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

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

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

Python Lists

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

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

Python Lists

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

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

Python Lists

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

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

Python Lists

So range(len(list)) is all indices for the listgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

So range(len(list)) is all indices for the listgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Python Lists

So range(len(list)) is all indices for the listgases = ['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 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

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.