>>> (1, 2) + (3, 4)             # Concatenation
(1, 2, 3, 4)

>>> (1, 2) * 4                  # Repitition
(1, 2, 1, 2, 1, 2, 1, 2)

>>> T = (1, 2, 3, 4)            # Indexing, slicing
>>> T[0], T[1:3]
(1, (2, 3))



>>> x = (40)         # An integer
>>> x
40
>>> y = (40,)        # A tuple containing an integer
>>> y
(40,)



>>> T = ('cc', 'aa', 'dd', 'bb')
>>> tmp = list(T)                         # make list from a tuple's items
>>> tmp.sort()                            # sort the list 
>>> tmp
['aa', 'bb', 'cc', 'dd']
>>> T = tuple(tmp)                        # make tuple from a list's items
>>> T
('aa', 'bb', 'cc', 'dd')



>>> T = (1, 2, 3, 4, 5)
>>> L = [x + 20 for x in T]
>>> L
[21, 22, 23, 24, 25]



>>> T = (1, [2, 3], 4)

>>> T[1] = 'spam'                     # This fails: cant chage tuple itself
TypeError: object doesn't support item assignment

>>> T[1][0] = 'spam'                  # This works: can chage mutables inside
>>> T
(1, ['spam', 3], 4)



>>> myfile = open('myfile', 'w')             # Open for output (creates).
>>> myfile.write('hello text file\n')        # Write a line of text.
>>> myfile.close()                           # Flush output buffers to disk.

>>> myfile = open('myfile')                  # Open for input: 'r' is default
>>> myfile.readline()                        # Read the line back.
'hello text file\n'
>>> myfile.readline()                        # Empty string: end of file
''



>>> X, Y, Z = 43, 44, 45                    # native Python objects
>>> S = 'Spam'                              # must be strings to store in file
>>> D = {'a': 1, 'b': 2}
>>> L = [1, 2, 3]
>>> 
>>> F = open('datafile.txt', 'w')                 # create output file
>>> F.write(S + '\n')                             # terminate lines with \n
>>> F.write('%s,%s,%s\n' % (X, Y, Z))             # convert numbers to strings
>>> F.write(str(L) + '$' + str(D) + '\n')         # convert and separate with $
>>> F.close()



>>> bytes = open('datafile.txt').read()            # raw bytes display
>>> bytes
"Spam\n43,44,45\n[1, 2, 3]${'a': 1, 'b': 2}\n"
>>> print bytes                                    # user-friendly display
Spam
43,44,45
[1, 2, 3]${'a': 1, 'b': 2}



>>> F = open('datafile.txt')                      # open again 
>>> line = F.readline()                           # read one line
>>> line
'Spam\n'
>>> line.rstrip()                                 # remove end-of-line
'Spam'



>>> line = F.readline()                           # next line from file
>>> line                                          # it is a string here
'43,44,45\n'
>>> parts = line.split(',')                       # split up on commas
>>> parts
['43', '44', '45\n']



>>> int(parts[1])                                 # convert form string to int
44
>>> numbers = [int(P) for P in parts]             # convert all in list at once
>>> numbers
[43, 44, 45]



>>> line = F.readline()
>>> line
"[1, 2, 3]${'a': 1, 'b': 2}\n"
>>> parts = line.split('$')                       # split (parse) on $
>>> parts
['[1, 2, 3]', "{'a': 1, 'b': 2}\n"]
>>> eval(parts[0])                                # convert to any object type
[1, 2, 3]
>>> objects = [eval(P) for P in parts]            # do same for all in list
>>> objects
[[1, 2, 3], {'a': 1, 'b': 2}]



>>> F = open('datafile.txt', 'w')
>>> import pickle
>>> pickle.dump(D, F)                         # pickle any object to file
>>> F.close()



>>> F = open('datafile.txt')
>>> E = pickle.load(F)                        # load any object from file
>>> E
{'a': 1, 'b': 2}



>>> open('datafile.txt').read()
"(dp0\nS'a'\np1\nI1\nsS'b'\np2\nI2\ns."



>>> F = open('data.bin', 'wb')                       # open binary output file
>>> import struct
>>> bytes = struct.pack('>i4sh', 7, 'spam', 8)       # make packed binary data
>>> bytes
'\x00\x00\x00\x07spam\x00\x08'
>>> F.write(bytes)                                   # write byte string 
>>> F.close()



