In [3]:
a = [1, 123, 456]
In [4]:
print a
[1, 123, 456]

In [8]:
a_squares = []
for v in a:
    a_squares = a_squares + [v*v]
print a_squares
[1, 15129, 207936]

In [9]:
a_squares = []
for v in a:
    a_squares.append(v*v)
print a_squares
[1, 15129, 207936]

In [10]:
a_squares = [ v*v for v in a ]
print a_squares
[1, 15129, 207936]

In [11]:
d = { 'bob': 20, 'joe': 21 }
In [12]:
print d
{'bob': 20, 'joe': 21}

In [13]:
d['bob']
Out[13]:
20
In [14]:
d['bob'] = 21
In [15]:
print d
{'bob': 21, 'joe': 21}

In [16]:
d['jane'] = 20
In [17]:
print d
{'jane': 20, 'bob': 21, 'joe': 21}

In [19]:
print a
[1, 123, 456]

In [22]:
for p,age in d.iteritems():
    print "Hello "+p+", you are "+str(age)
Hello jane, you are 20
Hello bob, you are 21
Hello joe, you are 21

In [25]:
message = { p: "Hello, you are "+str(age)   for p,age in d.iteritems() }
In [27]:
print message
{'jane': 'Hello, you are 20', 'bob': 'Hello, you are 21', 'joe': 'Hello, you are 21'}

In [28]:
message = { p: "Hello, you are "+str(age)   for p,age in d.iteritems() if age > 20 }
In [29]:
print message
{'bob': 'Hello, you are 21', 'joe': 'Hello, you are 21'}

In [30]:
a
Out[30]:
[1, 123, 456]
In [31]:
a[1] = 1234567
In [32]:
print a
[1, 1234567, 456]

In []: