% python 
>>> nudge = 1
>>> wink  = 2
>>> A, B = nudge, wink             # Tuple assignment
>>> A, B                           # Like A = nudge; B = wink
(1, 2)
>>> [C, D] = [nudge, wink]         # List assignment
>>> C, D
(1, 2)



>>> nudge = 1
>>> wink  = 2
>>> nudge, wink = wink, nudge      # Tuples: swaps values
>>> nudge, wink                    # Like T = nudge; nudge = wink; wink = T
(2, 1)



>>> [a, b, c] = (1, 2, 3)          # assign tuple of values to list of names
>>> a, c
(1, 3)
>>> (a, b, c) = "ABC"              # assign string of characters to tuple
>>> a, c
('A', 'C')



>>> string = 'SPAM'
>>> a, b, c, d = string                     # same number on both sides
>>> a, d
('S', 'M')

>>> a, b, c = string                        # error if not
error text omitted
ValueError: too many values to unpack



>>> a, b, c = string[0], string[1], string[2:]        # index and slice
>>> a, b, c
('S', 'P', 'AM')
 
>>> a, b, c = list(string[:2]) + [string[2:]]         # slice and concat
>>> a, b, c
('S', 'P', 'AM')

>>> a, b = string[:2]                                 # same, but simpler
>>> c = string[2:]
>>> a, b, c
('S', 'P', 'AM')

>>> (a, b), c = string[:2], string[2:]                # nested sequences
>>> a, b, c
('S', 'P', 'AM')



>>> ((a, b), c) = ('SP', 'AM')             # paired up by shape and position
>>> a, b, c
('S', 'P', 'AM')



>>> red, green, blue = range(3)
>>> red, blue
(0, 2)



>>> range(3)                        # try list(range(3)) in Python 3.0
[0, 1, 2]



>>> L = [1, 2, 3, 4]
>>> while L:
        front, L = L[0], L[1:]
        print front, L
	
1 [2, 3, 4]
2 [3, 4]
3 [4]
4 []



        front = L[0]
        L = L[1:]



>>> a = b = c = 'spam'
>>> a, b, c
('spam', 'spam', 'spam')

>>> c = 'spam'
>>> b = c
>>> a = b



>>> a = b = 0
>>> b = b + 1
>>> a, b
(0, 1)



>>> a = b = []
>>> b.append(42)
>>> a, b
([42], [42])



>>> a = []
>>> b = []
>>> b.append(42)
>>> a, b
([], [42])



>>> x = 1
>>> x = x + 1          # Traditional
>>> x
2
>>> x += 1             # Augmented
>>> x
3



>>> S = "spam"
>>> S += "SPAM"        # Implied concatenation
>>> S
'spamSPAM'



>>> L = [1, 2]
>>> L = L + [3]            # Concatenate: slower
>>> L
[1, 2, 3]
>>> L.append(4)            # Faster, but in-place
>>> L
[1, 2, 3, 4]



>>> L = L + [5, 6]         # Concatenate: slower
>>> L
[1, 2, 3, 4, 5, 6]
>>> L.extend([7, 8])       # Faster, but in-place
>>> L
[1, 2, 3, 4, 5, 6, 7, 8]



>>> L += [9, 10]           # Mapped to L.extend([9, 10])
>>> L
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



>>> L = [1, 2]
>>> M = L                     # L and M reference the same object
>>> L = L + [3, 4]            # concatenation makes a new object
>>> L, M                      # changes L but not M
([1, 2, 3, 4], [1, 2])

>>> L = [1, 2]
>>> M = L
>>> L += [3, 4]               # but += really means extend
>>> L, M                      # M sees the in-place change too!
([1, 2, 3, 4], [1, 2, 3, 4])



>>> x = 0            # x bound to an integer object
>>> x = "Hello"      # Now it's a string.
>>> x = [1, 2, 3]    # And now it's a list.





>>> L = [1, 2]
>>> L.append(3)             # append is an in-place change
>>> L
[1, 2, 3]



>>> L = L.append(4)         # but append returns None, not L
>>> print L                 # we lose our list!
None



>>> x = 'a'
>>> y = 'b'
>>> print x, y
a b




>>> print x + y
ab
>>> print '%s...%s' % (x, y)
a...b



>>> print 'hello world'                 # Print a string object.
hello world



>>> 'hello world'                       # Interactive echoes
'hello world'



>>> import sys                          # Printing the hard way
>>> sys.stdout.write('hello world\n')
hello world



print X

import sys
sys.stdout.write(str(X) + '\n')



import sys
sys.stdout = open('log.txt', 'a')     # Redirects prints to file
...
print x, y, x                         # Shows up in log.txt



>>> import sys
>>> temp = sys.stdout                   # save for restoring later
>>> sys.stdout = open('log.txt', 'a')   # redirect prints to a file
>>> print 'spam'                        # prints go to file, not here
>>> print 1, 2, 3
>>> sys.stdout.close()                  # flush output to disk
>>> sys.stdout = temp                   # restore original stream

>>> print 'back here'                   # prints show up here again
back here
>>> print open('log.txt').read()        # result of earlier prints
spam
1 2 3



log =  open('log.txt', 'a')     
print >> log, x, y, z                 # Print to a file-like object.
print a, b, c                         # Print to original stdout.



>>> log = open('log.txt', 'w')
>>> print >> log, 1, 2, 3
>>> print >> log, 4, 5, 6
>>> log.close()
>>> print 7, 8, 9
7 8 9
>>> print open('log.txt').read()
1 2 3
4 5 6



>>> import sys
>>> sys.stderr.write(('Bad!' * 8) + '\n')
Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!

>>> print >> sys.stderr, 'Bad!' * 8
Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!



class FileFaker:
    def write(self, string):
        # Do something with the string.
import sys
sys.stdout = FileFaker()
print someObjects         # Sends to the class write method



myobj = FileFaker()           # Redirect to an object for one print
print >> myobj, someObjects   # Does not reset sys.stdout



python script.py < inputfile > outputfile
python script.py | filterProgram