>>> F = open('data.bin', 'rb')
>>> data = F.read()                                  # get packed binary data
>>> data
'\x00\x00\x00\x07spam\x00\x08'
>>> values = struct.unpack('>i4sh', data)            # convert to Python objects
>>> values
(7, 'spam', 8)



>>> L = ['abc', [(1, 2), ([3], 4)], 5]
>>> L[1]
[(1, 2), ([3], 4)]
>>> L[1][1]
([3], 4)
>>> L[1][1][0]
[3]
>>> L[1][1][0][0]
3



>>> X = [1, 2, 3]
>>> L = ['a', X, 'b']           # Embed references to X's object.
>>> D = {'x':X, 'y':2}



>>> X[1] = 'surprise'         # Changes all three references!
>>> L
['a', [1, 'surprise', 3], 'b']
>>> D
{'x': [1, 'surprise', 3], 'y': 2}



>>> L = [1,2,3]
>>> D = {'a':1, 'b':2}

>>> A = L[:]                # Instead of: A = L (or list(L))
>>> B = D.copy()            # Instead of: B = D

>>> A[1] = 'Ni'
>>> B['c'] = 'spam'
>>>
>>> L, D
([1, 2, 3], {'a': 1, 'b': 2})
>>> A, B
([1, 'Ni', 3], {'a': 1, 'c': 'spam', 'b': 2})



>>> X = [1, 2, 3]
>>> L = ['a', X[:], 'b']          # Emded copies of X's object.
>>> D = {'x':X[:], 'y':2}



>>> L1 = [1, ('a', 3)]         # Same value, unique objects
>>> L2 = [1, ('a', 3)]
>>> L1 == L2, L1 is L2         # Equivalent? Same object?
(True, False)



>>> S1 = 'spam'
>>> S2 = 'spam'
>>> S1 == S2, S1 is S2
(True, True)



>>> S1 = 'a longer string'
>>> S2 = 'a longer string'
>>> S1 == S2, S1 is S2
(True, False)



>>> L1 = [1, ('a', 3)]
>>> L2 = [1, ('a', 2)]
>>> L1 < L2, L1 == L2, L1 > L2     # less,equal,greater: tuple of results
(False, False, True)





>>> L = [None] * 100
>>>
>>> L
[None, None, None, None, None, None, None, . . . ]



isinstance([1],list)
type([1])==list
type([1])==type([])
type([1])==types.ListType



>>> L = [1, 2, 3]
>>> M = ['X', L, 'Y']       # Embed a reference to L.
>>> M
['X', [1, 2, 3], 'Y']

>>> L[1] = 0                # Changes M too
>>> M
['X', [1, 0, 3], 'Y']



>>> L = [1, 2, 3]
>>> M = ['X', L[:], 'Y']       # Embed a copy of L.
>>> L[1] = 0                   # Changes only L, not M 
>>> L
[1, 0, 3]
>>> M
['X', [1, 2, 3], 'Y']



>>> L = [4, 5, 6]
>>> X = L * 4           # Like [4, 5, 6] + [4, 5, 6] + ...
>>> Y = [L] * 4         # [L] + [L] + ... = [L, L,...]

>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]



>>> L[1] = 0            # Impacts Y but not X
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]



>>> L = ['grail']              # Append reference to same object.
>>> L.append(L)                # Generates cycle in object: [...]
>>> L
['grail', [...]]



T = (1, 2, 3)

T[2] = 4             # Error!

T = T[:2] + (4,)     # Okay: (1, 2, 4)



2 ** 16
2 / 5, 2 / 5.0

"spam" + "eggs"
S = "ham"
"eggs " + S
S * 5
S[:0]
"green %s and %s" % ("eggs", S)

('x',)[0]
('x', 'y')[1]

L = [1,2,3] + [4,5,6]
L, L[:], L[:0], L[-2], L[-2:]
([1,2,3] + [4,5,6])[2:4]
[L[2], L[3]]
L.reverse(); L
L.sort(); L
L.index(4)

{'a':1, 'b':2}['b']
D = {'x':1, 'y':2, 'z':3}
D['w'] = 0
D['x'] + D['w']
D[(1,2,3)] = 4
D.keys(), D.values(), D.has_key((1,2,3))

[[]], ["",[],(),{},None]



>>> X = 'spam'
>>> Y = 'eggs'
>>> X, Y = Y, X



>>> D = {}
>>> D[1] = 'a'
>>> D[2] = 'b'
>>> D[(1, 2, 3)] = 'c'
>>> D
{1: 'a', 2: 'b', (1, 2, 3): 'c'}


