内置函数
1, abs() 取绝对值
1 #abs(x) 2 #Return the absolute value of a number. 3 # The argument may be an integer or a floating point number. 4 # If the argument is a complex number, its magnitude is returned. 5 a = -1 6 print("abs(-1):", abs(a)) 7 8 #result: 9 #abs(-1): 1
2, all(),any()
1 #all(iterable), any(iterable) 2 #all():Return True if all elements of the iterable are true (or if the iterable is empty) 3 #any():Return True if any element of the iterable is true. If the iterable is empty, return False. 4 list_number = [1, 2, 3, 0] 5 print("all() of list_number is:", all(list_number)) 6 print("any() of list_number is:",any(list_number)) 7 8 #Result: 9 #all() of list_number is: False 10 #any() of list_number is: True
3, bin(),oct(),hex()
1 #bin(x), oct(x), hex(x) 2 #bin(): Convert an integer number to a binary string. The result is a valid Python expression. 3 #oct(x): Convert an integer number to an octal string. The result is a valid Python expression. 4 #hex(x): Convert an integer number to a lowercase hexadecimal string prefixed with “0x”, for example: 5 i = 10 6 print("bin() of 10:", bin(10), type(bin(10))) 7 print("oct() of 10:", oct(10), type(oct(10))) 8 print("hex() of 10:", hex(10), type(hex(10))) 9 10 #Result 11 #bin() of 10: 0b1010 <class 'str'> 12 #oct() of 10: 0o12 <class 'str'> 13 #hex() of 10: 0xa <class 'str'>
4, bytes(),str()
1 #bytes(),str() 2 #bytes():Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256 3 #class str(object=b'', encoding='utf-8', errors='strict') Return a str version of object. See str() for details. 4 5 b_hello_gbk = bytes("中文", encoding="gbk") #gbk编码,一个中文使用两个字符表示 6 b_hello = bytes("中文", encoding="utf-8") #utf-8编码,一个中文字符使用3个字节表示 7 print("gbk encoding:", b_hello_gbk,type(b_hello_gbk)) 8 print("utf-8 encoding:", b_hello,type(b_hello)) 9 10 str_hello = str(b_hello,encoding="utf-8") 11 print(str_hello, type(str_hello)) 12 13 #gbk encoding: b'\xd6\xd0\xce\xc4' <class 'bytes'> 14 #utf-8 encoding: b'\xe4\xb8\xad\xe6\x96\x87' <class 'bytes'> 15 #中文 <class 'str'>
5, chr(),ord()
1 #chr(), ord() 2 #chr(): Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) 3 #returns the string 'a', while chr(8364) returns the string '€'. 4 #ord(): Given a string representing one Unicode character, return an integer representing the Unicode code point of 5 # that character. For example, ord('a') returns the integer 97 and ord('€') (Euro sign) returns 8364. 6 c = chr(65) 7 print(c, type(c)) 8 i = ord('A') 9 print(i, type(i)) 10 11 #Result 12 #A <class 'str'> 13 #65 <class 'int'>
6, complie(),eval(),exec()
1 #compile(), eval(), exec() 2 #compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 3 #Compile the source into a code or AST object. Code objects can be executed by exec() or eval(). source can either 4 #be a normal string, a byte string, or an AST object. Refer to the ast module documentation for information on how 5 #to work with AST objects. 6 str_code = "for i in range(0, 4): print(i)" 7 exec_code = compile(str_code, '', 'exec') 8 exec(exec_code) 9 10 str = "3*4+5" 11 print('eval of str:', eval(str)) 12 13 #Result: 14 #1 15 #2 16 #3 17 #eval of str: 17
7, divmod()
1 #divmod(a, b) 2 #Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder 3 #when using integer division. 4 page,left = divmod(97, 10) 5 print(page,left) 6 7 #Result 8 #9 7
8, filter(),map()
1 #filter(), map() 2 #filter(function, iterable) 3 #Construct an iterator from those elements of iterable for which function returns true. 4 def filter_condition(number): #过滤条件函数,返回值为true,false,为true的元素将被filter过滤到 5 if number > 3: 6 return True 7 return False 8 list_number = [1, 2, 3, 4, 5] 9 ret_filter1 = filter(filter_condition, list_number) #filter的第一个参数是函数名,不带参数 10 print("filter1:",list(ret_filter1)) #ret_filter是一个filter类的对象,需要用list转化成列表 11 #filter与lambda 配合 12 ret_filter2 = filter((lambda a: a > 3), list_number) 13 print("filter2:", list(ret_filter2)) 14 15 #map(function, iterable, ...) 16 #Return an iterator that applies function to every item of iterable, yielding the results. 17 def mapping(number): #map()相当于映射,对列表里索引元素加100 18 return number + 100 19 ret_map1 = map(mapping, list_number) 20 print("map1:", list(ret_map1)) 21 ret_map2 = map(lambda a: a + 100, list_number) #lambda表达式非常简洁 22 print("map2:",list(ret_map2)) 23 24 #Result: 25 #filter1: [4, 5] 26 #filter2: [4, 5] 27 #map1: [101, 102, 103, 104, 105] 28 #map2: [101, 102, 103, 104, 105]
9, range()
1 #range(stop) #输出从0开始不包含stop的元素 2 #range(start, stop[, step])#输出从start开始,不包含stop,步长step 3 for i in range(5): #输出从0开始,不包含5 4 print(i) 5 6 for i in range(0, 10, 2): #输出从0开始,不包含10,步长2 7 print(i)