内置函数就是Python给你提供的, 拿来直接用的函数,比如print.,input等。截止到python版本3.6.2 python一共提供了68个内置函数,我将它们分成 12 类,方便你学习。
ss="dp-xml">ss="alt">#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()
1. 和数字相关
(1) 数据类型
(2) 进制转换
ss="dp-xml">ss="alt">print(bin(10)) # 二进制:0b1010 ss="">print(hex(10)) # 十六进制:0xa ss="alt">print(oct(10)) # 八进制:0o12
(3) 数学运算
ss="dp-xml">ss="alt">print(abs(-2)) # 绝对值:2 ss="">print(divmod(20,3)) # 求商和余数:(6,2) ss="alt">print(round(4.50)) # 五舍六入:4 ss="">print(round(4.51)) #5 ss="alt">print(pow(10,2,3)) # 如果给了第三个参数. 表示最后取余:1 ss="">print(sum([1,2,3,4,5,6,7,8,9,10])) # 求和:55 ss="alt">print(min(5,3,9,12,7,2)) #求最小值:2 ss="">print(max(7,3,15,9,4,13)) #求最大值:15
2. 和数据结构相关
(1) 序列
a. 列表和元组
ss="dp-xml">ss="alt">print(list((1,2,3,4,5,6))) #[1, 2, 3, 4, 5, 6] ss="">print(tuple([1,2,3,4,5,6])) #(1, 2, 3, 4, 5, 6)
b. 相关内置函数
reversed() 将一个序列翻转, 返回翻转序列的迭代器
slice() 列表的切片
ss="dp-xml">ss="alt">ss="attribute">lst = ss="attribute-value">"你好啊" ss="">ss="attribute">it = ss="attribute-value">reversed(lst) # 不会改变原列表. 返回一个迭代器, 设计上的一个规则 ss="alt">print(list(it)) #['啊', '好', '你'] ss="">ss="attribute">lst = [1, 2, 3, 4, 5, 6, 7] ss="alt">print(lst[1:3:1]) #[2,3] ss="">ss="attribute">s = ss="attribute-value">slice(1, 3, 1) # 切片用的 ss="alt">print(lst[s]) #[2,3]
c. 字符串
ss="dp-xml">ss="alt">print(str(123)+'456') #123456 ss="">format() 与具体数据相关, 用于计算各种小数, 精算等.
ss="dp-xml">ss="alt">ss="attribute">s = ss="attribute-value">"hello world!" ss="">print(format(s, "^20")) #剧中 ss="alt">print(format(s, "ss="tag"><ss="tag-name">20")) #左对齐 ss="">print(format(s, "ss="tag">>20")) #右对齐 ss="alt"># hello world! ss=""># hello world! ss="alt"># hello world! ss="">print(format(3, 'b' )) # 二进制:11 ss="alt">print(format(97, 'c' )) # 转换成unicode字符:a ss="">print(format(11, 'd' )) # ⼗进制:11 ss="alt">print(format(11, 'o' )) # 八进制:13 ss="">print(format(11, 'x' )) # 十六进制(⼩写字母):b ss="alt">print(format(11, 'X' )) # 十六进制(大写字母):B ss="">print(format(11, 'n' )) # 和d⼀样:11 ss="alt">print(format(11)) # 和d⼀样:11 ss=""> ss="alt">print(format(123456789, 'e' )) # 科学计数法. 默认保留6位小数:1.234568e+08 ss="">print(format(123456789, '0.2e' )) # 科学计数法. 保留2位小数(小写):1.23e+08 ss="alt">print(format(123456789, '0.2E' )) # 科学计数法. 保留2位小数(大写):1.23E+08 ss="">print(format(1.23456789, 'f' )) # 小数点计数法. 保留6位小数:1.234568 ss="alt">print(format(1.23456789, '0.2f' )) # 小数点计数法. 保留2位小数:1.23 ss="">print(format(1.23456789, '0.10f')) # 小数点计数法. 保留10位小数:1.2345678900 ss="alt">print(format(1.23456789e+3, 'F')) # 小数点计数法. 很大的时候输出INF:1234.567890
ss="dp-xml">ss="alt">ss="attribute">bs = ss="attribute-value">bytes("今天吃饭了吗", ss="attribute">encoding=ss="attribute-value">"utf-8") ss="">print(bs) #b'\xe4\xbb\x8a\xe5\xa4\xa9\xe5\x90\x83\xe9\xa5\xad\xe4\xba\x86\xe5\x90\x97' ss="alt"> bytearray() 返回一个新字节数组. 这个数字的元素是可变的, 并且每个元素的值得范围是[0,256) ss=""> ss="alt">ss="attribute">ret = ss="attribute-value">bytearray("alex" ,ss="attribute">encoding =ss="attribute-value">'utf-8') ss="">print(ret[0]) #97 ss="alt">print(ret) #bytearray(b'alex') ss="">ret[0] = 65 #把65的位置A赋值给ret[0] ss="alt">print(str(ret)) #bytearray(b'Alex')
ss="dp-xml">ss="alt">print(ord('a')) # 字母a在编码表中的码位:97 ss="">print(ord('中')) # '中'字在编码表中的位置:20013 ss="alt"> ss="">print(chr(65)) # 已知码位,求字符是什么:A ss="alt">print(chr(19999)) #丟 ss=""> ss="alt">for i in range(65536): #打印出0到65535的字符 ss=""> print(chr(i), ss="attribute">end=ss="attribute-value">" ") ss="alt"> ss="">print(ascii("@")) #'@'
ss="dp-xml">ss="alt">ss="attribute">s = ss="attribute-value">"今天n吃了%s顿t饭" % 3 ss="">print(s)#今天# 吃了3顿 饭 ss="alt">print(repr(s)) # 原样输出,过滤掉转义字符 n t r 不管百分号% ss="">#'今天n吃了3顿t饭'
(2) 数据集合
frozenset() 创建一个冻结的集合,冻结的集合不能进行添加和删除操作。
(3) 相关内置函数
a. 语法:sorted(Iterable, key=函数(排序规则), reverse=False)
ss="dp-xml">ss="alt">ss="attribute">lst = [5,7,6,12,1,13,9,18,5] ss="">lst.sort() # sort是list里面的一个方法 ss="alt">print(lst) #[1, 5, 5, 6, 7, 9, 12, 13, 18] ss=""> ss="alt">ss="attribute">ll = ss="attribute-value">sorted(lst) # 内置函数. 返回给你一个新列表 新列表是被排序的 ss="">print(ll) #[1, 5, 5, 6, 7, 9, 12, 13, 18] ss="alt"> ss="">ss="attribute">l2 = ss="attribute-value">sorted(lst,ss="attribute">reverse=ss="attribute-value">True) #倒序 ss="alt">print(l2) #[18, 13, 12, 9, 7, 6, 5, 5, 1]
ss="dp-xml">ss="alt">#根据字符串长度给列表排序 ss="">ss="attribute">lst = ['one', 'two', 'three', 'four', 'five', 'six'] ss="alt">def f(s): ss=""> return len(s) ss="alt">ss="attribute">l1 = ss="attribute-value">sorted(lst, ss="attribute">key=ss="attribute-value">f, ) ss="">print(l1) #['one', 'two', 'six', 'four', 'five', 'three']
ss="dp-xml">ss="alt">ss="attribute">lst = ['one','two','three','four','five'] ss="">for index, el in enumerate(lst,1): # 把索引和元素一起获取,索引默认从0开始. 可以更改 ss="alt"> print(index) ss=""> print(el) ss="alt"># 1 ss=""># one ss="alt"># 2 ss=""># two ss="alt"># 3 ss=""># three ss="alt"># 4 ss=""># four ss="alt"># 5 ss=""># five
ss="dp-xml">ss="alt">print(all([1,'hello',True,9])) #True ss="">print(any([0,0,0,False,1,'good'])) #True
ss="dp-xml">ss="alt">ss="attribute">lst1 = [1, 2, 3, 4, 5, 6] ss="">ss="attribute">lst2 = ['醉乡民谣', '驴得水', '放牛班的春天', '美丽人生', '辩护人', '被嫌弃的松子的一生'] ss="alt">ss="attribute">lst3 = ['美国', '中国', '法国', '意大利', '韩国', '日本'] ss="">print(zip(lst1, lst1, lst3)) #ss="tag"><ss="tag-name">zip object at 0x00000256CA6C7A88ss="tag">> ss="alt">for el in zip(lst1, lst2, lst3): ss=""> print(el) ss="alt"># (1, '醉乡民谣', '美国') ss=""># (2, '驴得水', '中国') ss="alt"># (3, '放牛班的春天', '法国') ss=""># (4, '美丽人生', '意大利') ss="alt"># (5, '辩护人', '韩国') ss=""># (6, '被嫌弃的松子的一生', '日本')
b. 语法:fiter(function. Iterable)
function: 用来筛选的函数. 在filter中会自动的把iterable中的元素传递给function. 然后根据function返回的True或者False来判断是否保留留此项数据 , Iterable: 可迭代对象
ss="dp-xml">ss="alt">def func(i): # 判断奇数 ss=""> return i % ss="attribute">2 == 1 ss="alt"> ss="attribute">lst = [1,2,3,4,5,6,7,8,9] ss="">ss="attribute">l1 = ss="attribute-value">filter(func, lst) #l1是迭代器 ss="alt">print(l1) #ss="tag"><ss="tag-name">filter object at 0x000001CE3CA98AC8ss="tag">> ss="">print(list(l1)) #[1, 3, 5, 7, 9]
map() 会根据提供的函数对指定序列列做映射(lamda)
c. 语法:map(function, iterable)
可以对可迭代对象中的每一个元素进行映射. 分别去执行 function
ss="dp-xml">ss="alt">def f(i): return i ss="">ss="attribute">lst = [1,2,3,4,5,6,7,] ss="alt">ss="attribute">it = ss="attribute-value">map(f, lst) # 把可迭代对象中的每一个元素传递给前面的函数进行处理. 处理的结果会返回成迭代器print(list(it)) #[1, 2, 3, 4, 5, 6, 7]
3. 和作用域相关
ss="dp-xml">ss="alt">def func(): ss=""> ss="attribute">a = ss="attribute-value">10 ss="alt"> print(locals()) # 当前作用域中的内容 ss=""> print(globals()) # 全局作用域中的内容 ss="alt"> print("今天内容很多") ss="">func() ss="alt"># {'a': 10} ss=""># {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': ss="alt"># ss="tag"><ss="tag-name">_frozen_importlib_external.SourceFileLoader object at 0x0000026F8D566080ss="tag">>, ss=""># '__spec__': None, '__annotations__': {}, '__builtins__': ss="tag"><ss="tag-name">module 'builtins' ss="alt"># (built-in)ss="tag">>, '__file__': 'D:/pycharm/练习/week03/new14.py', '__cached__': None, ss=""># 'func': ss="tag"><ss="tag-name">function func at 0x0000026F8D6B97B8ss="tag">>} ss="alt"># 今天内容很多
4. 和迭代器/生成器相关
ss="dp-xml">ss="alt">for i in range(15,-1,-5): ss=""> print(i) ss="alt"># 15 ss=""># 10 ss="alt"># 5 ss=""># 0 ss="alt">ss="attribute">lst = [1,2,3,4,5] ss="">ss="attribute">it = ss="attribute-value">iter(lst) # __iter__()获得迭代器 ss="alt">print(it.__next__()) #1 ss="">print(next(it)) #2 __next__() ss="alt">print(next(it)) #3 ss="">print(next(it)) #4
5. 字符串类型代码的执行
ss="dp-xml">ss="alt">ss="attribute">s1 = ss="attribute-value">input("请输入a+b:") #输入:8+9 ss="">print(eval(s1)) # 17 可以动态的执行代码. 代码必须有返回值 ss="alt">ss="attribute">s2 = ss="attribute-value">"for i in range(5): print(i)" ss="">ss="attribute">a = ss="attribute-value">exec(s2) # exec 执行代码不返回任何内容 ss="alt"> ss=""># 0 ss="alt"># 1 ss=""># 2 ss="alt"># 3 ss=""># 4 ss="alt">print(a) #None ss=""> ss="alt"># 动态执行代码 ss="">exec(""" ss="alt">def func(): ss=""> print(" 我是周杰伦") ss="alt">""" ) ss="">func() #我是周杰伦
ss="dp-xml">ss="alt">ss="attribute">code1 = ss="attribute-value">"for i in range(3): print(i)" ss="">ss="attribute">com = ss="attribute-value">compile(code1, "", ss="attribute">mode=ss="attribute-value">"exec") # compile并不会执行你的代码.只是编译 ss="alt">exec(com) # 执行编译的结果 ss=""># 0 ss="alt"># 1 ss=""># 2 ss="alt"> ss="">ss="attribute">code2 = ss="attribute-value">"5+6+7" ss="alt">ss="attribute">com2 = ss="attribute-value">compile(code2, "", ss="attribute">mode=ss="attribute-value">"eval") ss="">print(eval(com2)) # 18 ss="alt"> ss="">ss="attribute">code3 = ss="attribute-value">"name = input('请输入你的名字:')" #输入:hello ss="alt">ss="attribute">com3 = ss="attribute-value">compile(code3, "", ss="attribute">mode=ss="attribute-value">"single") ss="">exec(com3) ss="alt">print(name) #hello
6. 输入输出
ss="dp-xml">ss="alt">print("hello", "world", ss="attribute">sep=ss="attribute-value">"*", ss="attribute">end=ss="attribute-value">"@") # sep:打印出的内容用什么连接,end:以什么为结尾 ss="">#hello*world@
7. 内存相关
hash() : 获取到对象的哈希值(int, str, bool, tuple). hash算法:(1) 目的是唯一性 (2) dict 查找效率非常高, hash表.用空间换的时间 比较耗费内存
ss="dp-xml">ss="alt">ss="attribute">s = ss="attribute-value">'alex' ss="">print(hash(s)) #-168324845050430382 ss="alt"> ss=""> ss="alt">ss="attribute">lst = [1, 2, 3, 4, 5] ss="">print(hash(lst)) #报错,列表是不可哈希的 ss="alt">id() : 获取到对象的内存地址 ss=""> ss="alt"> ss="">ss="attribute">s = ss="attribute-value">'alex' ss="alt">print(id(s)) #2278345368944
8. 文件操作相关
open() : 用于打开一个文件, 创建一个文件句柄
ss="dp-xml">ss="alt">ss="attribute">f = ss="attribute-value">open('file',ss="attribute">mode=ss="attribute-value">'r',ss="attribute">encoding=ss="attribute-value">'utf-8') ss="">f.read() ss="alt">f.close()
9. 模块相关
__ import__() : 用于动态加载类和函数
ss="dp-xml">ss="alt"># 让用户输入一个要导入的模块 ss="">import os ss="alt">ss="attribute">name = ss="attribute-value">input("请输入你要导入的模块:") ss="">__import__(name) # 可以动态导入模块
10. 帮 助
help() : 函数用于查看函数或模块用途的详细说明
ss="dp-xml">ss="alt">print(help(str)) #查看字符串的用途
11. 调用相关
callable() : 用于检查一个对象是否是可调用的. 如果返回True, object有可能调用失败, 但如果返回False. 那调用绝对不会成功
ss="dp-xml">ss="alt">ss="attribute">a = ss="attribute-value">10 ss="">print(callable(a)) #False 变量a不能被调用 ss="alt"># ss="">def f(): ss="alt"> print("hello") ss=""> print(callable(f)) # True 函数是可以被调用的
12. 查看内置属性
dir() : 查看对象的内置属性, 访问的是对象中的__dir__()方法
ss="dp-xml">ss="alt">print(dir(tuple)) #查看元组的方法