python入门(二),,函数Built-in


函数

Built-in Functions
abs()dict()help()min()setattr()
all()dir()hex()next()slice()
any()divmod()id()object()sorted()
ascii()enumerate()input()oct()staticmethod()
bin()eval()int()open()str()
bool()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()
delattr()hash()memoryview()set()

abs() Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.返回number类型的绝对值。参数应为整数或是浮点数。如果是复数,则取模∣z∣=√(a^2+b^2)

all() ReturnTrueif all elements of theiterableare true (or if the iterable is empty). Equivalent to:如果迭代对象中的所有元素都为true或迭代对象为空则返回true。空字符串,空列表,空元祖返回true。

技术分享图片
def all(iterable):    for element in iterable:        if not element:            return False    return True
View Code

any() ReturnTrueif any element of theiterableis true. If the iterable is empty, returnFalse. Equivalent to:迭代对象中任一元素为true,则返回true。迭代对象为空,返回false。

技术分享图片
def any(iterable):    for element in iterable:        if element:            return True    return False
View Code

ascii() Return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned byrepr()using\x,\uor\Uescapes. 返回一个对象的可打印的表示形式的字符串。如果不是ascII码,则返回\x,\u或\U的方式。

bin()Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. Ifxis not a Pythonintobject, it has to define an__index__()method that returns an integer. Some examples:将一个整型转化为带“0b”前缀的字符串。结果是一个有效的python表达式。如果参数不是Int类型,那么,他需要定义__index__()方法返回一个整数。

技术分享图片
>>> bin(3)‘0b11‘>>> bin(-10)‘-0b1010‘
View Code

bool()Return a Boolean value, i.e. one ofTrueorFalse.xis converted using the standardtruth testing procedure. Ifxis false or omitted, this returnsFalse; otherwise it returnsTrue. Theboolclass is a subclass ofint(seeNumeric Types — int, float, complex). It cannot be subclassed further. Its only instances areFalseandTrue(seeBoolean Values).返回一个布尔值 。

bytearray() Return a new array of bytes. Thebytearrayclass is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described inMutable Sequence Types, as well as most methods that thebytestype has, seeBytes and Bytearray Operations.返回一个新的字节数组。字节数组是由0--256之间的数组成的可变序列。基本不理解,跳过。

isinstance() Return true if theobjectargument is an instance of theclassinfoargument, or of a (direct, indirect orvirtual) subclass thereof. Ifobjectis not an object of the given type, the function always returns false. Ifclassinfois a tuple of type objects (or recursively, other such tuples), return true ifobjectis an instance of any of the types. Ifclassinfois not a type or tuple of types and such tuples, aTypeErrorexception is raised.如果参数是某个数据类型的实例或子集则返回true。如果这种类型指的是元祖,只要检验的参数是任何一种类型的实例即返回true。

定义函数

在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。

空函数

def nop():    pass

参数检查

调用函数时,如果参数个数不对,Python解释器会自动检查出来,并抛出TypeError,参数类型不对无法检查。

技术分享图片
def my_abs(x):    if not isinstance(x, (int, float)):        raise TypeError(‘bad operand type‘)    if x >= 0:        return x    else:        return -x
View Code

返回多个值(返回一个tuple)

技术分享图片
import mathdef move(x, y, step, angle=0):    nx = x + step * math.cos(angle)    ny = y - step * math.sin(angle)    return nx, ny
View Code

默认参数(必选参数在前,默认参数在后)

技术分享图片
def power(x, n=2):    s = 1    while n > 0:        n = n - 1        s = s * x    return s
View Code

python入门(二)

评论关闭