1. The basics. Here are the sort of results you should get, along with a few comments about their meaning:

                         Numbers
    >>> 2 ** 16              # 2 raised to the power 16
    65536
    >>> 2 / 5, 2 / 5.0       # integer / truncates, float / doesn't
    (0, 0.4)
    
    Strings
    >>> "spam" + "eggs"      # concatenation
    'spameggs'
    >>> 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 key
    2
    >>> 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
    ([[]], ['', [], (), {}, None])
  2. Indexing and slicing ...

Get Learning Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.