12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

S = "ham">>> "eggs " + S'eggs ham'>>> S * 5 # Repetition'hamhamhamhamham'>>> S[:0] # An empty slice at the front -- [0:0]''>>> "green %s and %s" % ("eggs", S) # Formatting'green eggs and ham'# Tuples>>> ('x',)[0] # Indexing a single-item tuple'x'>>> ('x', 'y')[1] # Indexing a 2-item tuple'y'# Lists>>> L = [1,2,3] + [4,5,6] # List operations>>> L, L[:], L[:0], L[-2], L[-2:]([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [], 5, [5, 6])>>> ([1,2,3]+[4,5,6])[2:4][3, 4]>>> [L[2], L[3]] # Fetch from offsets; store in a list[3, 4]>>> L.reverse( ); L # Method: reverse list in-place[6, 5, 4, 3, 2, 1]>>> L.sort( ); L # Method: sort list in-place[1, 2, 3, 4, 5, 6]>>> L.index(4) # Method: offset of first 4 (search)3# Dictionaries>>> {'a':1, 'b':2}['b'] # Index a dictionary by key2>>> D = {'x':1, 'y':2, 'z':3}>>> D['w'] = 0 # Create a new entry>>> D['x'] + D['w']1>>> D[(1,2,3)] = 4 # A tuple used as a key (immutable)>>> D{'w': 0, 'z': 3, 'y': 2, (1, 2, 3): 4, 'x': 1}>>> D.keys( ), D.values( ), D.has_key((1,2,3)) # Methods(['w', 'z', 'y', (1, 2, 3), 'x'], [0, 3, 2, 4, 1], 1)# Empties>>> [[]], ["",[],( ),{},None] # Lots of nothings: empty objects([[]], ['', [], ( ), {}, None])Part II, Types and Operations | 649

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!