笔记-python-lib-内置函数
笔记-python-lib-内置函数
注:文档来源为Python3.6.4官方文档
1. built-in functions
- abs(x) 返回绝对值
- all(iterable) return true if all elements of the iterable are true.
- any(iterable) return True If any element of the iterable is true.
- ascii(object) 类似 repr() 函数, 返回一个表示对象的字符串, 但是对于字符串中的非 ASCII 字符则返回通过 repr() 函数使用 \x, \u 或 \U 编码的字符。 生成字符串类似 Python2 版本中 repr() 函数的返回值。
- bin(x) convert an integer number to a binary string prefied with”0b”
- class bool([x]) return a boolena value,True or False.
- class bytearray() return a new array of bytes.
如果 source 为整数,则返回一个长度为 source 的初始化数组;
如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
如果没有输入任何参数,默认就是初始化数组为0个元素。
example:
>>>bytearray()
bytearray(b'')
>>> bytearray([1,2,3])
bytearray(b'\x01\x02\x03')
>>> bytearray('runoob', 'utf-8')
bytearray(b'runoob')
- class bytes()
- callable(object) return “True” if the object argument appears callable.False if not.
- chr(i) return the string representing a character whose Unicode code point in the integer i.
chr(97) >>>a
- @classmethod
transform a method into a class method.
classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。
class A(object):
bar = 1
def func1(self):
print ('foo')
@classmethod
def func2(cls):
print ('func2')
print (cls.bar)
cls().func1() # 调用 foo 方法
A.func2() # 不需要实例化
- compile()
compile the source into a code or AST object.
- class complex([real[, imag]])
return a complex number with the value real + imag*1j or convert a string o number to a complex number.
- delattr(object, name)
this is a relative of setattr(). the ‘name’ must be the name of one of the object’s attributes.
delattr(x, ‘foobar’) is equivalent to del x.foobar
- class dict(**kwarg)
- dir(object)
without arguments return the list of names in the current local scope.
with an argument, attempt to return a list of valid attributes for that object.
If the object is a module object, the list contains the names of the module’s attributes.
If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.
- divmod(a, b)
for intergers, the result is the same as(a//b, a%b).
for floating point numbers the result is (q, a%b)
>>> divmod(200.3345, 4)
(50.0, 0.33449999999999136)
- enumerate(iterable, start=0)
return an enumerate object.
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中
for i, element in enumerate(seq):
print(i, seq[i])
- eval(expression, globals= None, locals=None)
eval() 函数用来执行一个字符串表达式,并返回表达式的值
expression -- 表达式。
globals -- 变量作用域,全局命名空间,如果被提供,则必须是一个字典对象。
locals -- 变量作用域,局部命名空间,如果被提供,可以是任何映射对象。
- exec(object[, globals[, locals]])
exec 执行储存在字符串或文件中的 Python 语句,相比于 eval,exec可以执行更复杂的 Python 代码。
- filter(function, iterable)
construct an iterator from those elements of iterable for which function returns true.iterable may be either a sequence, a container which supports iteration, or an iterator.
- class float([x])
- format(value[, format_spec])
- class frozenset([iterable])
return a new frozenset object, optionally with elements taken from iterable.
frozenset object is a built-in class.
- getattr(object, name[, default])
return the value of the named attribute of object.
- globals()
return a dictionary representing the current global symbol table.
- hasattr(object, name)
the arguments are an object and a string.The result is True if the string is the name of the object’s attributes, False if not.
- hash(object)
return the hash value of the object(if it has one).Hash value are
hash() 函数可以应用于数字、字符串和对象,不能直接应用于 list、set、dictionary。
在 hash() 对对象使用时,所得的结果不仅和对象的内容有关,还和对象的 id(),也就是内存地址有关。
class Test:
def __init__(self, i):
self.i = i
for i in range(50):
t = Test(1)
print(hash(t), id(t))
测试后发现确实会在两个值之间切换.
- help([object])
- hex(x)
convert an interger number to a lowercase hexadecimal string prefixed with ‘0x’
- id(object)
return the “identity” of an object. this is an integer which is guaranteed to be unique and constant for this object during its lifetime.
- input([prompt])
- class int(x=0)
class int(x, base=10)
- isinstance(object, classinfo)
return true if the object argument is an instance of the classinfo argument.
- issubclass(class, classinfo)
- iter(object[, sentinel])
return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument.
sentinel -- 如果传递了第二个参数,则参数 object 必须是一个可调用的对象(如,函数),此时,iter 创建了一个迭代器对象,每次调用这个迭代器对象的__next__()方法时,都会调用 object。
- len()
- class list([iterable])
- locals()
- map(function, iterable, …)
Return an iterator that applies function to every item of iterable, yielding the results.
def f(x):
return x**2
lista = [x for x in range(18)]
listb = list(map(f, lista))
print(listb)
注意:在python3中返回的是iterator,需手动转换。
- max()
- memoryview(obj)
- min()
- next()
- oct(x)
- open()
- ord(c)
- pow(x, y[, z])
- print()
- class property(fget=None, fset=None, fdel=None, doc=None)
- range()
- repr(object)
return a string containg a printable representation
- reversed(seq)
- round(number[, ndigits])
- class set([iterable])
- setattr(object, name, value)
- class slice()
- sorted(iterable, *, key=None, reverse=False)
- @staticmethod
- sum(iterable[, start])
- super([type[, object-or-type]])
- tupple([iterable])
- class type(object)
- vars([object])
- zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.
zip支持解包操作。
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True
- __import__(name, globals=None, locals=None, fromlist=(), level=0)
笔记-python-lib-内置函数的更多相关文章
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- 如何查看Python的内置函数
经常调用的时候不知道python当前版本的内置函数是哪些,可以用下面的指令查看: C:\Users\Administrator>python Python 2.7.11 (v2.7.11:6d1 ...
- python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理
python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
- Python的内置函数
python的内置函数一共有68个,下面将简单介绍各个函数的功能. abs() dict() help() min() setattr() all() dir() hex() next() slice ...
- python print()内置函数
啦啦啦啦啦啦,我又来了,学习任何东西都得坚持,我一定的好好加油!!! 今天来说说print()函数,前边我们已经用过好好多次啦,现在来学习哈吧!!! Python的内置函数,print() print ...
- Python入门-内置函数一
什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...
- Python 集合内置函数大全(非常全!)
Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员 s ...
- Python字典内置函数和方法
Python字典内置函数和方法: 注:使用了 items.values.keys 返回的是可迭代对象,可以使用 list 转化为列表. len(字典名): 返回键的个数,即字典的长度 # len(字典 ...
- Python元组内置函数
Python元组内置函数: len(元组名): 返回元组长度 # len(元组名): # 返回元组长度 tuple_1 = (1,2,3,'a','b','c') print("tuple_ ...
随机推荐
- 最简实例演示asp.net5中用户认证和授权(1)
asp.net5中,关于用户的认证和授权提供了非常丰富的功能,如果结合ef7的话,可以自动生成相关的数据库表,调用也很方便. 但是,要理解这么一大堆关于认证授权的类,或者想按照自己项目的特定要求对认证 ...
- 【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
创建文件和读取文件的值 #region 判断文件是否存在,不存在则创建,否则读取值显示到窗体 public FormMain() { InitializeComponent(); //ReadFile ...
- Elasticsearch支持的字段类型
es支持下列简单的字段类型: String: string Whole number: byte, short, integer, long Floating point: float, double ...
- Xcode Ghost
Xcode Ghost,是一种手机病毒,主要通过非官方下载的 Xcode 传播,能够在开发过程中通过 CoreService 库文件进行感染,使编译出的 App 被注入第三方的代码,向指定网站上传用户 ...
- 从零开始的全栈工程师——js篇2.8
DOM(document object model) DOM主要研究htmll中的节点(也就是标签) 对节点进行操作 可以改变标签 改变标签属性 改变css样式 添加事件 一.操作流程 1 ...
- IOS防作弊产品技术原理分析
由于时间和水平有限,本文会存在诸多不足,希望得到您的及时反馈与指正,多谢! 工具环境: iPhone 6.系统版本 10.1.1IDA Pro 7.0 0x00:防作弊产品介绍 1.由于IOS系统的不 ...
- 华为服务器操作系统EulerOS V2.0
平台: linux 类型: 虚拟机镜像 软件包: java-1.8.0 php-5.4.16 python-2.7.5 qt-4.8.5 tomcat-7.0.69 basic software eu ...
- Protocol Buffer学习教程之类库应用(四)
Protocol Buffer学习教程之类库应用(四) 此教程是通过一个简单的示例,给C++开发者介绍一下如何使用protocol buffers编程,主要包括以下几部分: 定义一个.proto文件 ...
- LeetCode Single Number III (xor)
题意: 给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次.求这两个特殊的元素? 思路: 跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的.将数组扫一遍求全部元素的异或和 x,结 ...
- LeetCode Merge Sorted Array 合并已排序的数组
void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...