Python Series 1: Python For Productivities

Agenda

  • Intro to basic grammar
  • Demo: A Spelling Corrector
  • Demo: A MSDN Spider
  • </ul>

    All code is avaible at ShusenLiu@Github

    Data Types

    
    
    In [1]:
    a = 1010
    a, type(a)
    
    
    
    
    Out[1]:
    (1010, int)
    
    
    In [2]:
    a = 'Welcome to Python world!'
    a, type(a)
    
    
    
    
    Out[2]:
    ('Welcome to Python world!', str)
    
    
    In [3]:
    a = 3.14
    a, type(a)
    
    
    
    
    Out[3]:
    (3.14, float)
    
    
    In [4]:
    a = 'x'
    a, type(a)
    
    
    
    
    Out[4]:
    ('x', str)
    
    
    In [5]:
    3.14 + 10
    
    
    
    
    Out[5]:
    13.14
    
    
    In [6]:
    'Hello' + ' ' + 'World'
    
    
    
    
    Out[6]:
    'Hello World'

    Data Structures

    
    
    In [7]:
    #list
    xs = [ 1, 2, 3, 4, 5 ]
    xs
    
    
    
    
    Out[7]:
    [1, 2, 3, 4, 5]
    
    
    In [8]:
    for x in xs:
        print x
    
    
    
    
    1
    2
    3
    4
    5
    
    
    
    In [9]:
    #tuple
    ts = (1, 2, 3, 4, 5)
    ts
    
    
    
    
    Out[9]:
    (1, 2, 3, 4, 5)
    
    
    In [11]:
    for t in ts:
        print t
    
    
    
    
    1
    2
    3
    4
    5
    
    
    
    In [12]:
    a1, a2, a3, a4, a5 = ts
    print a1, a2, a3, a4, a5
    
    
    
    
    1 2 3 4 5
    
    
    
    In [13]:
    v1 = 123
    v2 = 'aha'
    print 'before swap:', v1, v2
    v2, v1 = v1, v2
    print "after swap:", v1, v2
    
    
    
    
    before swap: 123 aha
    after swap: aha 123
    
    
    
    In [14]:
    #slicing
    xs[1:4]
    
    
    
    
    Out[14]:
    [2, 3, 4]
    
    
    In [15]:
    xs[1:]
    
    
    
    
    Out[15]:
    [2, 3, 4, 5]
    
    
    In [16]:
    xs[1:4:2]
    
    
    
    
    Out[16]:
    [2, 4]
    
    
    In [17]:
    xs[-1]
    
    
    
    
    Out[17]:
    5
    
    
    In [18]:
    xs[-2]
    
    
    
    
    Out[18]:
    4
    
    
    In [19]:
    xs[:2] + xs[-2:]
    
    
    
    
    Out[19]:
    [1, 2, 4, 5]
    
    
    In [20]:
    ts[::-1]
    
    
    
    
    Out[20]:
    (5, 4, 3, 2, 1)
    
    
    In [21]:
    s = "Python is awesome."
    s[::-1]
    
    
    
    
    Out[21]:
    '.emosewa si nohtyP'
    
    
    In [22]:
    #dictionary
    ds = { 10:'1', 20:'2', '3':30, '4':40 }
    ds
    
    
    
    
    Out[22]:
    {10: '1', 20: '2', '3': 30, '4': 40}
    
    
    In [23]:
    ds.keys()
    
    
    
    
    Out[23]:
    [10, 20, '3', '4']
    
    
    In [24]:
    ds.values()
    
    
    
    
    Out[24]:
    ['1', '2', 30, 40]
    
    
    In [25]:
    ds.items()
    
    
    
    
    Out[25]:
    [(10, '1'), (20, '2'), ('3', 30), ('4', 40)]

    File IO

    
    
    In [26]:
    for idx,line in enumerate(file("big.txt")):
        if idx > 4:
            break
        print idx, line
    
    
    
    
    0 The Project Gutenberg EBook of The Adventures of Sherlock Holmes
    
    1 by Sir Arthur Conan Doyle
    
    2 (#15 in our series by Sir Arthur Conan Doyle)
    
    3 
    
    4 Copyright laws are changing all over the world. Be sure to check the
    
    

    List Comprehension

    
    
    In [27]:
    evens = [ x for x in range(100) if x % 2 == 0 ]
    
    
    
    In [28]:
    evens[:5]
    
    
    
    
    Out[28]:
    [0, 2, 4, 6, 8]
    
    
    In [29]:
    even_squares = [ x**2 for x in range(100) if x % 2 == 0 ]
    
    
    
    In [30]:
    even_squares[:5]
    
    
    
    
    Out[30]:
    [0, 4, 16, 36, 64]
    
    
    In [31]:
    even_square_dict = dict( (x, x**2) for x in range(100) if x % 2 == 0 )
    
    
    
    In [32]:
    even_square_dict[8]
    
    
    
    
    Out[32]:
    64
    
    
    In [33]:
    even_square_dict[28]
    
    
    
    
    Out[33]:
    784