python教程-廖雪峰
To-Do* python -m
from pkg_resources import resource_filename
def require_path(name):
return resource_filename(__name__, name)
*args **kwds
def study(name, age, gender='男', *args, **kwds):
print('Name:%s, Age:%d, Gender:%s'%(name, age, gender))
print(args)
print(kwds)
➜ study('snorlax', 24, '男', 175, 75, math=145, english=135)
Name:snorlax, Age:24, Gender:男
(175, 75)
{'math': 145, 'english': 135}
此外,* 与 ** 还可用于列表、元组、字典的解包:
a = (1,2,3)
print(a) # (1, 2, 3)
print(*a) # 1 2 3
b = [1,2,3]
print(b) # [1, 2, 3]
print(*b) # 1 2 3
c = {'name':'xufive', 'age':51}
print(c) # {'name': 'xufive', 'age': 51}
print(*c) # name age
print('name: {name}, age: {age}'.format(**c)) # name: snorlax, age: 24
三元表达式
>>> y = 5
>>> print('y是一个负数' if y < 0 else 'y是一个非负数')
y是一个非负数
>>> y = 5 >>> x = -1 if y < 0 else 1 >>> x 1列表推导式
>>> a = [1, 2, 3, 4, 5] >>> result = [i*i for i in a] >>> result [1, 4, 9, 16, 25]列表索引
>>> a = [0, 1, 2, 3, 4, 5] >>> b = ['a', 'b'] >>> a[2:2] = b >>> a [0, 1, 'a', 'b', 2, 3, 4, 5] # 2-2=0 在a[2]的位置插入b >>> a[3:6] = b >>> a [0, 1, 'a', 'a', 'b', 4, 5] # 6-3=3 将a[3:6]三个元素替换为blambda函数
>>> a = [{'name':'B', 'age':50}, {'name':'A', 'age':30}, {'name':'C', 'age':40}]
>>> sorted(a, key=lambda x:x['name']) # 按姓名排序
[{'name': 'A', 'age': 30}, {'name': 'B', 'age': 50}, {'name': 'C', 'age': 40}]
>>> sorted(a, key=lambda x:x['age']) # 按年龄排序
[{'name': 'A', 'age': 30}, {'name': 'C', 'age': 40}, {'name': 'B', 'age': 50}]
a = [1,2,3] for item in map(lambda x:x*x, a): print(item, end=', ') 1, 4, 9,yield
def get_square(n): for i in range(n): yield(pow(i,2)) a = get_square(5) # a装饰器for i in a: print(i, end=', ') print('-'*8) for i in a: print(i, end=', ') 0, 1, 4, 9, 16, --------
import time
def timer(func):
def wrapper(*args,**kwds):
t0 = time.time()
func(*args,**kwds)
t1 = time.time()
print('耗时%0.3f'%(t1-t0,))
return wrapper
@timer
def study(delay):
print('函数study开始')
time.sleep(delay)
print('函数study结束')
➜ study(3) 函数do_something开始 函数do_something结束 耗时3.004
timer() 是我们定义的装饰器函数,使用@把它附加在任何一个函数(比如study)定义之前,就等于把新定义的函数,当成了装饰器函数的输入参数。运行 study() 函数,可以理解为执行了timer(study) 。细节虽然复杂,不过这么理解不会偏差太大,且更易于把握装饰器的制造和使用。
assert所谓断言,就是声明表达式的布尔值必须为真的判定,否则将触发 AssertionError 异常。严格来讲,assert是调试手段,不宜使用在生产环境中,但这不影响我们用断言来实现一些特定功能,比如,输入参数的格式、类型验证等。
def i_want_to_sleep(delay):
assert(isinstance(delay, (int,float))), '函数参数必须为整数或浮点数'
print('开始睡觉')
time.sleep(delay)
print('睡醒了')
>>> i_want_to_sleep(1.1)
开始睡觉
睡醒了
>>> i_want_to_sleep(2)
开始睡觉
睡醒了
>>> i_want_to_sleep('2')
Traceback (most recent call last):
File "", line 1, in
i_want_to_sleep('2')
File "", line 2, in i_want_to_sleep
assert(isinstance(delay, (int,float))), '函数参数必须为整数或浮点数'
AssertionError: 函数参数必须为整数或浮点数
Utilities
ARGS
import argparse
parser = argparse.ArgumentParser(description="this is a sample.")
parser.add_argument("-n", "--number", default=233, type=int, help="number value.")
parser.add_argument("-s", "--string", default='hello', type=str, help="string")
parser.add_argument("-t", "--tuple", default=("05", 23), type=tuple, help="tuple")
parser.add_argument("-l", "--list", default=[10, "27"], type=list, help="list")
args = parser.parse_args()
print(args.number, args.string, args.tuple, args.list)
sub-command
# test.py
import argparse
def build_argparser():
parser = argparse.ArgumentParser(prog='PROG')
subparsers = parser.add_subparsers(help='sub-command help', dest = 'action')
#添加子命令
parser_a = subparsers.add_parser('action_a', help='action_a help')
parser_b = subparsers.add_parser('action_b', help='action_b help')
# 设置子命令参数 action_a
parser_a.add_argument("-i", "--i", type=int, help="i int")
parser_a.add_argument("-j", "--j", type=int, help="j int")
# 设置子命令参数 action_b
parser_b.add_argument("-x", "--x", type=int, help="x int")
parser_b.add_argument("-y", "--y", type=int, help="y int")
return parser
args = build_argparser().parse_args()
if args.action == 'action_a':
print(f"{args.i}+{args.j}={args.i+args.j}")
elif args.action == 'action_b':
print(f"{args.x}*{args.y}={args.x*args.y}")
➜ python test.py action_a -i 5 -j 10 5+10=15 ➜ python test.py action_b -x 5 -y 10 5*10=50sys.argv[]
import sys print(sys.argv[:])
➜ python study.py -a 111 -bb two ['study.py', '-a', '111', '-bb', 'two']Write Record File TXT
def list2txt(txt_list, txt_file):
with open(txt_file, mode='w') as f:
for item in txt_list:
f.write(f"{item}n")
def dict2txt(txt_dict, txt_file):
with open(txt_file, 'w') as f:
for i in txt_dict:
print(f"{i} {txt_dict[i]}n", end="")
f.write(f"{i} {txt_dict[i]}n")
def txt2list(txt_file):
txt_list = []
with open(txt_file, "r") as f:
for line in f.readlines():
line = line.strip('n') #去掉列表中每一个元素的换行符
txt_list.append(line)
return txt_list
JSON
def dict2json(json_dict, json_path):
import json
with open(json_path,'w') as f:
f.write(json.dumps(json_dict, ensure_ascii=False, indent=2))
pass
def json2dict(json_path:str):
import json
with open(json_path,'r', encoding='UTF-8') as f:
load_dict = json.load(f)
return load_dict
def view_dict(json_dict:dict):
for key,value in json_dict.items():
print('{key} {value}'.format(key = key, value = value))
if __name__ == "__main__":
json_dict = {}
var = "a"
json_dict[var] = 1 # {'a': 1}
json_dict["b"] = 2 # {'a': 1, 'b': 2}
dict2json(json_dict)
print(json2dict('./output.json'))
× output.json
{
"a": 1,
"b": 2
}
Filename
splitext
>>> import os
>>> a = "1/2/3/4/5/6.txt"
>>> os.path.splitext(a) # 提取扩展名
('1/2/3/4/5/6', '.txt')
>>>
basename
>>> import os >>> a = "1/2/3/4/5/6.txt" # 提取文件名 >>> os.path.basename(a) '6.txt' >>>Win or Linux or OSX
import sys
import platform
print(sys.platform)
def ShowPlatform():
print ("--------------Operation System-----------------------")
print(platform.architecture())
print(platform.platform())
print(platform.system())
# print(platform.uname())
print ("--------------Python Version-------------------------")
print(platform.python_version())
def UsePlatform():
print ("--------------Platform Version-----------------------")
sysstr = platform.system()
if sysstr == "Windows":
print ("Call Windows tasks")
elif sysstr == "Linux":
print ("Call Linux tasks")
else:
print ("Call System tasks: %s" % sysstr)
return sysstr
ShowPlatform()
UsePlatform()
Win10
win32
--------------Operation System-----------------------
('64bit', 'WindowsPE')
Windows-10-10.0.18362-SP0
Windows
--------------Python Version-------------------------
3.7.3
--------------Platform Version-----------------------
Call Windows tasks
Linux
linux
--------------Operation System-----------------------
('64bit', 'ELF')
Linux-5.4.0-40-generic-x86_64-with-glibc2.10
Linux
--------------Python Version-------------------------
3.8.3
--------------Platform Version-----------------------
Call Linux tasks
MacOS
darwin
--------------Operation System-----------------------
('64bit', '')
Darwin-19.6.0-x86_64-i386-64bit
Darwin
--------------Python Version-------------------------
3.7.6
--------------Platform Version-----------------------
Call System tasks: Darwin



