day3-编码、文件、集合、函数、递归
学习内容:
1. 文件编码
2. 文件
3. 集合
4.函数
5.递归
6.匿名函数
1. 文件编码:
常见的字符串编码有:ASCII 扩展的ASCII Unicode GBK GB2312 GB18030 UTF-8
ASCII: 用于表示英文,使用1个字节,一共可以表示128个字符。
扩展的ASCII: 用于表示更多的欧洲文字,一共可以表示256个字符。
GBK/GB2312/GB18030: 表示汉字。GBK/GB2312表示简体中文,GB18030表示繁体中文。
Unicode: 包含世界上所有的字符,是一个字符集。
UTF-8: 是Unicode字符的实现方式之一,它使用1-4个字符表示一个符号,根据不同的符号而变化字节长度。
字等符间的转换方法:
所有字符需要先解码为Unicode编码,再编码为其它编码方式。
python 2的转码方式:
# -*- coding: GBK -*- # GBK 转 UTF-8 msg = "我是谁!" print(msg.decode(encoding='GBK').encode(encoding='utf-8'))
python 3的转码方式:
python 3默认支持Unicode和GBK。
msg = "中国" # unicode print(msg.encode(encoding="gbk"))
2. 文件:
文件 info_new.txt
line 1
line 2
line 3
line 4
读取文件所有行:
file = open("info_new.txt") for line in file.readlines(): print(line.strip())
file.close()
输出:
line 1
line 2
line 3
line 4
覆盖写:
file = open("info_new.txt","r+") file.write("line 1")
for line in file.readlines():
print(line.strip())
file.close()
输出:
line 1
追加写:
file = open("info_new.txt","a")
file.write("line end")
file.close()
file = open("info_new.txt")
for line in file.readlines():
print(line.strip())
file.close()
输出:
line 1
line 2
line 3
line 4
line end
打开文件,移动n个字符
file = open("info_new.txt") file.seek(7) # 移动7个字符
file.tell() # 获取当前位置: 7 for line in file.readlines(): print(line.strip())
输出:
line 2
line 3
line 4
line end
截取当前位置到文件结尾的N个字符
file = open("info_new.txt","r+") file.truncate(7) for line in file.readlines(): print(line.strip())
输出:
line 1
读取从文件开始的N个字符
file = open("info_new.txt") print(file.readline(4))
输出:
line
3. 集合
a = {1,3,5,7,9} b = {2,4,7,9} print(a.intersection(b)) # 交集,a与b都有 print(a.difference(b)) # 差集, print(a.union(b)) # 并集,a 加 b print(a.symmetric_difference(b)) # 交集的差集 输出: {9, 7} {1, 3, 5} {1, 2, 3, 4, 5, 7, 9} {1, 2, 3, 4, 5}
4. 函数
函数定义、调用、形参、实参:
# 函数定义,形参为str1
def hello(str1): print(str1)
# 函数调用,实参:"hi" "hello world!" hello("hi") hello("hello world!")
函数返回值:
def ret(): names = ["wills","tom","jerry"] return names def print2(lName): print(lName) print2(ret())
输出:
['wills', 'tom', 'jerry']
默认参数:
def info(id,name,sex,age=30,dept="DEV"): msg = ''' id = %s name = %s sex = %s age = %s dept = %s ''' %(id,name,sex,age,dept) print(msg) info("1","wills","male","29","HR") info("2","tom","female","49") info("3","jerry","male")
输出:
id = 1
name = wills
sex = male
age = 29
dept = HR
id = 2
name = tom
sex = female
age = 49
dept = DEV
id = 3
name = jerry
sex = male
age = 30
dept = DEV
位置参数和关键参数:
正常情况下,给函数传递参数需要按顺序,如果不想按顺序,可以用关键参数(指定参数名即可)
def info(id,name,sex,dept="DEV",age=30): msg = ''' id = %s name = %s sex = %s age = %s dept = %s ''' %(id,name,sex,age,dept) print(msg) info("2","tom","female",age=20)
输出:
id = 2
name = tom
sex = female
age = 20
dept = DEV
可变参数(*args):(可以传递多个参数,不固定个数)
使用*args,传递参数时,可以用多个参数,args是一个元组。
def info(id,name,*args): print("id:" + id) print("name:" + name) print(args) info("1","wills","闷骚","奔放","渣渣","单身狗")
输出:
id:1
name:wills
('闷骚', '奔放', '渣渣', '单身狗')
使用**kwargs(传递字典参数)
def info(id,name,*args,**kwargs): print("id:" + id) print("name:" + name) print(args,kwargs) info("1","wills","闷骚","奔放","渣渣","单身狗",userid="9527") 输出: id:1 name:wills ('闷骚', '奔放', '渣渣', '单身狗') {'userid': '9527'}
5. 递归
递归:在函数内部,可以调用其它函数,如果一个函数在内部调用自身,这个函数就是递归函数。
使用递归函数求1到100的所有整数之和:
def calc(n): if n > 1: return n + calc(n-1) else: return n sum = calc(100) print(sum) 输出: 5050
6. 匿名函数
# 普通函数写法 def calc(n): res = n ** 2 return res print(calc(100)) # 匿名函数写法 calc2 = lambda n:n**2 print(calc2(10)) 输出: 10000 100