Lists
Lists are arrays that grow as needed.
A list is written with this syntax: [val0, val1, ..., valn],
e.g., [2, 5, "a", []], is a list of two ints, a string, and an
empty list. (You can mix types within a Python list).
Here are basic operations on lists:
x = [1,2,3] # sets x to the list, [1,2,3]
x.append(4) # resets x to [1,2,3,4]
y = x[1] # sets y to 2 (you index a list like an array)
z = [(y*y)+1, 4+2] # sets z to [5,6]
x = x + z # sets x to [1,2,3,4,5,6]
print x # prints the list, [1,2,3,4,5,6]
print len(x) # prints 6, the length of the list named by x
if isinstance(x, list): # asks if x's value is a list
for item in x : # the for-loop iterates through x's elements
print item, item * item
# the loop prints 1 1
# 2 4
# ...
# 6 36
# this loop does the same work as the above for-loop:
i = 0
while i != len(x) :
item = x[i]
print item, item * item
i = i + 1
if y in x : # asks if y's value is present within list x
print "yes"
# lists can be "sliced" (sublist-ed):
x = [2, 4, 6, 8]
y = x[1:] # "slice out" sublist x[1], x[2], ... and assign to y
print y # prints [4,6,8]
z = x[:3] # "slice out" sublist up to, but not including, element 3
print z # prints [2,4,6]
# how to emulate a stack with slicing:
st = [] # empty
st = [2] + st # push 2
st = [4] + st # push 4
print st[0] # print top
st = st[1:] # pop
# Using append and pop to emulate a stack:
st = [] # empty
st.append(2)
st.append(4) # st has value [2,4]
print st[len(st)-1] # prints top value, 4
print st[-1] # also prints 4, since negative ints index from the right (!)
n = st.pop() # pop is a built-in method
print n, st # prints 4 [2]
# Here is cute trick that exposes that lists are really objects:
if x in x :
print "is this possible?!" # Actually, it is: x = ["boo"]
# x.append(x)
# Now, x in x computes to True (why?)
Dictionaries
A dictionary (implemented in Python as a hash table) is similar to a record struct, but dictionaries
can grow as needed and the key set is not fixed in advance.
A dictionary is written with this syntax:
{key0: val0, key1: val1, ... keyn: valn}, e.g.,
{"p": True, "q": False, 99: {}} is a dictionary that has entries
for the keys "p", "q", and 99. (You can mix keys and values
of different type, but lists and dictionaries cannot themselves
be keys.)
Here are basic operations on dictionaries:
d = {"p": True, "q": False, 99: {}} # sets d to {"p":True, "q":False, 99:{}}
x = d["q"] # sets x to False
d["r"] = not x # adds new key:val pair to d, which now is
# {"p":True, "q":False, 99:{}, "r":True}
d["q"] = 100 # resets the value of the "q" key; d is
# {"p":True, "q":100, 99:{}, "r":True}
print d # prints the dict, but the keys might
# be in nonsorted order, since the dict
# is implemented as a hash table
if isinstance(d, dict): # asks if d's value is a dictionary
for key in d : # like the for-loop for lists, enumerates each
print key, d[key] # key within d and uses it in the body
if "s" in d : # asks if d holds a key named "s"
print d["s"]
Of course, you can build a list of dictionaries or a dictionary of lists, or you can mix together dictionaries, lists, ints, strings, etc., in a single data structure.