Python内置函数(1)

作者: 复苏的兵马俑 | 来源:发表于2020-02-15 11:04 被阅读0次

Python内置函数(1)— abs()、all()、any()、ascii()、bin()、bool()、breakpoint()、bytearray()、bytes()、callable()。
Python内置函数(2)— chr()、classmethod()、compile()、complex()、delattr()、dict()、dir()、divmod()、enumerate()、eval()。
Python内置函数(3)— exec()、filter()、float()、format()、frozenset()、getattr()、globals()、hasattr()、hash()、help()。
Python内置函数(4)— hex()、id()、input()、int()、isinstance()、issubclass、iter()、len()、list()、locals()。
Python内置函数(5)— map()、max()、memoryview()、min()、next()、object()、oct()、open()、ord()、pow()。
Python内置函数(6)— print()、property()、range()、repr()、reversed()、round()、set()、setattr()、slice()、sorted()。
Python内置函数(7)— staticmethod()、str()、sum()、super()、tuple()、type()、vars()、zip()、__import__()。

内置函数(原文)
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
内置函数(中文)
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
Python内置函数.png

01、abs()

a)描述

原文:Return the absolute value of the argument.
中文:返回参数(数字)的绝对值。

b)语法

abs() 方法的语法:abs( x )

c)参数

x:数值表达式,可以是整数、浮点数和复数。

d)返回值

函数返回 x(数字)的绝对值,如果参数是一个复数,则返回它的模。

e)实例
print("abs(-28) : ", abs(-28))
print("abs(128.28) : ", abs(128.28))
print("abs(complex(3,4)) : ", abs(complex(3,4)))

运行结果:

abs(-28) :  28
abs(128.28) :  128.28
abs(complex(3,4)) :  5.0

02、all()

a)描述

原文:
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
中文:
如果bool(x)对迭代中的所有x值都为真,则返回True。
如果iterable为空,则返回True。
诠释:
all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否都为 True,如果是返回 True,否则返回 False。
元素除了是 0、空、None、False 外都算 True。
函数等价于:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
b)语法

all() 方法的语法:all(iterable)

c)参数

iterable:元组或列表。

d)返回值

如果iterable的所有元素不为0、''、False或者iterable为空,all(iterable)返回True,否则返回False;
注意:空元组、空列表返回值为True,这里要特别注意。

e)实例
print("all(['k', 'e', 'v', 'i', 'n']):",all(['k', 'e', 'v', 'i', 'n']))  # 列表list,元素都不为空或0
print("all(['k', 'e', '', 'i', 'n']):",all(['k', 'e', '', 'i', 'n']))   # 列表list,存在一个为空的元素
print("all([0, 1, 2, 3]):",all([0, 1, 2, 3]))          # 列表list,存在一个为0的元素
print("all(('k', 'e', 'v', 'i', 'n')):",all(('k', 'e', 'v', 'i', 'n')))  # 元组tuple,元素都不为空或0
print("all(('k', 'e', '', 'i', 'n')):",all(('k', 'e', '', 'i', 'n')))   # 元组tuple,存在一个为空的元素
print("all((0, 1, 2, 3)):",all((0, 1, 2, 3)))          # 元组tuple,存在一个为0的元素
print("all([]):",all([]))  # 空列表
print("all(()):",all(()))  # 空元组

运行结果:

all(['k', 'e', 'v', 'i', 'n']): True
all(['k', 'e', '', 'i', 'n']): False
all([0, 1, 2, 3]): False
all(('k', 'e', 'v', 'i', 'n')): True
all(('k', 'e', '', 'i', 'n')): False
all((0, 1, 2, 3)): False
all([]): True
all(()): True

03、any()

a)描述

原文:
Return True if bool(x) is True for any x in the iterable.
If the iterable is empty, return False.
中文:
如果bool(x)对迭代中的任意x都为真,则返回True。
如果iterable为空,则返回False。
诠释:
any() 函数用于判断给定的可迭代参数 iterable 是否全部为 False,则返回 False,如果有一个为 True,则返回 True。
元素除了是 0、空、FALSE 外都算 True。
函数等价于:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
b)语法

any() 方法的语法:any(iterable)

c)参数

iterable:元组或列表。

d)返回值

如果都为空、0、False,则返回False,如果不都为空、0、False,则返回True。

e)实例
print("any(['k', 'e', 'v', 'i', 'n']:",any(['k', 'e', 'v', 'i', 'n']))  # 列表list,元素都不为空或0
print("any(['k', 'e', '', 'i', 'n']):",any(['k', 'e', '', 'i', 'n']))   # 列表list,存在一个为空的元素
print("any([0, '', False]):",any([0, '', False]))        # 列表list,元素全为0,'',false
print("any(('k', 'e', 'v', 'i', 'n')):",any(('k', 'e', 'v', 'i', 'n')))  # 元组tuple,元素都不为空或0
print("any(('k', 'e', '', 'i', 'n')):",any(('k', 'e', '', 'i', 'n')))   # 元组tuple,存在一个为空的元素
print("any((0, '', False)):",any((0, '', False)))        # 元组tuple,元素全为0,'',false
print("any([]):",any([])) # 空列表
print("any(()):",any(())) # 空元组

运行结果:

any(['k', 'e', 'v', 'i', 'n']: True
any(['k', 'e', '', 'i', 'n']): True
any([0, '', False]): False
any(('k', 'e', 'v', 'i', 'n')): True
any(('k', 'e', '', 'i', 'n')): True
any((0, '', False)): False
any([]): False
any(()): False

04、ascii()

a)描述

