NOTE: python3 内置函数
1.常用函数:
abs() —— 计算绝对值
max() —— 计算最大值
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
min() —— 计算最小值
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, [, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
len() —— len(obj, /)返回长度
Return the number of items in a container.
divmod()——divmod(x, y, /) —— 返回除法的结果和余数;
Return the tuple (x//y, x%y). Invariant: divy + mod == x.
divmod(5,2)
(2,1)
pow() —— pow(x, y, z=None, /)计算幂
pow(2,3)
23
pow(2,3,4)
23%4
Equivalent to xy (with two arguments) or xy % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
round —— 四舍五入
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
callable() ——
callable(obj, /)
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.
cmp() —— 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1(仅用于python2)
python3用于operation.eq()代替(import operator)
range() —— class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return an object that produces a sequence of integers from start (inclusive)
| to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, …, j-1.
| start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
| These are exactly the valid indices for a list of 4 elements.
| When step is given, it specifies the increment (or decrement).
type() —— 输出类型
**
2.类型转换
**
complex—— 转换为复数类型
complex(2,3)
2+3j
complex(real[, imag]) -> complex number
|
| Create a complex number from a real part and an optional imaginary part.
| This is equivalent to (real + imag*1j) where imag defaults to 0.
ord()——转换为字符对应的数值(ASCII码)
ord(‘a’)
97
chr(i, /)——将ASCII码转换为Unicode码
Return a Unicode string of one character with ordinal i;
chr(97)
‘a’
bool——转换为相应的真假值
bool(0)
Falsebool(1)
Truebool(‘a’)
True
bool(2)
True
Returns True when the argument x is true, False otherwise.
| The builtins True and False are the only two instances of the class bool.
| The class bool is a subclass of the class int, and cannot be subclassed.
int——转换为整型,integer
Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.int(). For floating point
| numbers, this truncates towards zero.
float——转换为浮点数
Convert a string or number to a floating point number, if possible.
float(“1.2”)
1.2
long()——转换为长整型,long integer
long(“123442”)
123442L
bin(),返回一个字符串,表示一个数值的二进制数
bin(31)
‘0b11111’
hex(),返回一个字符串,表示一个一个数值的十六进制数
hex(31)
‘0xlf’
oct(),返回一个字符串,表示一个数值的八进制数
oct(31)
‘037’
list,转换为列表;
list((1,2,3,4))
[1, 2, 3, 4]
tuple,转换为定值表;
tuple([1,2,3,4])
(1, 2, 3, 4)
slice,构建下标对象;
a = [1,2,3,4,5]
slices = slice(0,4,2)
a[slices]
[1, 3]
dict,构建词典;
dict(a=1,b=[1,2],c=‘hello’)
{‘a’: 1, ‘c’: ‘hello’, ‘b’: [1, 2]}## 3 序列操作
**
3 序列操作
**
all,所有元素都相当与True;
a = range(0,4)
b = range(1,4)
a
[0, 1, 2, 3]
b
[1, 2, 3]
all(a)
False
all(b)
True
any,是否有任意一个元素相当于True;
a = [0]
b = range(0,4)
a
[0]
b
[0, 1, 2, 3]
any(a)
False
any(b)
True
sorted,返回排序后的序列,默认是递增序列,如果指定reverse为True,则返回递减序列;
a = [1,4,3,2]
sorted(a)
[1, 2, 3, 4]
sorted(a,reverse=False)
[1, 2, 3, 4]
sorted(a,reverse=True)
[4, 3, 2, 1]
reversed,返回反序的序列;
a = [1,4,3,2]
b = [ele for ele in reversed(a)]
b
[2, 3, 4, 1]
filter(),参数为函数和列表,元组
语法:
filter(function or None, iterable) --> filter object
|
| Return an iterator yielding those items of iterable for which function(item)
| is true. If function is None, return the items that are true.
filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。
该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。
def fun(x):
if x>5:
return True
m = range(10)
new_m = filter(fun,m)
new_list = list(new_m)
print(new_list)
print(new_m)
>>>[6,7,8,9]
>>><filter object at 0x000001E22C873DD8>
4 类,对象,属性
**
hasattr,检查对象是否拥有某个属性;
getattr,返回某属性;
setattr,将对象中的属性设置为新的属性;
delattr,删除对象中的属性;
isinstance,判断对象是否为类生成的对象;
isinstance() —— isinstance(obj, class_or_tuple, /)
Return whether an object is an instance of a class or of a subclass thereof.
A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
or ...`` etc.
issubclass,判断类是否为某类的子类;