首页 > 代码库 > 内置函数

内置函数

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
abs(x)

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
all(),any()

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‘>
bin(), oct(), hex()

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‘>
bytes(),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‘>
chr(),ord()

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
compile(),eval(),exec()

 

内置函数