Assigning Values to a List
A list is, as the name suggests, a series of values. In Python, these values are assigned by placing them within square braces and separating them by commas like this:
Note that 'addends' is comprised of integer variables while the others are comprised of strings, as the quotes suggest.
<name of list> = [ <value>, <value>, <value> ] girls = ['sugar', 'spice', 'everything nice'] lotto = ['26', '12', '23'] addends = [4, 34, 7]
A list can contain any type of Python object -- even other lists.
>>> lotto = addends >>> print lotto ['26', '12', [4, 34, 7]]
Accessing the Values of List
To access a part of a list, one uses the same kind of phrase as one used for a string literal:
A few examples:
<name of list>[<index number>]
ingredient1 = girls print girls print 'brown ' + ingredient1 print girls + " is not a " + girls
sugar is not a spice
Lists can be concatenated in a way similar to strings by using the plus operator ('+'):
primes = [1, 3, 5, 7] + [9, 11] mishmash = girls + lotto + addends print primes print mishmashoutput:
[1, 3, 5, 7, 9, 11]
['sugar', 'spice', 'everything nice', '26', '12', '23', 4, 34, 7]