1 课前甜点
- Python 代码一般比 C 系语言短的缘由
- 高级数据类型容许在一个表达式中表示复杂的操做
- 代码块的划分是按照缩进
- 不须要预约义变量
2 使用 Python 解释器
2.1 调用解释器
- EOF unix C-d windows C-z
- 执行 Python 代码 python -c command [arg]
- 把 Python 模块当脚本使用 python -m module [arg]
- 执行脚本后进入交互模式 python -i 1.py
2.1.1 传入参数
- sys.argv sys.argv[0] 是程序名
2.1.2 交互模式
2.2 解释器的运行环境
2.2.1 源文件的字符编码
- 默认编码就是 utf-8 必须放在第一行或者 UNIX “shebang” 后
3 Python 的非正式介绍
- 注释 #
3.1 Python 做为计算器使用
3.1.1 数字
- 除法 / 永远返回浮点数类型 // floor division
- 乘方 **
- 交互模式下,上一次被执行显示的表达式的值会被保存在 _
- Python 内置对复数的支持 3+5j
3.1.2 字符串
- 原始字符串
- 三重引号会自动包含换行,能够在行尾加反斜杠防止换行
- 相邻的两个字符串字面值会自动链接到一块儿
- 经过索引取得的单个字符是一个长度为一的字符串
- 切片中越界索引会被自动处理
- Python 中字符串是不可修改的,若是须要一个不一样的字符串,应当新建一个 a = “heheda” b = a[:-1] + “b”
3.1.3 列表
- 列表的切片都返回一个新列表
- 列表支持使用 + 拼接其余列表
-
能够给列表切片赋值,这样能够改变列表大小,甚至清空整个列表python
a = [1, 2, 3, 4] a[1:3] = [4, 5] a[:] = []
3.2 走向编程的第一步
3.2.1 多重赋值
3.2.2 print
print 会在多个参数间加空格能够传入 end 参数指定结尾替换换行的字符串sql
4 其余流程控制工具
4.1 if 语句
- 输入 aa = input(“input: ”)
- if if elif else
4.2 for 语句
- Python 中的 for 是对任意序列进行迭代
-
若是在循环内须要修改序列中的值,推荐先拷贝一份副本shell
words = ['aaa', 'bbb', 'ccccccc'] for w in words[:]: if len(w) > 5: words.insert(0, w)
4.3 range() 函数
- range(0, 10, 3) 0, 3, 6, 9
-
以序列的索引迭代编程
a = ['a', 'b', 'c'] for i in range(len(a)): print(i, a[i]) for i, v in enumerate(a): print(i, v)
- range() 返回的不是列表,是一个可迭代对象
4.4 break 和 continue 语句,以及循环中的 else 子句
- else 子句 else 子句会在未执行 break,循环结束时执行
4.5 pass 语句
- pass 一般用于建立最小的类 class MyEmptyClass: pass
- pass 也用于写代码时做为函数或条件子句体的占位符,保持在更抽象的层次上思考
4.6 定义函数
- 文档字符串
- 全局变量和外部函数的变量能够被引用,可是不能在函数内部直接赋值
- 函数的重命名机制
- 没有 return, 函数会返回 None
- 列表方法 append() 比 + 效率高
4.7 函数定义的更多形式
4.7.1 参数默认值
- in 测试一个序列是否包含某个值
- 默认值时在定义过程当中在函数定义处计算的,而不是调用时
-
默认值只执行一次,若是是可变对象,好比列表,就能够在后续调用之间共享默认值相似于 c 中的静态变量json
def f(a, L=[]): print(a) L.append(a) print(L) f(1) f(2) f(3) f(4)
4.7.2 关键字参数
- 关键字参数必须在位置参数后
- 形参 * **
4.7.3 任意的参数列表
- 任意的参数列表 *args 通常位于形式参数列表的末尾,只有关键字参数能出如今其后
4.7.4 解包参数列表
- 使用* 从列表或元组中解包参数 args = [3, 6] range(*args)
- 使用** 从字典解包参数
4.7.5 Lambda 表达式
-
闭包windows
def make_incrementor(n): return lambda x: x + n f = make_incrementor(33) f(0)
-
传递一个函数做为参数api
pairs = [(1, 'one'), (2, 'two'), (3, 'three')] pairs.sort(key=lambda pair: pair[1])
4.7.6 文档字符串
- 一些约定第一行简要描述对像的目的,大写字母开头,句点结尾第二行空白,后面几行是一个或多个段落,描述调用约定,反作用等
4.7.7 函数标注
-
标注不会对代码有实质影响,只是给人看
def f(ham: str, eggs: str = "eggs") -> str: print(f.__annotations__) return ham + ' and ' + eggs
4.8 小插曲: 编码风格
- 使用 4 空格缩进,而不是制表符
- 一行不超过 79 个字符(能够在较大显示器并排放置多个代码文件)
- 使用空行分割函数和类,以及函数内的较大的代码块
- 尽可能把注释放到单独的一行
- 使用文档字符串
- 在运算符先后和逗号后使用空格
- 函数和类命名应使用一致规则类 UpperCamelCase 函数 lowercasewithunderscores
- 通常不另外设置代码的编码
- 不要在标识符中使用非 ASCII 字符
5 数据结构
5.1 列表的更多特性
- 列表对象方法的清单 list.append(x) list.extend(iterable) list.insert(i, x) list.remove(x) list.pop([i]) list.clear() list.index(x[, start[, end]]) list.count(x) list.sort(key=None, reverse=False) list.reverse() list.copy()
5.1.1 列表做为栈使用
- list.append(x) list.pop()
5.1.2 列表做为队列使用
-
在列表开头插入或弹出元素很慢
from collections import deque queue = deque(['aaa', 'bbb', 'ccc']) queue.append('ddd') queue.popleft()
5.1.3 列表推导式
- for 循环后,循环变量还存在
- 列表推导式中,for 和 if 顺序和展开形式的顺序是一致的
- 若是表达式是一个元组,须要加括号
5.1.4 嵌套的列表推导式
a = [1, 2, 3] b = [4, 5, 6] print(list(zip(a, b)))
5.2 del 语句
- del 能够删除指定索引的元素
- del 能够删除切片指定的范围
- del a[:]
- del a
5.3 元组和序列
- 元组一般包含不一样种类的元素,列表的元素通常是同种类型
- 空元组能够直接被一对圆括号建立
- 元组输入时圆括号无关紧要
5.4 集合
- set()
- 集合操做 a | b a - b a & b a ^ b
- 集合推导式 a = {x for x in ’abcdedd’ if x not in ’abc’}
5.5 字典
- 只要不包含不可变对象,均可以做为键
5.6 循环的技巧
- 字典循环时,能够用 items() 方法把关键字和值同时取出
- 序列循环时,能够用 enumerate()函数将索引和值同时取出
- 要同时在多个序列中循环时,能够用 zip()函数
- 若是要逆向一个序列,可使用 reversed()函数
- sorted()函数能够返回一个新的排好序的序列
- 在循环时想修改列表内容,最好建立新列表
5.7 深刻条件控制
- in 和 not in
- is 和 is not 比较是否是同一个对象
- 比较操做能够传递
5.8 比较序列和其余类型
- 序列也能够比较
6 模块
- 若是常用某个导入的函数,能够赋值给一个局部变量重命名
6.1 更多有关模块的信息
-
几种导入方式
from fibo import fib, fib2 from fibo import * import fibo as fib from fibo import fib as fibonacci
- 每一个模块在每一个解释器会话中只被导入一次,若是更改了模块,要从新启动解释器或 import importlib importlib.reload(module)
6.1.1 以脚本的方式执行模块
- 若是直接执行脚本,_name__ 被赋值为_main__
6.1.2 模块搜索路径
- 搜索路径
- 内置模块
- sys.path 给出的目录列表
- 包含输入脚本的目录
- PYTHONPATH
- 取决于安装的默认设置
6.1.3 “编译过的”Python 文件
- python3 会把 pyc 统一放在_pycache_下 python2 是放在源文件同目录下
- pyc 和 py 文件运行速度是同样的,只是 pyc 的载入速度更快
6.2 标准模块
- sys.ps1 sys.ps2 用于定义交互模式下的提示符
6.3 dir() 函数
- dir 用于查找模块定义的名称
- 若是没有参数,dir 会列出当前定义的名称
- dir 不会列出内置函数和变量,他们定义在标准模块 builtins
6.4 包
- 包是一种经过用“带点号的模块名”来构造 Python 模块命名空间的方法
- _init_.py 中能够执行包的初始化代码或设置_all__ 变量
- 一些导入方式
- 导入模块 import a.b.c 使用时要使用全名 from a.b import c 能够直接用 c from a.b.c import d 能够直接用 d
6.4.1 从包中导入*
- 导入一个包时会参考它 init.py 内_all__ 列表的内容
6.4.2 子包参考
- 相对导入 from . improt echo from .. import formats from ..filters import equalizer
- 相对导入时基于当前模块的名称进行导入的,因此主模块必须用绝对导入
6.4.3 多个目录中的包
7 输入输出
7.1 更漂亮的输出格式
- 一些格式化输出方式 aa = ’a test’ b = f’hehe {aa}’ ’{gg}’.format(aa)
- str() 返回人类可读的值的表示
- repr() 生成解释器可读的表示
7.1.1 格式化字符串文字
- 一些示例 print(f’value {math.pi:.3f}.’) print(f’{name:10} {num:10d}’) print(f’{animal!r}’) # !a ascii() !s str() !r repr()
7.1.2 字符串的 format() 方法
7.1.3 手动格式化字符串
- rjust() ljust() center()
7.1.4 旧的字符串格式化方法
- %
7.2 读写文件
- 最好使用 with
7.2.1 文件对象的方法
- 经常使用方法列表 f.read() f.readline() f.write() f.tell() f.seek(5) f.seek(-3,2) # 0 start 1 current 2 end
- 遍历行 for line in f: pass
7.2.2 使用 json 保存结构化数据
- 经常使用方法 jons.dumps([1, ’a’, 2]) json.dump(x, f) x = json.load(f)
8 错误和异常
8.1 语法错误
8.2 异常
8.3 处理异常
- else 子句必须放在 except 后
8.4 抛出异常
- 在 except 处理时能够 raise 抛出异常
8.5 用户自定义异常
- 自定义异常通常只是提供一些属性
8.6 定义清理操做
- finally 子句
8.7 预约义的清理操做
- with 语句
9 类
9.1 名称和对象
- Python 中,多个变量能够绑定到一个对象,对于可变对象,表现得相似于指针
9.2 Python 做用域和命名空间
9.2.1 做用域和命名空间示例
-
关于 nonlocal global 的用法
def do_local(): spam = "local spam" def do_nonlocal(): nonlocal spam spam = "nonlocal spam" def do_global(): global spam spam = "global spam" spam = "test spam" do_local() print("After local assignment:", spam) do_nonlocal() print("After nonlocal assignment:", spam) do_global() print("After global assignment:", spam) scope_test() print("In global scope:", spam)
输出结果 After local assignment: test spam After nonlocal assignment: nonlocal spam After global assignment: nonlocal spam In global scope: global spam
9.3 初探类
9.3.1 类定义语法
9.3.2 类对象
- 类对象支持两种操做: 属性引用和实例化
- _doc__ 文档字符串
9.3.3 实例对象
- 实例对象的惟一操做是属性引用
- 两种属性名称: 数据属性和方法
- 数据属性不须要声明,在第一次赋值时产生
9.3.4 方法对象
-
区分方法对象和函数对象
class TestClass: def kk(self): print('aaa') bb = TestClass() bb.kk() TestClass.kk(bb)
9.3.5 类和实例变量
9.4 补充说明
9.5 继承
- isinstance(obj, int)
- issubclass(bool, int)
9.5.1 多重继承
- 搜索的顺序是深度优先,从左到右
9.6 私有变量
- 经过约定,下划线开头的名称
9.7 杂项说明
-
定义空类,简单实现将命名数据捆绑在一块儿
class Employee: pass johb = Employee() john.name = "peter" john.dept = "computer lab"
- 实例方法对象具备的属性 m._self__ 带有 m 方法的实例对象 m._func__ 该方法对应的函数对象
9.8 迭代器
- for 的机制
- for 会用 iter() 函数调用容器内的_iter_()方法
- 此方法会返回一个定义了_next_()方法的对象
- 调用 next() 函数,会调用上一步返回对象的_next_()方法
- 当元素用尽时,_next_()会引起 StopIteration 异常
- 给本身的类添加迭代器行为
- 定义_iter__ 方法,返回 self
-
定义_next__ 方法,返回一个改变引用所指的对象,同时设置条件,遍历完 raise StopIteration
class Reverse: def __init__(self, data): self.data = data self.index = len(data) def __iter__(self): return self def __next__(self): if self.index == 0: raise StopIteration self.index = self.index - 1 return self.data[self.index]
9.9 生成器
- 写法相似标准函数,可是返回不用 return,用 yield
- 生成器会自动建立_iter_()和_next_(),生成器终结时会自动引起 StopIteration
9.10 生成器表达式
- 生成器表达式被设计用于生成器将当即被外层函数所使用的状况 sum(i*i for i in range(10))
10 标准库简介
10.1 操做系统接口
- import os os.getcwd() os.chdir() os.system
- 对于平常文件和目录管理任务,shutil 模块提供了更易于使用的更高级模块 shutil.copyfile() shutil.move()
10.2 文件通配符
- import glob 提供了在一个目录中使用通配符搜索文件列表的函数 glob.glob(’*.py’)
10.3 命令行参数
- import sys print(sys.argv)
-
argparse
import argparse from getpass import getuser parser = argparse.ArgumentParser(description='An argparse example.') parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.') parser.add_argument('--verbose', '-v', action='count') args = parser.parse_args() greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3] print(f'{greeting}, {args.name}') if not args.verbose: print('Try running this again with multiple "-v" flags!')
10.4 错误输出重定向和程序终止
- sys.std sys.stdin sys.stdout sys.stderr.write(’wrong’)
- 终止脚本 sys.exit()
10.5 字符串模式匹配
- import re re.findall(r’\bf[a-z]*’, ’which foot or hand fell fastest’) re.sub(r’(\b[a-z]+) \1’, r’\1’, ’cat in the the hat’) ’cat in the hat’
- 当只须要简单的功能时,首选字符串方法
10.6 数学
- math 提供了对浮点数的低层 C 库函数的访问
- import math math.cos(math.pi / 4) math.log(1024, 2)
- random random.choice([’a’, ’b’, ’c’]) random.sample(range(100), 10) # 选 10 个样本出来 random.random() # 0 到 1 的随机数 random.randrange(5) # 从 range(5) 选
- import statistics statistics.mean() # 平均值 statistics.median() # 中位数 statistics.variance() # 方差
10.7 互联网访问
-
urllib.request
from urllib.request import urlopen with urlopen('http://www.baidu.com') as response: for line in response: line = line.decode('utf-8') if 'EST' in line or 'EDT' in line: print(line)
-
smtplib
import smtplib server = smtplib.SMTP('localhost') server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org From: soothsayer@example.org Beware the Ides of March. """) server.quit()
10.8 日期和时间
-
datetime
# dates are easily constructed and formatted from datetime import date now = date.today() now # datetime.date(2003, 12, 2) now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.") # '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.' # dates support calendar arithmetic birthday = date(1964, 7, 31) age = now - birthday age.days # 14368
10.9 数据压缩
-
zlib
import zlib s = b'witch which has which witches wrist watch' len(s) t = zlib.compress(s) len(t) zlib.decompress(t) zlib.crc32(s)
10.10 性能测量
- timeit 快速测试小段代码
>>> from timeit import Timer >>> Timer(’t=a; a=b; b=t’, ’a=1; b=2’).timeit() 0.57535828626024577 >>> Timer(’a,b = b,a’, ’a=1; b=2’).timeit() 0.54962537085770791
- Profile
import cProfile, pstats, io from pstats import SortKey import hashlib import time pr = cProfile.Profile() pr.enable() data = "你好" for i in range(10000): m = hashlib.md5(data.encode("gb2312")) time.sleep(2) pr.disable() s = io.StringIO() sortby = SortKey.CUMULATIVE ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print(s.getvalue())
10.11 质量控制
-
doctests 能够验证文档字符串中嵌入的测试
def average(values): """Computes the arithmetic mean of a list of numbers. >>> print(average([20, 30, 70])) 40.0 """ return sum(values) / len(values) import doctest doctest.testmod() # automatically validate the embedded tests
-
unittest
import unittest class TestStatisticalFunctions(unittest.TestCase): def test_average(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 1), 4.3) with self.assertRaises(ZeroDivisionError): average([]) with self.assertRaises(TypeError): average(20, 30, 70) unittest.main() # Calling from the command line invokes all tests
10.12 自带电池
- xmlrpc.client xmlrpc.server
- json
- csv
- xml.etree.ElementTree xml.dom xml.sax
- sqlite3
- gettext locale codecs
11 标准库简介 – 第二部分
- 本部分介绍的是专业编程须要的更高级的模块,不多用在小脚本中
11.1 格式化输出
- reprlib 用于缩略显示大型或深层嵌套的容器对象 >>> reprlib.repr(set(’supercalifragilisticexpialidocious’))
“{‘a’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, …}”
- pprint 美化输出版 print
- textwrap 格式化文本段落,适应给定的屏幕宽度 print(textwrap.fill(doc, width=40))
11.2 模板
- from string import Template
>>> t = Template(’${village}folk send $$10 to $cause.’) >>> t.substitute(village=’Nottingham’, cause=’the ditch fund’) ’Nottinghamfolk send $10 to the ditch fund.’
11.3 使用二进制数据记录格式
- struct 模块的 pack() unpack()
11.4 多线程
- threading
11.5 日志记录
- logging
11.6 弱引用
- weakref 能够没必要建立引用就能跟踪对象
11.7 用于操做列表的工具
- from array import array
- from collections import deque
- import bitset
- from heapq import heapify, heappop heappush
11.8 十进制浮点运算
- from decimal import * 能够提供足够的精度,也能知足浮点数的相等性
12 虚拟环境和包
12.1 概述
12.2 建立虚拟环境
- venv
12.3 使用 pip 管理包
- pip 经常使用命令 pip search xxx pip install novas pip install requests==2.6.0 pip install –upgrade requests pip show requests pip list pip freeze > requirements.txt pip install -r requirements.txt
13 接下来?
14 交互式编辑和编辑历史
14.1 Tab 补全和编辑历史
- python3 解释器已经有 tab 补全了
- 默认配置,编辑历史会被保存在 home 目录 .pythonhistory
14.2 默认交互式解释器的替代品
- ipython