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 ...
随机推荐
- 黄聪:如何为IIS增加svg和woff等字体格式的MIME
现在字体图标已经渐渐代替了图片了,移动端用起来也很方便. 使用了字体文件来显示矢量的图标,为了能在IIS上正常显示图标,可以通过增加iis的MIME-TYPE来支持图标字体文件 下面就把IIS增加sv ...
- 在vue1.0遇到vuex和v-model的坑
事情是这样的,在开发项目的过程中我使用了vuex并且在store中定义了一个保存用户信息的对象 userInfo : { 'nickName' : '', // 昵称 'password' :'', ...
- 使用JavaScript的Join方法
在本例中,我们将创建一个数组,然后把它的所有元素放入一个字符串: <script type="text/javascript"> var arr = new Array ...
- sklearn学习笔记2
Text classifcation with Naïve Bayes In this section we will try to classify newsgroup messages using ...
- c# 变量,对象,静态类型,集合类的线程安全回顾
1.变量的线程安全性与变量的作用域有关. 2.对象 对象是类型的实例 在创建对象时,会单独有内存区域存储对象的属性和方法.所以,一个类型的多个实例,在执行时,只要没有静态变量的参与,应该都是线程安全的 ...
- Microsoft Capicom 2.1 On 64bit OS
第一步下载capicom.dll http://files.cnblogs.com/files/chen110xi/DLL.7z 第二步注册capicom.dll至SysWow64 第三步VS中设置 ...
- (Array)121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 尚学堂Spring视频教程(二):Spring控制反转
用Spring来实现IOC 在上节中我们自定义了一个接口BeanFactory和类ClassPathXmlApplicationContext来模拟Spring,其实它们在Spring中确实是存在的, ...
- 64位电脑上配置mysql-connector-odbc的方法
在系统盘搜索odbcad32,选择在C:\Windows\SysWOW64内的文件
- 牛客网程序员面试金典:1.2——原串翻转(java实现)
问题描述: 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串. ...