【Python】内置函数
一、内置函数表格
二、内置函数详情
2.1 abs(x)
返回绝对值
1
2
|
>>> abs ( - 5 ) 5 |
2.2 all(iterable)
如果这个可迭代的元素都为真,就返回true。非0就为真,负数也为真,空也为真
1
2
3
4
5
6
7
8
9
10
11
|
>>> all ([ - 1 , 2 , 3 , 4 , 5 ]) True >>> all (( - 1 , 2 , 3 , 4 )) True >>> all ([]) True >>> all ([ - 1 , 0 , 2 , 3 , 4 ]) False |
2.3 any(iterable)
可迭代的元素中,有一个为真,则返回真,空列表返回假。
1
2
3
4
5
6
7
8
|
>>> any ([ - 1 , 0 , 1 , 2 , 3 ]) True >>> any ([]) False >>> any ([ 0 ]) False >>> any ([ 1 ]) True |
2.4 ascii(object)
把内存对象变成一个可打印的字符串格式
1
2
|
>>> ascii([ 1 , 2 , 3 , 4 ]) '[1, 2, 3, 4]' |
2.5 bin(x)
把一个整数转换为二进制数
1
2
3
4
5
6
7
8
9
|
>>> bin ( 11111 ) '0b10101101100111' >>> bin ( - 1223 ) '-0b10011000111' >>> bin ( 1.2 ) Traceback (most recent call last): File "<stdin>" , line 1 , in <module> TypeError: 'float' object cannot be interpreted as an integer |
2.6 boll([X])
不为空则为真,反之为假;判断正确为真,错误为假
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
>>> bool ([ 1 , 2 , 3 , 4 ]) True >>> bool ([]) False >>> bool ( "1" ) True >>> bool ( "sfasfsa" ) True >>> bool ("") False >>> bool ( - 1 ) True >>> bool ( 0 ) False >>> bool () False >>> bool ({}) False >>> bool ({ "sdf" : 1 }) True >>> bool (()) False >>> bool (( 1 , 2 )) True >>> bool ( 3 > 5 ) False >>> bool ( 3 < 5 ) True |
2.7 bytearray([source[,encoding[,errors]]])
字节数组,并且可以修改二进制的字节
1
2
3
4
5
6
|
>>> b = bytearray( "abcd" ,encoding = "utf-8" ) >>> b[ 0 ] # 打印第一个元素的ascii值 97 >>> b[ 0 ] = 100 # 修改第一个元素的ascii值,赋值只能是ascii值 >>> b bytearray(b 'dbcd' ) |
2.8 bytes([source[, encoding[, errors]]])
字符串转换成字节
1
2
3
4
5
6
7
8
9
|
>>> b = bytes( "abcd" ,encoding = "utf-8" ) >>> b b 'abcd' >>> b[ 0 ] 97 >>> b[ 0 ] = 100 Traceback (most recent call last): File "<stdin>" , line 1 , in <module> TypeError: 'bytes' object does not support item assignment |
2.9 callable(object)
判断一个对象是否可以被调用,只有在后面有括号的,表示可以调用,比如:函数、类
1
2
3
4
5
6
|
>>> callable ([]) False >>> def bus(): pass ... >>> callable (bus) True |
2.10 chr(i)
通过ascii的值,找到对应的字符
1
2
|
>>> chr ( 99 ) 'c' |
2.11 ord(c)
根据字符,找到对应的ascii值
1
2
|
>>> ord ( "c" ) 99 |
2.12 dict(**kwarg)、dict(mapping,**kwarg)、dict(iterable, **kwarg)
生成一个字典
1
2
3
4
5
6
7
8
9
10
11
12
|
#传入非固定关键字参数 >>> dict (name = "bigberg" ,age = 22 ) { 'name' : 'bigberg' , 'age' : 22 } # 传入列表 >>> s_list = [( "name" , "bigberg" ),( "age" , 22 )] >>> dict (s_list) { 'name' : 'bigberg' , 'age' : 22 } >>> n_list = [[ 'names' ,[ 'zhangsan' , 'lisi' , 'wangwu' ]],[ 'job' ,[ 'doctor' , 'teacher' , 'police' ]]] >>> dict (n_list) { 'names' : [ 'zhangsan' , 'lisi' , 'wangwu' ], 'job' : [ 'doctor' , 'teacher' , 'police' ]} |
2.13 dir(object)
查看方法
dir(list): 查看列表的方法
dir(dict): 查看字典的方法
2.14 divmod(a,b)
地板除,获得一个元组,元组第一个元素是商,第二个元素是余数。
1
2
|
>>> divmod ( 14 , 3 ) ( 4 , 2 ) |
2.15 enumerate(iterable,start=0)
获取一个列表,列表中的每个元素都是一个元组,元组的第一个数是iterable的索引,第二个数是iterable的元素。
1
2
3
4
5
|
fruits = [ 'apple' , 'orange' , 'banana' ] print ( list ( enumerate (fruits))) #输出 [( 0 , 'apple' ), ( 1 , 'orange' ), ( 2 , 'banana' )] |
2.16 eval(expression, globals=None, locals=None)
把字典类型的字符串变成字典,把一个整数类型的字符变成int类型,或者加减乘除这种简单转换成表达式。
1
2
3
|
>>> s = "5+989" >>> eval (s) 994 |
最新文章
- find out the neighbouring max D_value by counting sort in stack
- Centos 编写 service 启动
- 四则运算之C++实现篇
- [Effective JavaScript 笔记]第59条:避免过度的强制转换
- Django.template框架 template context (非常详细)
- c++ List、Vector、Stack、Queue使用
- [转]优化PHP程序的方法
- CXF Service Interceptor请求,响应报文之控制台输出
- linux新手入门前知道的一些概念
- java TreeSet应用
- Mysql各种引擎原理实战对比
- Spring的事务管理1
- arrow function、function.apply
- shell脚本语法学习
- WEB服务器----Apache 安装配置
- Android Sms短信发送
- STL 源代码剖析 算法 stl_algo.h -- inplace_merge
- jsp:jstl标签库国际化 fmt
- Miner3D Basic基础版
- python - web自动化测试 - 元素操作 - 鼠标键盘
热门文章
- 20155226 实验三 敏捷开发与XP实践 实验报告
- 20155236 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
- 20155321 实验四 Android程序设计
- 2016-2017-2 20155339《 java面向对象程序设计》实验四Android程序设计
- day4 边缘检测Canny
- Apache入门篇(四)之LAMP架构部署
- mysqldump: Got errno 28 on write(mysql)
- linux下,将一个目录中的图片文件合成为gif图片
- docker error:/root/.docker/config.json: is a directory
- python 的入门