pythonnotes's posterous http://pythonnotes.posterous.com Most recent posts at pythonnotes's posterous posterous.com Mon, 29 Nov 2010 09:56:58 -0800 Python lists http://pythonnotes.posterous.com/python-lists http://pythonnotes.posterous.com/python-lists #Lists are useful as a way of assigning a series of values to a particular list name
#Those values can then be called by their position (index) in the series

myfridge = ['cheese', 'milk', 'beer']

#the square brackets indicate that this is a list
#each item in the list is separated by a comma
#items can then be called as follows:

myfridge[0]

#this would call the first item in the list (index position 0)

#you can also change the contents of a list with various functions as follows

myfridge.append('ham')

#this appends - .append - the string 'ham' to the list myfridge. 
#Note the full stop before .append and the parenthesis containing the argument to be passed
#the parenthesis could also use a variable instead of a string, or a function that returns a value, or another list

myfridge.pop(-1)

#this returns the last item in the list (at index position -1) and removes it from the list at the same time (pops)

#other functions include 
#counting how many times something occurs in a list - .count('ham')
#returning the index of a particular item in a list - .index('ham')
#removing the first instance in a list of a particular item - .remove('ham')
#inserting an item in a list at a particular position - .insert(3, 'ham')
#sorting items - .sort()
#reversing the order of items - .reverse()

#other list functions can be found at http://docs.python.org/tutorial/datastructures.html

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1080526/paulb_bigger.jpg http://posterous.com/users/ZyH4FHK2wVz Paul Bradshaw paulbradshaw Paul Bradshaw