Python3中的内置函数
前段时间发现公司的好多同事问我Python3中的内置函数都有哪些?这个问题让我一愣,一时间我还真没想起来几个,于是就打算写一篇关于常用的内置函数文章,如有写的不好或不对的欢迎评论区留言告诉我!好了废话不多说进入正题:
- print()
这个函数不必多说了相信学习python时学的第一句代码就是这个,print用来打印输出>>> print('hello world!') hello world!
- int()
把数据类型转换成整数>>> a = '126546' >>> type(a) <class 'str'> >>> b = int(a) >>> b 126546 >>> type(b) <class 'int'>
- float()
把数据类型转换成浮点类>>> a = 1235 >>> type(a) <class 'int'> >>> b = float(a) >>> b 1235.0 >>> type(b) <class 'float'>
- str()
把数据类型转换成字符串>>> a = 12345 >>> type(a) <class 'int'> >>> b = str(a) >>> b '12345' >>> type(b) <class 'str'>
- list()
把数据类型转换成列表>>> a = 'asdasd' >>> type(a) <class 'str'> >>> b = list(a) >>> type(b) <class 'list'> >>> a 'asdasd'
- tuple()
把数据类型转换成元组>>> a = 'asdasd' >>> type(a) <class 'str'> >>> b = tuple(a) >>> b ('a', 's', 'd', 'a', 's', 'd') >>> type(b) <class 'tuple'>
- dict()
把数据类型转换成字典>>> a = [('a', 'b')] >>> type(a) <class 'list'> >>> b = dict(a) >>> b {'a': 'b'} >>> type(b) <class 'dict'>
- set()
把数据类型转换成集合>>> a = ('a', 's', 'd', 'a', 's', 'd') >>> type(a) <class 'tuple'> >>> b = set(a) >>> b {'s', 'a', 'd'} # 集合有值不重复特性 >>> type(b) <class 'set'>
- range()
创建一个范围内的列表,包含开头不包含结尾>>> a = list(range(1,10)) >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9]
- round()
四舍五入
当然有传可选参数选择保留小数点位数>>> a = 5.6666 >>> b = round(a) >>> b 6
>>> a = 5.6666 >>> b = round(a, 2) >>> b 5.67
- zip()
列表生成式函数
当然也可生成字典>>> a = ('a', 's', 'd', 'a', 's', 'd') >>> b = (1, 2, 3, 4, 5, 6) >>> c = list(zip(a, b)) >>> c [('a', 1), ('s', 2), ('d', 3), ('a', 4), ('s', 5), ('d', 6)]
>>> a = ('a', 's', 'd', 'a', 's', 'd') >>> b = (1, 2, 3, 4, 5, 6) >>> c = dict(zip(a, b)) >>> c {'a': 4, 's': 5, 'd': 6} # 字典的key是不可重复的
- sorted()
列表排序,不改变原列表>>> a = [9, 8, 7, 6, 5, 4, 3, 2, 1] >>> sorted(a) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a [9, 8, 7, 6, 5, 4, 3, 2, 1]
- reversed()
列表降序排列,不改变原列表>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(reversed(a)) [9, 8, 7, 6, 5, 4, 3, 2, 1]
- map()
把传入的参数迭代入函数里>>>def square(x) : # 计算平方数 ... return x ** 2 ... >>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方 [1, 4, 9, 16, 25]
- type()
返回类型>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> type(a) <class 'list'>
- enumerate()
这个函数不常用,但是是个很好用的函数,给可遍历的数据对象附上坐标>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> enumerate(a) <enumerate object at 0x00000210A4BBC048> >>> for i in enumerate(a): ... print(i) ... (0, 1) (1, 2) (2, 3) (3, 4) (4, 5) (5, 6) (6, 7) (7, 8) (8, 9)
- abs()
绝对值>>> a = 1 >>> b = -1 >>> abs(a) == abs(b) True
- sum()
求和,计算列表中所有元素的和>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> sum(a) 45
- pow()
次方,两个必传参数>>> a = 5 >>> pow(a, 2) 25
- format()
格式化输出字符串,其实和占位符效果一样>>> print('hello {}!'.format('world')) hello world! >>> print('hello {}!'.format('python')) hello python!
- isinstance()
判断一个对象是否是一个已知的类型>>> a = 1 >>> type(a) <class 'int'> >>> isinstance(a, int) True >>> isinstance(a, str) False
- bool()
布尔>>> a = 1 >>> bool(a) True >>> b = 0 >>> bool(b) False
- id()
查看在内存中的地址>>> a = 1524 >>> id(a) # 查看变量a的值在内存中的地址 2270502432496
希望本文对大家有一定帮助,作为有3年多开发经验的程序员给大家一句忠告:基础一定要打好,往往开发中容易被忽略而出BUG的就是基础的东西。目前能想起来的内置函数也就这么多,欢迎大家评论中补充