first commit

This commit is contained in:
2025-08-27 14:39:37 +08:00
commit 81ebee9cf3
97 changed files with 14191 additions and 0 deletions

View File

@@ -0,0 +1,535 @@
# 内置函数与匿名函数
## 内置函数
截止到python版本3.6.2现在python一共为我们提供了**68个内置函数。**
| 内置函数 | | | | |
| :------------ | :---------- | :----------- | :--------- | :------------- |
| abs() | dict() | help() | min() | setattr() |
| all() | dir() | hex() | next() | slice() |
| any() | divmod() | id() | object() | sorted() |
| ascii() | enumerate() | input() | oct() | staticmethod() |
| bin() | eval() | int() | open() | str() |
| bool() | exec() | isinstance() | ord() | sum() |
| bytearray() | filter() | issubclass() | pow() | super() |
| bytes() | float() | iter() | print() | tuple() |
| callable() | format() | len() | property() | type() |
| chr() | frozenset() | list() | range() | vars() |
| classmethod() | getattr() | locals() | repr() | zip() |
| compile() | globals() | map() | reversed() | __import__() |
| complex() | hasattr() | max() | round() | |
| delattr() | hash() | memoryview() | set() | |
### 作用域相关
* locals :函数会以字典的类型返回当前位置的全部局部变量。
* globals函数以字典的类型返回全部全局变量。
```python
a = 1
b = 2
print(locals())
print(globals())
# 这两个一样,因为是在全局执行的
def func(argv):
c = 2
print(locals())
print(globals())
func(3)
```
### 其他相关
#### 字符串类型代码的执行 evalexeccomplie
* eval执行字符串类型的代码并返回最终结果。
```python
ret = eval('2 + 2')
print(ret)
n = 20
ret = eval('n + 23')
print(ret)
eval('print("Hello world")')
```
* exec:执行字符串类型的代码。
```python
s = '''
for i in range(5):
print(i)
'''
exec(s)
```
compile:将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。
1. 参数source字符串。即需要动态执行的代码段。
2. 参数 filename代码文件名称如果不是从文件读取代码则传递一些可辨认的值。当传入了source参数时filename参数传入空字符即可。
3. 参数model指定编译代码的种类可以指定为 exec,eval,single。当source中包含流程语句时model应指定为exec当source中只包含一个简单的求值表达式model应指定为eval当source中包含了交互式命令语句model应指定为'single'。
```python
# 流程语句使用exec
code1 = 'for i in range(5): print(i)'
compile1 = compile(code1,'','exec')
exec(compile1)
# 简单求值表达式用eval
code2 = '1 + 2 + 3'
compile2 = compile(code2,'','eval')
eval(compile2)
# 交互语句用single
code3 = 'name = input("please input you name: ")'
compile3 = compile(code3,'','single')
exec(compile3)
print(name)
```
有返回值的字符串形式的代码用eval没有返回值的字符串形式的代码用exec一般不用compile。
#### 输入输出相关 inputprint
* input:函数接受一个标准输入数据,返回为 string 类型。
* print:打印输出。
```python
''' 源码分析
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
file: 默认是输出到屏幕,如果设置为文件句柄,输出到文件
sep: 打印多个值之间的分隔符,默认为空格
end: 每一次打印的结尾,默认为换行符
flush: 立即把内容输出到流文件,不作缓存
"""
'''
print(11,22,33,sep='*')
print(11,22,33,end='')
print(44,55)
with open('log','w',encoding='utf-8') as f:
print('写入文件',file=f,flush=True)
```
#### 内存相关 hash id
* hash获取一个对象可哈希对象intstrBooltuple的哈希值。
```python
print(hash(12322))
print(hash('123'))
print(hash('arg'))
print(hash('aaron'))
print(hash(True))
print(hash(False))
print(hash((1,2,3)))
```
* id:用于获取对象的内存地址。
```python
print(id('abc'))
print(id('123'))
```
#### 文件操作相关
* open函数用于打开一个文件创建一个 file 对象,相关的方法才可以调用它进行读写。
#### 模块相关`__import__`
* `__import__`:函数用于动态加载类和函数 。
#### 帮助
* help函数用于查看函数或模块用途的详细说明。
```python
print(help(print))
```
#### 调用相关
* callable函数用于检查一个对象是否是可调用的。如果返回Trueobject仍然可能调用失败但如果返回- False调用对象ojbect绝对不会成功。
```python
print(callable(0))
print(callable('hello'))
def demo1(a, b):
return a + b
print(callable(demo1))
class Demo2:
def test1(self):
return 0
print(callable(Demo2))
a = Demo2()
print(callable(a))
# 没有实现 __call__, 返回 False
```
#### 查看内置属性
* dir函数不带参数时返回当前范围内的变量、方法和定义的类型列表带参数时返回参数的属性、方法列表。如果参数包含方法`__dir__()`,该方法将被调用。如果参数不包含`__dir__()`,该方法将最大限度地收集参数信息。
```python
print(dir()) # 获得当前模块的属性列表
print(dir([ ])) # 查看列表的方法
```
### 迭代器生成器相关
* range函数可创建一个整数对象一般用在 for 循环中。
* next内部实际使用了`__next__`方法,返回迭代器的下一个项目。
```python
# 首先获得Iterator对象:
it = iter([1,2,3,4,5,6])
# 循环
while True:
try:
# 获得下一个值
x = next(it)
print(x)
except StopIteration: # 遇到StopIteration就退出循环
break
```
* iter函数用来生成迭代器讲一个可迭代对象生成迭代器
```python
from collections import Iterable
from collections import Iterator
l = [1,2,3,4] # 可迭代对象,但不是迭代器
print(isinstance(l,Iterable))
print(isinstance(l,Iterator))
l1 = iter(l) # 从一个可迭代对象生成迭代器
print(isinstance(l1,Iterable))
print(isinstance(l1,Iterator))
```
### 基础数据类型相关
#### 数字相关14个
数据类型4个
* bool :用于将给定参数转换为布尔类型,如果没有参数,返回 False。
* int函数用于将一个字符串或数字转换为整型。
```python
print(int())
print(int('12'))
print(int(3.6))
print(int('0100',base=2)) # 将2进制的 0100 转化成十进制。结果为 4
```
* float函数用于将整数和字符串转换成浮点数。
* complex函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。。
```python
print(complex(1,2))
print(complex(1))
print(complex("1"))
print(complex("1+2j"))
```
进制转换3个
* bin将十进制转换成二进制并返回。
* oct将十进制转化成八进制字符串并返回。
* hex将十进制转化成十六进制字符串并返回。
```python
print(bin(10),type(bin(10)))
print(oct(10),type(oct(10)))
print(hex(10),type(hex(10)))
```
数学运算7
* abs函数返回数字的绝对值。
* divmod计算除数与被除数的结果返回一个包含商和余数的元组(a // b, a % b)。
* round保留浮点数的小数位数默认保留整数。 -pow函数是计算x的y次方如果z在存在则再对结果进行取模其结果等效于pow(x,y) %z
```python
print(abs(-5)) # 5
print(divmod(7,2)) # (3, 1)
print(round(7/3,2)) # 2.33
print(round(7/3)) # 2
print(round(3.32567,3)) # 3.326
print(pow(2,3)) # 8
print(pow(2,3,3)) # 2
```
* sum对可迭代对象进行求和计算可设置初始值
* min返回可迭代对象的最小值可加keykey为函数名通过函数的规则返回最小值
* max返回可迭代对象的最大值可加keykey为函数名通过函数的规则返回最大值
```python
print(sum([1,2,3]))
print(sum([1,2,3],100))
print(min([1,2,3]))
ret = min([1,2,3,-10],key=abs)
print(ret)
dic = {'a':3,'b':2,'c':1}
print(min(dic,key=lambda x:dic[x]))
# x为dic的keylambda的返回值即dic的值进行比较返回最小的值对应的键
print(max([1,2,3]))
ret = max([1,2,3,-10],key=abs)
print(ret)
dic = {'a':3,'b':2,'c':1}
print(max(dic,key=lambda x:dic[x]))
```
#### 数据结构相关24个
列表和元祖2个
* list将一个可迭代对象转化成列表如果是字典默认将key作为列表的元素
* tuple将一个可迭代对象转化成元祖如果是字典默认将key作为元祖的元素
```python
l = list((1,2,3))
print(l)
l = list({1,2,3})
print(l)
l = list({'k1':1,'k2':2})
print(l)
tu = tuple((1,2,3))
print(tu)
tu = tuple([1,2,3])
print(tu)
tu = tuple({'k1':1,'k2':2})
print(tu)
```
相关内置函数2个
* reversed将一个序列翻转并返回此翻转序列的迭代器。
* slice构造一个切片对象用于列表的切片。
```python
ite = reversed(['a',2,4,'f',12,6])
for i in ite:
print(i)
l = ['a','b','c','d','e','f','g']
sli = slice(3)
print(l[sli])
sli = slice(0,7,2)
print(l[sli])
```
字符串相关9
* str将数据转化成字符串。
* format:与具体数据相关,用于计算各种小数,精算等。
```python
# 字符串可以提供的参数,指定对齐方式,<是左对齐, >是右对齐,^是居中对齐
print(format('test','<20'))
print(format('test','>20'))
print(format('test','^20'))
# 整形数值可以提供的参数有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None
print(format(192,'b')) # 转换为二进制
print(format(97,'c')) # 转换unicode成字符
print(format(11,'d')) # 转换成10进制
print(format(11,'o')) # 转换为8进制
print(format(11,'x')) # 转换为16进制小写字母表示
print(format(11,'X')) # 转换为16进制大写字母表示
print(format(11,'n')) # 和d一样
print(format(11)) # 和d一样
# 浮点数可以提供的参数有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None
print(format(314159265,'e')) # 科学计数法默认保留6位小数
print(format(314159265,'0.2e')) # 科学计数法保留2位小数
print(format(314159265,'0.2E')) # 科学计数法保留2位小数,大写E
print(format(3.14159265,'f')) # 小数点计数法默认保留6位小数
print(format(3.14159265,'0.10f')) # 小数点计数法保留10位小数
print(format(3.14e+10000,'F')) # 小数点计数法,无穷大转换成大小字母
# g的格式化比较特殊假设p为格式中指定的保留小数位数先尝试采用科学计数法格式化得到幂指数exp如果-4<=exp<p则采用小数计数法并保留p-1-exp位小数否则按小数计数法计数并按p-1保留小数位数
print(format(0.00003141566,'.1g'))
# p=1,exp=-5 ==》 -4<=exp<p不成立按科学计数法计数保留0位小数点
print(format(0.00003141566,'.2g'))
# p=2,exp=-5 ==》 -4<=exp<p不成立按科学计数法计数保留1位小数点
print(format(3.1415926777,'.1g'))
# p=1,exp=0 ==》 -4<=exp<p成立按小数计数法计数保留0位小数点
print(format(3.1415926777,'.2g'))
# p=2,exp=0 ==》 -4<=exp<p成立按小数计数法计数保留1位小数点
print(format(3141.5926777,'.2g'))
# p=2,exp=3 ==》 -4<=exp<p不成立按科学计数法计数保留1位小数点
print(format(0.00003141566,'.1n')) # 和g相同
print(format(0.00003141566)) # 和g相同
```
* bytes用于不同编码之间的转化。
```python
s = '你好'
bs = s.encode('utf-8')
print(bs)
s1 = bs.decode('utf-8')
print(s1)
bs = bytes(s,encoding='utf-8')
print(bs)
b = '你好'.encode('gbk')
b1 = b.decode('gbk')
print(b1.encode('utf-8'))
```
* bytearry返回一个新字节数组。这个数组里的元素是可变的并且每个元素的值范围: 0 <= x < 256
```python
ret = bytearray('aaron',encoding='utf-8')
print(id(ret))
print(ret)
print(ret[0])
ret[0] = 65
print(ret)
print(id(ret))
```
* memoryview: 内存查看对象是指对支持缓冲区协议的数据进行包装在不需要复制对象基础上允许Python代码访问
```python
ret = memoryview(bytes('你好',encoding='utf-8'))
print(len(ret))
print(ret)
print(bytes(ret[:3]).decode('utf-8'))
print(bytes(ret[3:]).decode('utf-8'))
```
* ord:输入字符找该字符编码的位置
* chr:输入位置数字找出其对应的字符
* ascii:是ascii码中的返回该值不是就返回/u
```python
# ord 输入字符找该字符编码的位置
print(ord('a'))
print(ord('中'))
# chr 输入位置数字找出其对应的字符
print(chr(97))
print(chr(20013))
# 是ascii码中的返回该值不是就返回/u...
print(ascii('a'))
print(ascii('中'))
```
* repr:返回一个对象的string形式
```python
name = 'aaron'
print('Hello %r'%name)
str1 = '{"name":"aaron"}'
print(repr(str1))
print(str1)
```
数据集合3个
* dict创建一个字典
* set创建一个集合
* frozenset返回一个冻结的集合冻结后集合不能再添加或删除任何元素
相关内置函数8个
* len:返回一个对象中元素的个数
* sorted对所有可迭代的对象进行排序操作
```python
l = [('a',1),('c',3),('d',4),('b',2)]
print(sorted(l,key=lambda x:x[1]))
print(sorted(l,key=lambda x:x[1],reverse=True)) # 降序
```
* enumerate: 用于将一个可遍历的数据对象(如列表元组或字符串)组合为一个索引序列同时列出数据和数据下标一般用在 for 循环当中
```python
print(enumerate([1,2,3]))
for i in enumerate([1,2,3]):
print(i)
for i in enumerate([1,2,3],100):
print(i)
```
* all可迭代对象中全都是True才是True
* any可迭代对象中有一个True 就是True
```python
print(all([1,2,True,0]))
print(any([1,'',0]))
```
* zip函数用于将可迭代的对象作为参数将对象中对应的元素打包成一个个元组然后返回由这些元组组成的列表如果各个迭代器的元素个数不一致则返回列表长度与最短的对象相同
```python
l1 = [1,2,3,]
l2 = ['a','b','c',5]
l3 = ('*','**',(1,2,3))
for i in zip(l1,l2,l3):
print(i)
```
filter用于过滤序列过滤掉不符合条件的元素返回由符合条件元素组成的新列表
```python
def func(x): return x%2 == 0
ret = filter(func,[1,2,3,4,5,6,7,8,9,10])
print(ret)
for i in ret:
print(i)
```
* map:会根据提供的函数对指定序列做映射Python 3.x 返回迭代器
```python
def square(x):
return x**2
ret1 = map(square,[1,2,3,4,5,6,7,8])
ret2 = map(lambda x:x ** 2,[1,2,3,4,5,6,7,8])
ret3 = map(lambda x,y : x+y,[1,2,3,4,5,6,7,8],[8,7,6,5,4,3,2,1])
for i in ret1:
print(i,end=' ')
print('')
for i in ret2:
print(i,end=' ')
print('')
for i in ret3:
print(i,end=' ')
```
## 匿名函数
匿名函数为了解决那些功能很简单的需求而设计的一句话函数
```python
# 这段代码
def calc(n):
return n ** n
print(calc(10))
# 换成匿名函数
calc = lambda n: n ** n
print(calc(10))
```
>匿名函数格式的说明
**函数名 = lambda 参数 :返回值**
1. 参数可以有多个,用逗号隔开
2. 匿名函数不管逻辑多复杂,只能写一行,且逻辑执行结束后的内容就是返回值
3. 返回值和正常的函数一样可以是任意数据类型
```python
l=[3,2,100,999,213,1111,31121,333]
print(max(l))
dic={'k1':10,'k2':100,'k3':30}
print(max(dic))
print(dic[max(dic,key=lambda k:dic[k])])
res = map(lambda x:x**2,[1,5,7,4,8])
for i in res:
print(i)
res = filter(lambda x:x>10,[5,8,11,9,15])
for i in res:
print(i)
```