This is written for beginners.

0. 程序的运行方式

  • 命令行交互 (REPL: Read-Eval-Print-Loop)
  • 脚本文件
  • 1
    
        > python script.py
    
  • iPython 以及 iPython Notebook

1. 简单的单行代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 在命令行交互式运行时,下面的print都不必写。

print 232345  # 整数
print 23.2345  # 浮点数
print 2.12E33  # 科学计数法
print 1.2 + 0.345j  # 复数
print "Hello World!"  # 字符串
print 1, 2.3, 4343, 'adwd3'  # 一下print多个

print 1 + 1  # 代码风格:运算符前后的空格
print 1.23 - 32.4345  # 减法
print 123456 * 789012312**3  # **表示指数运算
print "Hello " + "World!"  # 字符串拼接

print 1 == 1
print 1 == 2
print True == (1 == 1)
print False == (1 == 1)
print (1 < 2) and (1 == 1)
print (1 < 2) or (1 == 1)
print not (1 < 2)

print abs(-123.453)  # 绝对值
print max(4, 5, 2, 1, 6, 3)  # 求最大值
print min(4, 5, 2, 1, 6, 3)  # 求最小值

import math  # 更复杂的数学运算需要专门的包
print math.sin(3.14159)  # 正弦函数

# 或者
from math import *  # 导入数学函数库里的所有函数
print sin(3.14159)  # 正弦函数可以直接使用了
# 坏处:污染名字空间

print [0,1,2,3]  # List
print range(4)   # Also a list
print (0,1,2,3)  # Tuple
print {'email': 'xxyy@gmail.com', 'address': 'AA, MI'}  # Dictionary

2. 一个计算机语言一般包含哪些部分

  1. 基本的语法结构
    • 变量
    • 下划线或字母开头,后面跟任意多个下划线、字母、或数字
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      
      a = 1
      print id(a), id(1)
      
      srwe = 1.23
      b = srwe
      srwe_ = 1.23
      print id(srwe), id(srwe_), id(b)
      
      srwe = 1.2E33
      srwe_ = 1.2E33
      print id(srwe), id(srwe_), srwe == srwe_
      
      _a = 1
      _ab_2 = 'asda'
      
      print id(1.23)
      a = 1.23
      print id(a), id(1.23)
      b = a
      print id(b)
      b = 1.23
      print id(b)
      print id(1.23)
      
      print id(1)
      a = 1
      print id(a), id(1)
      b = a
      print id(b)
      b = 1
      print id(b)
      print id(1)
      
    • Python使用基于缩进(也就是对齐)的语法结构; 用空格,不要用Tab!
    • 条件判断
    • 1
      2
      3
      4
      5
      6
      7
      
      a = 1; b = 2  # 分号只在把两个语句写在同一行的时候需要  
      if a == b:  
          print 'Do something'  
      elif a > b:
          print 'Do nothing'
      else:
          print 'Nothing to do'
      
    • 循环结构
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
      a = [1,2,3]
      for i in a:
          print i*i
      
      for i in 'fjsjk2jri3':
          print i
      
      i = 10
      while i > 0:
          print i
          i = i - 1
      
    • 函数
    • 1
      2
      3
      4
      
      def f(x):
          return x * x * x
      def g(x, y, z):
          return x * y * z
      
  2. 基本数据结构
    • String
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
      print ''
      print ""
      print '' == ""
      s = 'weew1%^$'
      print len(s)
      s = 'weew\n1%\n\n^$'
      print s
      s = r'weew\n1%\n\n^$'
      print s
      s = '你好!'
      print s
      
    • List
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      
      []
      [1,2,3]
      a = [1, 'aasd', 3.14]
      print len(a)
      print a
      a.append('QQQ')
      print a
      a[0] = 233
      print a
      for i in a:
          print i
      for i in xrange(len(a)):
          print a[i]
      
    • Tuple
    • 1
      2
      3
      4
      5
      
      ()
      (1,2,3)
      a = (1, 'aasd', 3.14)
      print a[0], a[1]
      a[0] = 3  # Error!
      
    • Dictionary
    • 1
      2
      3
      4
      5
      
      {}
      {1: 'a', 2: 'b'}
      {(1,2): 'abab', (3,4): '2e22r', 5: '2342fe', 'aa': [1, 2, 3.5]}
      d = {'email': 'xxyy@gmail.com', 'address': 'AA, MI'}
      print d['email'], d['address']
      
    • Set
    • 1
      2
      
      s = {1, 1, 2, 2, 'a', 'qqq', 3.14, 3.14}
      print s
      
    • Class
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      
      class AA():
          def __init__(self):
              self.name = 'AA'
              print 'Class ', self.name, ' is created.'
          def show(self):
              print self.name
          def __del__(self):
              print 'Class ', self.name, ' is destructed.'
      
      a = AA()
      print a.name
      del a
      
  3. 标准库
    1. math
    2. os
    3. glob
    4. sys
    5. time
    6. re
    7. itertools
    8. functools
    9. multiprocessing
    10. subprocess
    11. random
  4. 外部库
    1. numpy
    2. matplotlib
    3. pandas
    4. scikit-learn
    5. PIL
  5. 高级语法结构
    1. map, reduce, filter
    2. decorator
    3. metaclass
    4. 垃圾回收
    5. 如何自己写library
    6. 如何用其它语言(C, C++, Fortran)来扩展Python
    7. 用Python处理Python自身的语法元素

References

  1. The official tutorial
  2. A Byte of Python
  3. Learn Python the Hard Way
  4. Dive Into Python
  5. Python Practice Book
  6. Five mini programming projects for the Python beginner
  7. Think Python: How to Think Like a Computer Scientist
  8. Learning Python for non-developers
  9. Python教程
  10. Python爬虫
  11. Python Style Guide
  12. Python Topics Tree
  13. Lutz, Learning Python
  14. Lutz, Programming Python
  15. Chun, Core Python Programming
  16. Chun, Core Python Applications Programming
  17. Goodrich, Data structures and algorithms in Python
  18. 陈儒, Python源码剖析
  19. 雨痕, Python学习笔记
  20. Bird, Natural Language Processing with Python

Other related material

  1. The Unix Philosophy
  2. How to Build a Blog
  3. TCP-IP协议
  4. 互联网协议入门
  5. HTTP协议