Python内置函数总结
1、abs()
取绝对值
1
2
3
4
|
>>> a = abs ( - 7 ) >>> b = abs ( 7 ) >>> print (a,b) 7 7 |
2、all()
循环参数,如果每个元素都为真,那么all的返回值为真
0,None,以及空值都为假,其他都为真(""为假," "为真)
1
2
3
4
|
>>> s = all ([ True , True ]) >>> f = all ([ True , True , False ]) >>> print (s,f) True False |
3、any()
循环参数,只要有一个为真,则为真
1
2
3
|
>>> f = any ([ True , True , False ]) >>> print (f) True |
4、ascii()
在给定对象的所属的类中执行该类的“__repr__”方法,获取其返回值
5、bin()
十进制转二进制
1
2
3
|
>>> s = bin ( 10 ) >>> print (s) 0b1010 |
二进制转十进制
1
2
3
|
>>> i = int ( '0b11' ,base = 2 ) # base=2表示二进制 >>> print (i) 3 |
6、oct()
十进制转八进制
1
2
3
|
>>> s = oct ( 8 ) >>> print (s) 0o10 |
八进制转十进制
1
2
3
|
>>> s = int ( '0o11' ,base = 8 ) >>> print (s) 9 |
7、int()
十进制
8、hex()
十进制转十六进制
1
2
3
|
>>> s = hex ( 14 ) >>> print (s) 0xe |
9、bool()
判断真假,把对象转换成布尔值
10、bytes() 字节
字符串转换成字节
1
2
3
|
>>> s = bytes( 'shaw' ,encoding = 'utf-8' ) >>> print (s) b 'shaw' |
11、chr()
接收一个数字,查找数字对应的ascii表中对应的值
1
2
3
|
>>> shaw = chr ( 65 ) >>> print (shaw) A |
12、ord()
接收一个字符,查找该字符在ascii表中对应的数字
1
2
3
|
>>> f = ord ( 'a' ) >>> print (f) 97 |
13、callable()
检查对象是否可执行
14、dict()
把对象转换成字典
15、dir()
查看对象所有方法
16、divmod()
给定对象(除数,被除数),计算商与余数(计算结果为一个元祖类型)
1
2
3
|
>>> s = divmod ( 10 , 3 ) >>> print (s) ( 3 , 1 ) |
17、enumerate()
对可循环对象前添加序号
1
2
3
4
5
6
7
8
|
>>>a = [ 'shaw' , 'sam' , 'alices' , 24 ] >>> for i in enumerate (a): ... print (i) ... ( 0 , 'shaw' ) ( 1 , 'sam' ) ( 2 , 'alices' ) ( 3 , 24 ) |
18、eval()
把字符类型转换成int,再计算结果
1
2
3
|
>>>s = eval ( '1 + 4' ) >>> print (s) 5 |
19、exec()
用来执行python代码(表达式)
1
2
3
4
5
6
7
8
9
|
>>> exec ( 'for i in range(8):print(i)' ) 0 1 2 3 4 5 6 7 |
20、filter()过滤对象
filter('函数','可以迭代的对象')
# 循环对象中的每个元素,并作为函数的参数执行前面的函数,如果函数返回True,表示符合条件
1
2
3
4
5
6
7
8
9
10
11
|
def shaw(x): if x > 10 : return True a = [ 6 , 8 , 11 , 33 , 44 ] s = filter (shaw,a) fori in s: print (i) C:\Python35\python.exeF: / PyCharm / Python / PY_learn / lianxi.py 11 33 44 |
21、map()
map('函数','可以迭代的对象')
# 循环对象中的每个元素,并作为函数的参数执行函数,并返回新数值
1
2
3
4
5
6
7
8
9
10
|
def shaw(x): return x + 10 a = [ 11 , 33 , 44 ] s = map (shaw,a) fori in s: print (i) C:\Python35\python.exeF: / PyCharm / Python / PY_learn / lianxi.py 21 43 54 |
22、format()
str格式化输出数据
1
2
3
4
5
6
7
|
a = [ 'sam' , 'shaw' , 'alices' ] fori in a: print ( '24{}' . format (i)) C:\Python35\python.exeF: / PyCharm / Python / PY_learn / lianxi.py 24sam 24shaw 24alices |
23、globals()
获取当前代码里面所有的全局变量
24、locals()
获取当前代码里面所有的局部变量
25、hash()
计算给定对象哈希值
1
2
|
>>> hash ( 'shaw' ) 3346328168152020605 |
26、id()
查看对象的内存地址
1
2
3
|
>>>a = 123 >>> id (a) 1402863216 |
27、help()
查看对象所属类的方法的详细信息
28、input()
获取输入信息
1
2
3
4
5
|
user = input ( '用户名:' ).strip() print (user) C:\Python35\python.exeF: / PyCharm / Python / PY_learn / lianxi.py 用户名:shaw shaw |
29、isinstance()
判断对象所属的数据类型
1
2
3
4
5
|
>>>s = [ 'shaw' , 'sam' ] >>> if isinstance (s, list ): ... print ( 'haha' ) ... haha |
30、len()
取对象的长度
1
2
3
|
>>>s = [ 'shaw' , 'sam' ] >>> len (s) 2 |
31、max()
取对象最大值
min()
最对象最小值
1
2
3
4
5
|
>>>f = [ 1 , 23 , 22 , 99 ] >>> max (f) 99 >>> min (f) 1 |
32、pow()
计算幂
1
2
3
|
>>>i = pow ( 2 , 3 ) >>> print (i) 8 |
34、round()
为对象四舍五入
1
2
3
4
5
6
|
>>>f = round ( 4.4 ) >>> print (f) 4 >>>f = round ( 4.6 ) >>> print (f) 5 |
35、sum()
求和
1
2
3
|
>>>f = sum ([ 11 , 22 ]) >>> print (f) 33 |
36、zip()
把两个可迭代对象,新组合成一个
1
2
3
4
5
6
7
8
9
|
s = [ 1 , 2 , 3 ] f = [ 'a' , 'b' , 'c' ] k = zip (s,f) fori in k: print (i) C:\Python35\python.exeF: / PyCharm / Python / PY_learn / lianxi.py ( 1 , 'a' ) ( 2 , 'b' ) ( 3 , 'c' ) |
37、sorted()
排序
数字,按从小到大顺序排序
1
2
3
|
>>>f = [ 1 , 23 , 22 , 99 ] >>> sorted (f) [ 1 , 22 , 23 , 99 ] |
字符,先数字(从小到大),再字母(按字母在assic表中对应数字大小,从小到大),在中文
待完善。。。
Python内置函数总结的更多相关文章
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- Python内置函数进制转换的用法
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...
- Python内置函数(12)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- Python内置函数(61)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- 那些年,很多人没看懂的Python内置函数
Python之所以特别的简单就是因为有很多的内置函数是在你的程序"运行之前"就已经帮你运行好了,所以,可以用这个的特性简化很多的步骤.这也是让Python语言变得特别的简单的原因之 ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】实习小记-python 内置函数__eq__函数引发的探索
[转]实习小记-python 内置函数__eq__函数引发的探索 乱写__eq__会发生啥?请看代码.. >>> class A: ... def __eq__(self, othe ...
随机推荐
- Spark之SQL解析(源码阅读十)
如何能更好的运用与监控sparkSQL?或许我们改更深层次的了解它深层次的原理是什么.之前总结的已经写了传统数据库与Spark的sql解析之间的差别.那么我们下来直切主题~ 如今的Spark已经支持多 ...
- bug_ _org.json.JSONException: End of input at character 0 of
10-16 18:28:39.549: W/System.err(4950): org.json.JSONException: End of input at character 0 of 10-16 ...
- Java 技术文章摘录
sokcet 编程实例 android bundle类 Android -- Looper.prepare()和Looper.loop() —深入版 Java NIO系列教程 XML操作 Androi ...
- java如何区分是form表单请求,还是ajax请求
requestType = request.getHeader("X-Requested-With"); if(requestType==null) ...
- javascript、ECMAScript、DOM、BOM关系
ECMAScript,正式名称为 ECMA 262 和 ISO/IEC 16262,是宿主环境中脚本语言的国际 Web 标准. ECMAScript 规范定义了一种脚本语言实现应该包含的内容:但是,因 ...
- Java 多张图片合成一张 drawImage
package com.yunfengtech.solution.business; import java.awt.Color; import java.awt.Graphics; import ...
- 转:C# 中 MSCHART 饼状图显示百分比
转自:http://blog.sina.com.cn/s/blog_51beaf0e0100yffo.html 1)显示百分比 Chart1.Series["Series1"].L ...
- 实战p12文件转pem文件
1.首先生成一个ssl的证书 选择app IDS 后实现下面这个(这里不详细说明怎么生成了) 点击Download按钮,我就下载Development的ssl证书,下载成功后,双击运行,会打开钥匙串程 ...
- NodeJs 创建 Web 服务器
以下是演示一个最基本的 HTTP 服务器架构(使用8081端口),创建 ser.js 文件,代码如下所示: var http = require('http'); var fs = require(' ...
- Mac与Phy组成原理的简单分析
1. general 下图是网口结构简图.网口由CPU.MAC和PHY三部分组成.DMA控制器通常属于CPU的一部分,用虚线放在这里是为了表示DMA控制器可能会参与到网口数据传输中. 对于上述的三部分 ...