原文:
Return an ASCII-only representation of an object.
As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x, \u or \U escapes. This generates a string similar to that returned by repr() in Python 2.
中文:
返回对象的纯ASCII表示形式。
作为repr(),返回一个包含可打印的对象表示形式的字符串,但是使用\x、\u或\u转义repr()返回的字符串中的非ASCII字符。这将生成一个类似于Python 2中repr()返回的字符串。
诠释:
ascii() 函数类似 repr() 函数, 返回一个表示对象的字符串, 但是对于字符串中的非 ASCII 字符则返回通过 repr() 函数使用 \x, \u 或 \U 编码的字符。 生成字符串类似 Python2 版本中 repr() 函数的返回值。

b)语法

ascii() 方法的语法:ascii(object)

c)参数

object:对象。

d)返回值

返回字符串。

e)实例
print("ascii('kevin'):",ascii('kevin'))
print("ascii('Python学习'):",ascii('Python学习'))

运行结果:

ascii('kevin'): 'kevin'
ascii('Python学习'): 'Python\u5b66\u4e60'

05、bin()

a)描述

原文:
Return the binary representation of an integer.
中文:
返回整数的二进制表示形式。

b)语法

bin() 方法的语法:bin(x)

c)参数

x:int数字

d)返回值

字符串。

e)实例
print("bin(12):",bin(12))
print("bin(28):",bin(28))

运行结果:

bin(12): 0b1100
bin(28): 0b11100

06、bool()

a)描述

bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False。
bool 是 int 的子类。

b)语法

bool() 方法的语法:class bool([x])

c)参数

x:要进行转换的参数。

d)返回值

返回 True 或 False。

e)实例
print("bool():",bool())
print("bool(0):",bool(0))
print("bool(1):",bool(1))
print("bool(''):",bool(''))
print("bool('kevin'):",bool('kevin'))
print("bool([]):",bool([]))
print("bool(['kevin']):",bool(['kevin']))
print("bool(())):",bool(()))
print("bool(('kevin')):",bool(('kevin')))
print("issubclass(bool, int):",issubclass(bool, int))  # bool 是 int 子类

运行结果:

bool(): False
bool(0): False
bool(1): True
bool(''): False
bool('kevin'): True
bool([]): False
bool(['kevin']): True
bool(())): False
bool(('kevin')): True
issubclass(bool, int): True

07、breakpoint()

a)描述

原文:
breakpoint(*args, *kws)
Call sys.breakpointhook(
args, *kws). sys.breakpointhook() must accept whatever arguments are passed.
By default, this drops you into the pdb debugger.
中文:
breakpoint(
args, *kws)
调用breakpointhook (
args, **kws),sys.breakpointhook()必须接受传递的任何参数。
默认情况下,这将进入pdb调试器。

b)语法
c)参数
d)返回值
e)实例

08、bytearray()

a)描述

bytearray()函数返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256。

b)语法

bytearray()方法语法:class bytearray([source[, encoding[, errors]]])

c)参数

如果 source 为整数,则返回一个长度为 source 的初始化数组;
如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
如果没有输入任何参数,默认就是初始化数组为0个元素。

d)返回值

返回新字节数组。

e)实例
print("bytearray():",bytearray())
print("bytearray([1,2,8]):",bytearray([1,2,8]))
print("bytearray('kevin', 'utf-8'):",bytearray('kevin', 'utf-8'))

运行结果:

bytearray(): bytearray(b'')
bytearray([1,2,8]): bytearray(b'\x01\x02\x08')
bytearray('kevin', 'utf-8'): bytearray(b'kevin')

09、bytes()

a)描述

bytes()函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列。它是 bytearray 的不可变版本。

b)语法

bytes 的语法:class bytes([source[, encoding[, errors]]])

c)参数

如果 source 为整数,则返回一个长度为 source 的初始化数组;
如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
如果没有输入任何参数,默认就是初始化数组为0个元素。

d)返回值

返回一个新的 bytes 对象。

e)实例
a = bytes([1,2,3,4])
print("a:",a)
print("type(a):",type(a))
b = bytes('kevin','ascii')
print("b:",b)
print("type(b):",type(b))

运行结果:

a: b'\x01\x02\x03\x04'
type(a): <class 'bytes'>
b: b'kevin'
type(b): <class 'bytes'>

10、callable()

a)描述

原文:
Return whether the object is callable (i.e., some kind of function).
Note that classes are callable, as are instances of classes with a __call__() method.
中文:
返回对象是否可调用(也就是某种功能)。
注意,类是可调用的,就像使用了__call__()方法的类的实例一样。
诠释:
callable() 函数用于检查一个对象是否是可调用的。如果返回 True,object 仍然可能调用失败;但如果返回 False,调用对象 object 绝对不会成功。
对于函数、方法、lambda 函式、 类以及实现了 call 方法的类实例, 它都返回 True。

b)语法

callable()方法语法:callable(object)

c)参数

object:对象

d)返回值

可调用返回 True,否则返回 False。

e)实例
print("callable(0):",callable(0))
print("callable('kevin'):",callable('kevin'))
def add(a, b):
    return a + b
print("callable(add):",callable(add))  # 函数返回 True
class A:  # 类
    def method(self):
        return 0
print("callable(A):",callable(A))  # 类返回 True
a = A()
print("callable(a):",callable(a))  # 没有实现 __call__, 返回 False
class B:
    def __call__(self):
        return 0
print("callable(B):",callable(B))
b = B()
print("callable(b):",callable(b))  # 实现 __call__, 返回 True

运行结果:

callable(0): False
callable('kevin'): False
callable(add): True
callable(A): True
callable(a): False
callable(B): True
callable(b): True

相关文章

网友评论

    本文标题:Python内置函数(1)

    本文链接:https://www.haomeiwen.com/subject/canjfhtx.html