Python基础语法文章导航:
- Python基础(01初识数据类型&变量)
- Python基础(02条件&循环语句)
- Python基础(03字符串格式化&运算符&进制&编码)
- Python基础(04 基础练习题)
- Python数据类型(day05整型&布尔类型&字符串类型)
- Python数据类型(06列表&元组)
- Python数据类型(07集合&字典&浮点型&None)
- Python文件操作01(自动化测试文件相关操作)
目录
前言
学习笔记
一.生成器
生成器是由“函数+yield”关键字创造出来的写法,在特定情况下,用他可以帮助节省内存。
1.生成器函数定义:
函数中有yield存在,这个函数就是生产生成器的函数。
def func():
print(111)
yield 1
2.生成器对象
执行生成器函数func,返回的生成器对象。
注意:执行生成器函数时,函数内部代码不会执行。
def func():
print(111)
yield 1
print(222)
yield 2
print(333)
yield 3
print(444)
data = func()
#执行结果为空
# 执行生成器函数func,返回的生成器对象。
# 注意:执行生成器函数时,函数内部代码不会执行。
3.执行生成器函数中的代码
-
next
-
for(常用)
-
send
生成器的特点:记录在函数中的执行位置,下次执行next时,会从上一次的位置基础上再继续向下执行。
def func():
print(111)
yield 1
print(222)
yield 2
print(333)
yield 3
print(444)
data = func()
v1 = next(data)
print(v1)
v2 = next(data)
print(v2)
v3 = next(data)
print(v3)
# v4 = next(data)
# print(v4) #这两行如果不注释、会报错 结束或中途遇到return,程序爆:StopIteration 错误
#111
# 1
# 222
# 2
# 333
# 3
def func():
print(111)
yield 1
print(222)
yield 2
print(333)
yield 3
print(444)
data = func()
for item in data:
print(item)
# 111
# 1
# 222
# 2
# 333
# 3
# 444
def func():
print(111)
v1 = yield 1
print(v1)
print(222)
v2 = yield 2
print(v2)
print(333)
v3 = yield 3
print(v3)
print(444)
data = func()
n1 = data.send(None)
print(n1)
n2 = data.send(666)
print(n2)
n3 = data.send(777)
print(n3)
n4 = data.send(888)
print(n4)
# 111
# 1
# 666
# 222
# 2
# 777
# 333
# 3
# 888
# 444
# 报异常:StopIteration
二.内置函数
Python内部为我们提供了很多方便的内置函数,在此整理出来36个常用的内置函数。
1. abs,绝对值
v = abs(-10)
print(v)
#10
2.pow,指数
v1 = pow(2,5) # 2的5次方 2**5
print(v1)
#32
3.sum,求和
v1 = sum([-11, 22, 33, 44, 55]) # 可以被迭代-for循环
print(v1)
#143
4.divmod,求商和余数
v1, v2 = divmod(9, 2)
print(v1, v2)
#4 1
5.round,小数点后n位(四舍五入)
v1 = round(4.11786, 2)
print(v1) # 4.12
6.min,最小值
v1 = min(11, 2, 3, 4, 5, 56)
print(v1) # 2
v2 = min([11, 22, 33, 44, 55]) # 迭代的类型(for循环)
print(v2) #11
v3 = min([-11, 2, 33, 44, 55], key=lambda x: abs(x))
print(v3) # 2
7.max,最大值
v1 = max(11, 2, 3, 4, 5, 56)
print(v1) #56
v2 = max([11, 22, 33, 44, 55])
print(v2) #55
v3 = max([-11, 22, 33, 44, 55], key=lambda x: x * 10)
print(v3) # 55
8.all,是否全部为True
v1 = all([11, 22, 44, ""]) # False
9.any,是否存在True
v2 = any([11,22,44,""]) # True
10. 进制转化
-
bin,十进制转二进制
-
oct,十进制转八进制
-
hex,十进制转十六进制
11.ord,获取字符对应的unicode码点(十进制)
v1 = ord("武")
print(v1, hex(v1))
#27494 0x6b66
12. chr,根据码点(十进制)获取对应字符
v1 = chr(27494)
print(v1)
#武
13.数据类型
-
int
-
foat
-
str,unicode编码
-
bytes,utf-8、gbk编码
-
bool
-
list
-
dict
-
tuple
-
set
v1 = "武沛齐" # str类型 v2 = v1.encode('utf-8') # bytes类型 v3 = bytes(v1,encoding="utf-8") # bytes类型
14.其他
-
len
-
print
-
input
-
open
-
type,获取数据类型
v1 = "123" if type(v1) == str: pass else: pass
-
range
-
print(range(10)) #range(0, 10)
-
id
-
hash
v1 = hash("武沛齐") print(v1) #5316533598667125626
-
help,帮助信息
-
pycharm,不用
-
终端,使用
-
-
zip
v1 = [11, 22, 33, 44, 55, 66] v2 = [55, 66, 77, 88] v3 = [10, 20, 30, 40, 50] result = zip(v1, v2, v3) for item in result: print(item) # (11, 55, 10) # (22, 66, 20) # (33, 77, 30) # (44, 88, 40)
-
callable,是否可执行,后面是否可以加括号。
v1 = "武沛齐" v2 = lambda x: x def v3(): pass print(callable(v1)) print(callable(v2)) print(callable(v3)) #False #True #True
-
sorted,排序
v1 = sorted([11,22,33,44,55]) print(v1) # [11, 22, 33, 44, 55]
info = { "wupeiqi": { 'id': 10, 'age': 119 }, "root": { 'id': 20, 'age': 29 }, "seven": { 'id': 9, 'age': 9 }, "admin": { 'id': 11, 'age': 139 }, } result = sorted(info.items(), key=lambda x: x[1]['id']) print(result) # [('seven', {'id': 9, 'age': 9}), ('wupeiqi', {'id': 10, 'age': 119}), ('admin', {'id': 11, 'age': 139}), ('root', {'id': 20, 'age': 29})]
三.推导式
推导式是Python中提供了一个非常方便的功能,可以让我们通过一行代码实现创建list、dict、tuple、set 的同时初始化一些值。
请创建一个列表,并在列表中初始化:0、1、2、3、4、5、6、7、8、9...299 整数元素。
data = []
for i in range(300):
data.append(i)
1.列表
num_list1 = [i for i in range(10)]
num_list2 = [[i, i] for i in range(10)]
num_list3 = [[i, i] for i in range(10) if i > 6]
print(num_list1)
print(num_list2)
print(num_list3)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]
# [[7, 7], [8, 8], [9, 9]]
2. 集合
num_set1 = { i for i in range(10)}
num_set2 = { (i,i,i) for i in range(10)}
num_set3 = { (i,i,i) for i in range(10) if i>3}
print(num_set1)
print(num_set2)
print(num_set3)
# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
# {(6, 6, 6), (5, 5, 5), (9, 9, 9), (2, 2, 2), (0, 0, 0), (7, 7, 7), (3, 3, 3), (4, 4, 4), (8, 8, 8), (1, 1, 1)}
# {(6, 6, 6), (5, 5, 5), (9, 9, 9), (7, 7, 7), (4, 4, 4), (8, 8, 8)}
3.字典
num_dict1 = { i:i for i in range(10)}
num_dict2 = { i:(i,11) for i in range(10)}
num_dict3 = { i:(i,11) for i in range(10) if i>7}
print(num_dict1)
print(num_dict2)
print(num_dict3)
# {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
# {0: (0, 11), 1: (1, 11), 2: (2, 11), 3: (3, 11), 4: (4, 11), 5: (5, 11), 6: (6, 11), 7: (7, 11), 8: (8, 11), 9: (9, 11)}
# {8: (8, 11), 9: (9, 11)}
4.元组,不同于其他类型。
# 不会立即执行内部循环去生成数据,而是得到一个生成器。
data = (i for i in range(10))
print(data)
for item in data:
print(item)
5. 拓展
推导式支持嵌套
data = [ i for i in range(10)]
print(data,end=" ")
print()
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
data1 = [ (i,j) for j in range(5) for i in range(10)]
data1 = []
for i in range(10):
for j in range(5):
data1.append( (i,j) )
data1 = [ [i, j] for j in range(5) for i in range(10)]
print(data1)
#[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2], [0, 3], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [8, 3], [9, 3], [0, 4], [1, 4], [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4], [8, 4], [9, 4]]
# 一副扑克牌
poker_list = [ (color,num) for num in range(1,14) for color in ["红桃", "黑桃", "方片", "梅花"]]
poker_list = [ [color, num] for num in range(1, 14) for color in ["红桃", "黑桃", "方片", "梅花"]]
print(poker_list)
#[['红桃', 1], ['黑桃', 1], ['方片', 1], ['梅花', 1], ['红桃', 2], ['黑桃', 2], ['方片', 2], ['梅花', 2], ['红桃', 3], ['黑桃', 3], ['方片', 3], ['梅花', 3], ['红桃', 4], ['黑桃', 4], ['方片', 4], ['梅花', 4], ['红桃', 5], ['黑桃', 5], ['方片', 5], ['梅花', 5], ['红桃', 6], ['黑桃', 6], ['方片', 6], ['梅花', 6], ['红桃', 7], ['黑桃', 7], ['方片', 7], ['梅花', 7], ['红桃', 8], ['黑桃', 8], ['方片', 8], ['梅花', 8], ['红桃', 9], ['黑桃', 9], ['方片', 9], ['梅花', 9], ['红桃', 10], ['黑桃', 10], ['方片', 10], ['梅花', 10], ['红桃', 11], ['黑桃', 11], ['方片', 11], ['梅花', 11], ['红桃', 12], ['黑桃', 12], ['方片', 12], ['梅花', 12], ['红桃', 13], ['黑桃', 13], ['方片', 13], ['梅花', 13]]
常见面试题1:
def num():
return [lambda x: i * x for i in range(4)]
# 1. num()并获取返回值 [函数,函数,函数,函数] i=3
# 2. for循环返回值
# 3. 返回值的每个元素(2)
result = [m(2) for m in num()] # [6,6,6,6]
print(result)
常见面试题2:
def num():
return (lambda x: i * x for i in range(4))
# 1. num()并获取返回值 生成器对象
# 2. for循环返回值
# 3. 返回值的每个元素(2)
result = [m(2) for m in num()] # [0,2,4,6 ]
print(result)
总结:
生成器、推到式要重点学习。