python一共有多少个内置函数_Python 35个内置函数,你都ok吗?
Python一共有60多个内置函数,今天先梳理其中35 个
1 abs()
绝对值或复数的模
In [1]: abs(-6)
Out[1]: 6
。
2 all()
接受一个迭代器,如果迭代器的所有元素都为真,那么返回True,否则返回False
In [2]: all([1,0,3,6])
Out[2]: False
In [3]: all([1,2,3])
Out[3]: True
3 any()
接受一个迭代器,如果迭代器里有一个元素为真,那么返回True,否则返回False
In [4]: any([0,0,0,[]])
Out[4]: False
In [5]: any([0,0,1])
Out[5]: True
4 ascii()
调用对象的repr() 方法,获得该方法的返回值
In [30]: class Student():
...: def __init__(self,id,name):
...: self.id = id
...: self.name = name
...: def __repr__(self):
...: return 'id = '+self.id +', name = '+self.name
In [33]: print(xiaoming)
id = 001, name = xiaoming
In [34]: ascii(xiaoming)
Out[34]: 'id = 001, name = xiaoming'
5 bin()
将十进制转换为二进制
In [35]: bin(10)
Out[35]: '0b1010'
6 oct()
将十进制转换为八进制
In [36]: oct(9)
Out[36]: '0o11'
7 hex()
将十进制转换为十六进制
In [37]: hex(15)
Out[37]: '0xf'
8 bool()
测试一个对象是True, 还是False.
In [38]: bool([0,0,0])
Out[38]: True
In [39]: bool([])
Out[39]: False
In [40]: bool([1,0,1])
Out[40]: True
9 bytes()
将一个字符串转换成字节类型
In [44]: s = "apple"
In [45]: bytes(s,encoding='utf-8')
Out[45]: b'apple'
10 str()
将字符类型、数值类型等转换为字符串类型
In [46]: integ = 100
In [47]: str(integ)
Out[47]: '100'
11 callable()
判断对象是否可以被调用,能被调用的对象就是一个callable 对象,比如函数 str, int 等都是可被调用的,但是例子4 中xiaoming这个实例是不可被调用的:
In [48]: callable(str)
Out[48]: True
In [49]: callable(int)
Out[49]: True
In [50]: xiaoming
Out[50]: id = 001, name = xiaoming
In [51]: callable(xiaoming)
Out[51]: False
12 chr()
查看十进制整数对应的ASCII字符
In [54]: chr(65)
Out[54]: 'A'
13 ord()
查看某个ascii对应的十进制数
In [60]: ord('A')
Out[60]: 65
14 classmethod()
classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。
In [66]: class Student():
...: def __init__(self,id,name):
...: self.id = id
...: self.name = name
...: def __repr__(self):
...: return 'id = '+self.id +', name = '+self.name
...: @classmethod
...: def f(cls):
...: print(cls)
15 complie()
将字符串编译成python 能识别或可以执行的代码,也可以将文字读成字符串再编译。
In [74]: s = "print('helloworld')"
In [75]: r = compile(s,"", "exec")
In [76]: r
Out[76]: at 0x0000000005DE75D0, file "", line 1>
In [77]: exec(r)
helloworld
16 complex()
创建一个复数
In [81]: complex(1,2)
Out[81]: (1+2j)
17 delattr()
删除对象的属性
In [87]: delattr(xiaoming,'id')
In [88]: hasattr(xiaoming,'id')
Out[88]: False
18 dict()
创建数据字典
In [92]: dict()
Out[92]: {}
In [93]: dict(a='a',b='b')
Out[93]: {'a': 'a', 'b': 'b'}
In [94]: dict(zip(['a','b'],[1,2]))
Out[94]: {'a': 1, 'b': 2}
In [95]: dict([('a',1),('b',2)])
Out[95]: {'a': 1, 'b': 2}
19 dir()
不带参数时返回当前范围内的变量,方法和定义的类型列表;带参数时返回参数的属性,方法列表。
In [96]: dir(xiaoming)
Out[96]:
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'name']
20 divmod()
分别取商和余数
In [97]: divmod(10,3)
Out[97]: (3, 1)
21 enumerate()
返回一个可以枚举的对象,该对象的next()方法将返回一个元组。
In [98]: s = ["a","b","c"]
...: for i ,v in enumerate(s,1):
...: print(i,v)
...:
1 a
2 b
3 c
22 eval()
将字符串str 当成有效的表达式来求值并返回计算结果;取出字符串中内容
In [99]: s = "1 + 3 +5"
...: eval(s)
...:
Out[99]: 9
23 exec()
执行字符串或complie方法编译过的字符串,没有返回值
In [74]: s = "print('helloworld')"
In [75]: r = compile(s,"", "exec")
In [76]: r
Out[76]: at 0x0000000005DE75D0, file "", line 1>
In [77]: exec(r)
helloworld
24 filter()
过滤器,构造一个序列,等价于
[ item for item in iterables if function(item)]
在函数中设定过滤条件,逐一循环迭代器中的元素,将返回值为True时的元素留下,形成一个filter类型数据。
In [101]: fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])
In [102]: list(fil)
Out[102]: [11, 45, 13]
25 float()
将一个字符串或整数转换为浮点数
In [103]: float(3)
Out[103]: 3.0
26 format()
格式化输出字符串,format(value, format_spec)实质上是调用了value的format(format_spec)方法。
In [104]: print("i am {0},age{1}".format("tom",18))
i am tom,age18
27 frozenset()
创建一个不可修改的集合。
In [105]: frozenset([1,1,3,2,3])
Out[105]: frozenset({1, 2, 3})
28 getattr()
获取对象的属性
In [106]: getattr(xiaoming,'name')
Out[106]: 'xiaoming'
29 globals()
返回一个描述当前全局变量的字典
30 hasattr()
In [110]: hasattr(xiaoming,'name')
Out[110]: True
In [111]: hasattr(xiaoming,'id')
Out[111]: False
31 hash()
返回对象的哈希值
In [112]: hash(xiaoming)
Out[112]: 6139638
32 help()
返回对象的帮助文档
In [113]: help(xiaoming)
Help on Student in module __main__ object:
class Student(builtins.object)
| Methods defined here:
|
| __init__(self, id, name)
|
| __repr__(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
33 id()
返回对象的内存地址
In [115]: id(xiaoming)
Out[115]: 98234208
34 input()
获取用户输入内容
In [116]: input()
aa
Out[116]: 'aa'
35 int()
int(x, base =10) , x可能为字符串或数值,将x 转换为一个普通整数。如果参数是字符串,那么它可能包含符号和小数点。如果超出了普通整数的表示范围,一个长整数被返回。
In [120]: int('12',16)
Out[120]: 18
python一共有多少个内置函数_Python 35个内置函数,你都ok吗?相关推荐
- python中locals函数_Python神奇的内置函数locals的实例讲解
摘要 本文我们介绍神奇的locals函数,包括动态创建变量和动态访问变量,以及一个应用场景. 相同属性不相邻问题 需求:有两个list,分别为list1和list2.list1中有n个对象,每个对象有 ...
- python nums函数_Python中的内置函数
Python中有很多内置函数,不需要我们调用模块可以直接使用,而且都是常用函数的封装,下面我们来看看Python中的内置函数都有那些. 1.abs() abs()是绝对值函数,把一个负数转化为正数,数 ...
- python内置的数学函数_Python数字和内置数学函数
python内置的数学函数 In this section, we will be learning about Numbers and various Math functions availabl ...
- python函数装饰函数_Python精进-装饰器与函数对象
本文为<爬着学Python>系列第四篇文章. 从本篇开始,本专栏在顺序更新的基础上,会有不规则的更新. 在Python的学习与运用中,我们迟早会遇到装饰器,这个概念对于初识装饰器的新手来说 ...
- python修改列表中字典内的值_python修改字典内key对应值的方法
python学习笔记:字典 python版本:Python 2.6.6 系统环境:CentOS release 6.2 x86_64 本文参考了互联网上前辈的一些文章 一.字典是python中最灵活的 ...
- python数据分析实战 fabio nelli百度云_Python数据分析实战 内利(Fabio Nelli),杜春晓 9787115432209...
商品描述: 基本信息 书名:Python数据分析实战 定**价:59.00元 作者: 内利(Fabio Nelli) 著,杜春晓 译 出版社:人民邮电出版社 出版日期:2016-08-01 ISBN: ...
- python中len和range函数_Python中len()和range()函数
函数:len() 1:作用:返回字符串.列表.字典.元组等长度 2:语法:len(str) 3:参数: str:要计算的字符串.列表.字典.元组等 4:返回值:字符串.列表.字典.元组等元素的长度 5 ...
- python中求差的函数_Python编程基础11:函数和模块
一.函数和模块概述 (一)函数概述 函数可以看成是语句的集合,通过函数调用来执行其包含的语句.函数可以返回一个计算结果,根据每次函数调用的参数,可以返回不同的计算结果.Python利用函数提高代码的重 ...
- python递归函数1到n的平方和_python中的高阶函数与递归函数
高阶函数 一个函数作为另一个函数的参数传入:函数名可以作为返回值:这样的函数即为高阶函数. 例如:求两个数的平方和 def f(m): return m*m def f1(a,b,function): ...
最新文章
- 无头结点单链表的逆置_单链表的增删查 逆置 倒数第k个节点等问题
- const 和 #define区别
- PHP后期静态绑定概念和用法
- AspNetCore 中使用 InentityServer4(2)
- 第一章 初识Mathematica
- 国盾量子等参与中国量子通信行业首批标准编制 量子通信商用再加速
- ft232h引脚_一种基于芯片ft232h的usb接口电路的制作方法
- linux删除目录下文件的几种方法
- springmvc配置中文乱码过滤器
- 输出IMG格式SAR图像——Img格式图像文件概述
- 高数:第七章(同济大学第七版)
- Oracle 索引层级,十六、oracle 索引
- Linux makefile 教程 非常详细,且易懂
- 什么才是高标准要求自己?
- 数据库:实验五MySQL数据库对象-(视图、索引)
- 0.前言 与 Eigen库的使用整理
- Matlab基础知识五
- 什么是RSS?如何订阅RSS?
- WIN8应用商店闪退
- 两万字长文总结,梳理 Java 入门进阶那些事
热门文章
- JVM 分析工具和查看命令,超详细
- Elasticsearch学习笔记1
- 阿里云容器服务体验: 部署 ShellPays 条码支付整合服务平台 -- (四)结案陈词
- Netbeans自定义mode
- 虚拟桌面几个常见问题?
- SharePoint 2007讨论板(Discussion Board)的答复按钮变脸
- concurrent: wai notify notifyAll
- java泛型学习三:受限制的通配符以及泛型方法
- 计算机科学825,2017年河南大学计算机与信息工程学院825专业基础课(软件工程导论、数据结构)之数据结构考研题库...
- 2017计算机软件,2017年计算机防病毒软件排名全面保护PC安全!