【文件首部/编码声明】
#!/usr/bin/env python# -*- coding:utf-8 -*-
# 声明脚本使用 utf-8 编码是为了在脚本中含有中文时也能被正确解释运行
【注释】
# here we can put a comment, 在井号后添加注释。
【缩进/大小写】
必须使用 4 个空格进行缩进。 python是大小写敏感的语言 。
【普通数据类型】
整数 1,100,-123,0
浮点数 3.14,-9.01,1.2e9,1.2e-5
普通字符串 'Hello\tWorld.\n'
普通字符串 "I'm Chica, how're u?"
'ABCDEFG'[:3] = 'ABC'
'ABCDEFG'[::3] = 'ADG'
多行字符串 '''line1
line2
line3'''
不经转义的原始字符串 r'Hello\tWorld.\n'
不经转义的原始多行字符串 r'''MultilineString'''
Unicode编码的字符串 u'HelloFrog'
二进制字符串 b'HelloBinary'
布尔值 true,false
空值 none
【高级数据类型】
列表 zone = ['China', 'USA', 'England']
zone[0] = 'China'
zone[:1] = ['China']
zone[:2] = ['China', 'USA']
zone[-1] = 'England'
zone[-2:] = ['USA', 'England']
zone[0::2] = ['China', 'England'] # 以 2 为步长从第 0 个元素到最后一个元素进行取值
len(zone) = 3# 列表的值是可修改的
元组 name = ('Zhao', 'Qian', 'Sun')
name[1:] = ('Qian', 'Sun')
只含一个数值元素的元组 num = (1, )
# 元组的值是不可修改的,所以数据更安全
# 但是可以在元组中定义列表,元组中包含的列表的所属元素是可修改的
字典 d = {'Zhao':175, 'Qian':162, 'Sun':170}
d['Zhao'] = 175
if 'Zhao' in d :
# 如果 'Zhao' 被包含在字典中则执行至此
集合 s = set([1, 2, 2, 3, 3, 3])
s = set([1, 2, 3]) # set 会去掉集合中重复的元素
s1 & s2 # 计算交集
s1 | s2 # 计算并集
【杂项】
删除自定义变量myVar globals().pop('myVar')
Windows清屏 os.system('cls') #需要先执行命令import os导入os模块
Linux清屏 os.system('clear')
赋值 =
比较 >,<,==,>=,<=
数值 +,-,*,/,%,**
逻辑 and,or,not
【输入/输出】
【格式化字符串】
print "Hello %s, you're %d years old." %('Jack', 24)
整数 %d
字符串 %s
浮点数 %f
保留2位小数 %.2f
十六进制 %x
百分号 %%
【列表生成式】
【生成器】
1)方法一
2)方法二
# 把函数转换为生成器,在函数中包含 yield 关键字
# yield 关键字会使脚本程序产生一个中断
【常用库函数/方法】
int('123') 将字符串 '123' 转换为整数 123
float('0.5') 将字符串 '0.5' 转换为浮点数 0.5
str(123) 将数值 123 转换为字符串 '123'
unicode(123) 将 123 转换为 unicode 编码格式的值 u'123'
bool(a) 将 a 转换为布尔值。返回值为 true 或 false
ord('A') 将字符转换成对应的 ASCII 码值
chr(65) 将 ASCII 码值转换成对应的字符
abs(-123) 返回数值 -123 的绝对值
cmp(a, b) 比较 a 和 b 的大小。a>b 返回 1,a<b 返回 -1,a==b 返回 0
len('string') 返回字符串的长度 或 返回列表的元素个数。1个汉字的长度是1
sorted([4, 2, 3, 5, 1]) 返回升序排序后的列表 [1, 2, 3, 4, 5]
isinstance(x, str) 判断 x 是否为字符串,返回 true 或 false
isinstance(x, Iterable) 判断 x 是否可迭代,返回 true 或 false
open('./test.txt', 'r') 以只读方式打开文件 ./test.txt
FILE.read() 将文件内容全部读取到内存中
FILE.read(size) 读取 size 字节的文件内容到内存中
FILE.readline() 读取文件中的 1 行到内存
FILE.readlines() 将文件内容全部读取到内存中,并返回一个列表
FILE.write('China1912') 向文件中写入字符串 'China1912'
FILE.close() 关闭文件,释放文件句柄
STR.encode('utf-8') 字符串方法,将 Unicode 编码的字符串 STR 转换为 utf-8 编码
STR.decode('utf-8') 字符串方法,将 utf-8 编码的字符串 STR 转换为 Unicode 编码
STR.replace('a', 'A') 字符串方法,将字符串 STR 中的字符 a 替换为字符 A
LIST.append('France') 列表方法,在列表 LIST 末尾追加元素 France
LIST.insert(1, 'France') 列表方法,在列表 LIST[1] 位置插入元素 France
LIST.pop() 列表方法,返回列表 LIST 的末尾值并将该值从列表中删除
LIST.pop(1) 列表方法,返回 LIST[1] 的值并将其从列表中删除
LIST.sort() 列表方法,返回对元素排序后的列表
DICT.get('Zhao') 字典方法,返回 Zhao 这个键对应的值
DICT.get('Zhao', -1) 字典方法,返回 Zhao 这个键对应的值,若值不存在则返回默认值-1
DICT.pop('Zhao') 字典方法,返回 Zhao 这个键对应的值,并将 Zhao 的键值对从字典中删除
【流程控制】
1)条件语句一
2)条件语句二
3)循环语句一
4)循环语句二
5)循环语句三
6)循环语句四
7)循环语句五
8)循环语句六
9)循环语句七
【函数定义】
1)普通函数
2)空函数
3)可变参数函数一:
4)可变参数函数二:
5)匿名函数
【函数名也是变量】
# 函数名也是变量,可以作为参数传递、赋值、返回
1)赋值到其它变量
2)重定义函数名
3)作为入口参数
4)作为入口参数的经典示例
5)作为返回值
#函数作为返回值时可实现闭包、装饰器
【闭包】
# 闭包的概念比较高级,之后单独开篇文章进行讲解
【模块】
# 脚本 test.py 也可称作模块,将其放置于单独的文件夹 AAA 中
# 同时在文件夹 AAA 里新建一个 __init__.py 脚本,该脚本可以为空
# 此时文件夹就是一个包(Package)
# 模块调用形式为 AAA.test
1)验证模块是否以被导入模式启动
2)第三方库
# 安装命令为 pip install XXX
PIL 图像处理支持库
MySQL-python MySQL支持库
numpy 科学计算支持库
Jinja2 文本模板支持库
【异常处理】
1)普通异常处理
2)错误日志记录
3)自定义抛出异常
4)向上抛出异常
【调试】
1)使用断言替代打印
# 使用 -0 参数关闭脚本中的断言
$python -0 test.py
2)使用 logging 记录异常日志
3)调试器单步调试
# 使用 -m pdb 参数运行脚本
$python -m pdb test.py
# 在 pbd 中,输入命令 l 查看脚本代码,输入 n 执行下一条语句,输入 p <变量名> 查看变量值,输入 q 退出调试工具。
4)设置断点
【文件I/O操作】
1)普通写法
2)简化写法
3)读取文件时解码
【模拟 shell/cmd 操作】
1)os 模块
2)shutil 模块