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
print x[0] # prints 1
print x[len(x)-1] # prints the values of x[5], that is, prints 6
print x[-1] # prints the last element in the list, that is, prints 6
x.pop() # shortens x by removing its rightmost value
print x # prints [1,2,3,4,5]
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"
# You can use colon-indexing to grab a sublist out of a list:
# read x[:n] as "all elements of x upto (not including) n"
# read x[n:] as "all elements of x from n to the end"
x = [0,2,4,6,8]
y = x[1:] # assigns [2,4,6,8] to y
z = x[:3] # assigns [0,2,4] to z
w = x[:len(x)-1] # assigns [0,2,4,6] to w
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"]
Types
Python has classes, and one can construct objects from classes. Lists and dictionaries
are "built-in classes", similar to arrays in Java and C#. It is possible to ask for the
type of an object and compare types:
a = [1,2,3]
d = { "a": 99 }
def p() : print "hello"
print type(a) # prints <type 'list'>, a type-tag object
print type(p) # prints <type 'function'>
if type(a) == type(1) : # does a have the same type as the int 1 ?
print "a is an int"
elif type(a) == type{"") : # does a have the same type as a string ?
print "a is a string"
elif type(a) == type(d) : # do a and d have the same types ?
print "a and d have the same type"
else :
print "I dunno"
Here is a 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?)