Python
Lists
A list is a collection of data items
Typically, all the items in the list are of the same
type
But they don�t have to be
We can make a list of integers like this:
>>> a =
[0, 1, 2, 3, 5, 6]
Or
>>> a = range(7)
Which does the same thing
When range() takes a single argument x, it fills in integer
values from 0 to x � 1
Or
>>> a = range(0, 7, 1)
Which does the same thing yet again
The first number is the starting number, the
next is the last number in the list � 1, and the third is the step between
numbers in the list
Try changing the numbers within parentheses to
see what you get. Try negative numbers!
You can make a list of strings:
>>> workdays = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday"]
Please note: single and double quote marks in word processed documents are often not
the same characters as those recognized by Python (or other programming
languages)
For best results, type this stuff in yourself
Doing things with lists
Python uses the handy concept of list �slices�
to extract portions of a list
For example, you can create a new list from
workdays that reverses their order:
>>> reverseWorkdays = workdays[::-1]
You can find the index of an item in a list (if
it exists on the list):
>>> workdays.index("Wednesday")
2
Please remember: positions in lists begin with 0 and increase incrementally with each item
You can add things to lists:
>>> workdays.append("Saturday")
And now Saturday has been added to the end of workdays
List comprehensions
These are user-defined operations on lists
that usually create new lists using the old one for data
Here is a relatively simple example:
>>>
[x**2 for x in range(10)]
[0, 1, 4, 9, 16,
25, 36, 49, 64, 81]
** is the power operator so x**2 raises x to the 2nd power
The result is a new list that contains the
squares of the numbers 0-9
For our example, the list comprehension takes
this form:
[x**2���� for x���� in�� range(10)]
Function� input ���� input list
So, for every x from 0 through 9, place the
squared x value in the returned list
Try this to print just the squares of the odd
integers 1, 3, 5, and 9:
>>>
[x**2 for x in [1, 3, 5, 9]]
You could create the same list from our first
example by adding a predicate:
>>>
[x**2 for x in range(10) if x % 2
> 0]
Remember the % (modulo or remainder) operator?
It�s very handy!
The predicate,
if x % 2 > 0
�will �filter� our input list so that only odd numbers (those
that have a non-zero remainder when divided by 2) will be squared and placed on
the list
Don�t get discouraged by the vast number of
ways to do things
It just takes a while to build up your toolkit
Another list comprehension example
This is a good example of how a list
comprehension can work on string objects (from http://www.secnetix.de/olli/Python/list_comprehensions.hawk)
>>> words = 'The quick brown fox jumps over the lazy dog'.split()
>>> print words
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] |
>>>
>>> stuff = [[w.upper(), w.lower(), len(w)] for w in words]
>>> for i in stuff:
... print i
...
['THE', 'the', 3] |
Each w in words is output as a 3-element list
containing w as an upper case word, w as a lower case word, and w�s length