Python 13th days, old boy python 13th
Built-in functions:
Bytes bytecode string
1 str = bytes ('abc', encoding = 'utf-8', errors = 'strict ') # byte format string 2 print (str)
Bytearray: modifiable bytecode string
1 str_1 = bytearray ('abcd', encoding = 'utf-8', errors = 'strict ') #: modifiable Byte Code Format String 2 print (str_1) 3 str_1 [1] = 70 # use the number corresponding to the ascii code table to modify 4 print (str_1)
callable:
1 def func (): 2 print ('1') 3 4 print (callable (func) # The callable object returns True; otherwise, False.
chr ord
1 print (chr (199) # query the character 2 print (ord ('A') corresponding to the number in the ascii code table # query the character number corresponding to the ascii code table
compile:
1 str_2 = ''' 2 a = 0 3 while a <100: 4 a + = 1 5 if a % 2 = 1: 6 print () 7 8 ''' 9 str_3 = compile (str_2, 'err. log', 'exec ') # The string can be compiled into executable code 10 exec (str_3) # execute 11 exec (str_2) with exec) # using exec, you can also directly compile and run 12 print (dir (str_2) # You can view all the methods available to the object
divmod:
1 x, y = 15, 62 print (divmod (x, y) # Calculate the quotient and remainder of x divided by y
eval:
1 list_2 = "{'A': '1', 1', '2': 'C', 'D ': '8'} "2 list_1 = eval (list_2) # You can convert a string to a dictionary \ set 3 n = '2 + 1' 4 list_3 = eval (n) # You can also convert a simple arithmetic string 5 print (list_1) 6 print (list_3)
enumerate:
1 list_4 = [1, 2, 4] 2 list_5 = enumerate (list_4) # convert the list into a triple 3 for I in list_5: 4 print (I)
filter map:
List_5 = filter (lambda n: n> 5, range (10) # The filter function works with an anonymous function to return the list_5 = map (lambda n: n> 5, range (10) # map works with anonymous functions to return True for values in anonymous functions that conform to the delimiter and False for non-conforming conditions, print ('I am a gorgeous line '. center (60, '-') for I in list_5: print (I) print ('I am a gorgeous line '. center (60, '-') list_5 = map (lambda n: n * n, range (10 )) # map can be used with the anonymous function to return all values generated in the anonymous function for I in list_5: print (I)
functools.reduce:
1 import functools2 res = functools. reduce (lambda x, n: x * n, range () # using anonymous functions, you can accumulate 3 print (res) operations on x and n)
frozenset:
1 list_4 = set ({3: {1, 2}, 4: [1, 2, 4]}) 2 list_5 = set ([1, 2, 2, 233,2]) 3 print (list_4) 4 print (list_5) 5 list_6 = frozenset (list_4) # convert the set to an immutable tuples 6 list_7 = frozenset (list_5) # convert the set into an unchangeable tuples 7 print (list_6) 8 print (list_7)
Globals:
1 globals () # obtain the dictionary set of keys and names of all variables in the entire file program
hash:
1 print (hash (list_6) # hash list, hash value, assign the object to a hash value. The hash value is unique in the system and will not be repeated, the generated set cannot be assigned with the hash value 2 print (hash (list_7) # hash list, hash value, and object assigned to a hash value. The hash value is only the unique ID in the system, not repeated
Hex oct:
1 x = hex (10) # convert the integer to hexadecimal format 2 y = oct (10) # convert the integer to Octal 3 print (x) 4 print (y)
locals:
1 def fun_2 (): #2 a = 13 print (locals () # Return the local variable 4 fun_2 ()
Max min
1 print (max (list_5) # Return the maximum value in the list
2 print (min (list_5) # Return the minimum value in the list
next():
1 B = (I * 2 for I in range (100) # Generator 2 print (next (B )) # same as _ next _ in the iterator 3 print (next (B) # same as _ next _ in the iterator 4 print (next (B )) # same as _ next _ in the iterator
Pow ():
1 n, m = 3, 4 2 I = pow (n, m) # Power of m in n
Range
1 list_8 = range (1, 9) # array list generator
2 print (list_8)
Round ():
1 numb = 100.123456 #2 print (round (numb, 3) # number of decimal places of the Floating Point
Sorted (): sorts dictionaries.
1 dict_1 = {4:435, 5: 98,9:-89,7: 6} 2 print (dict_1) 3 dict_2 = sorted (dict_1.items () # sort and convert to list, 4 print (dict_2) 5 dict_3 = sorted (dict_1.items (), KEY = lambda x: x [1]) by default. # Use lambda, for the specified KEY, use value (content) as the Order of 6 print (dict_3)
zip:
1 d = [1, 2, 4] 2 e = ['A', 'B', 'C'] 3 for I in zip (d, e ): # combine the two tables by position to form a list of tuples. If the table is not in the same length, the tables are combined by short values. 4 print (I)