Beginning Python --- Lesson 0
This is written for beginners.
0. 程序的运行方式
- 命令行交互 (REPL: Read-Eval-Print-Loop)
- 脚本文件
- iPython 以及 iPython Notebook
1
> python script.py
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. 一个计算机语言一般包含哪些部分
- 基本的语法结构
- 变量 下划线或字母开头,后面跟任意多个下划线、字母、或数字
- Python使用基于缩进(也就是对齐)的语法结构; 用空格,不要用Tab!
- 条件判断
- 循环结构
- 函数
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)
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
- 基本数据结构
- String
- List
- Tuple
- Dictionary
- Set
- Class
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
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]
1 2 3 4 5
() (1,2,3) a = (1, 'aasd', 3.14) print a[0], a[1] a[0] = 3 # Error!
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']
1 2
s = {1, 1, 2, 2, 'a', 'qqq', 3.14, 3.14} print s
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
- 标准库
- math
- os
- glob
- sys
- time
- re
- itertools
- functools
- multiprocessing
- subprocess
- random
- 外部库
- numpy
- matplotlib
- pandas
- scikit-learn
- PIL
- 高级语法结构
- map, reduce, filter
- decorator
- metaclass
- 垃圾回收
- 如何自己写library
- 如何用其它语言(C, C++, Fortran)来扩展Python
- 用Python处理Python自身的语法元素
References
- The official tutorial
- A Byte of Python
- Learn Python the Hard Way
- Dive Into Python
- Python Practice Book
- Five mini programming projects for the Python beginner
- Think Python: How to Think Like a Computer Scientist
- Learning Python for non-developers
- Python教程
- Python爬虫
- Python Style Guide
- Python Topics Tree
- Lutz, Learning Python
- Lutz, Programming Python
- Chun, Core Python Programming
- Chun, Core Python Applications Programming
- Goodrich, Data structures and algorithms in Python
- 陈儒, Python源码剖析
- 雨痕, Python学习笔记
- Bird, Natural Language Processing with Python
Other related material