Python-day4-built-in function 2, python-day4-Function
Abstract: There are many built-in functions with high availability in python, which are helpful for later programming ~~~~
Callable function, chr function and ord function, random function, compile function, evec and eval function, dir function, divmod function, isinstance function, filter and map function
1 # review of the previous Lesson 2 li = [11, 22, 33, 44] 3 def f1 (arg): 4 arg. append (55) 5 li = f1 (li) 6 print (li) 7 >>> None Note: because the function does not return, the returned value is None, li = f1 (li) is to assign the None value to li 8 9 li = [11, 22, 33, 44] 10 def f1 (arg): 11 arg. append (55) 12 f1 (li) 13 print (li) 14 >>> [11, 22, 33, 44, 55]
1 # callable function, determine whether to call 2 def f1 (): 3 pass4 f1 () 5 f2 = 1236 print (callable (f1 )) 7 #>>> True8 print (callable (f2) 9 #>>> False
1 # chr function and ord function 2 r = chr (65) 3 print (r) 4 >>> A # convert 65 to the corresponding ASCII code 5 n = ord ('A') 6 print (n) 7 >>> 97 # convert subtitle a to an ASCII number.
# Random verification code, with a pure 6-digit letter import randomli = [] for I in range (6): temp = random. randrange () # randomly obtain numbers from 65 to 91 c = chr (temp) # convert a random number to the letter li. append (c) # append to the list result = "". join (li) print (result) # random verification code, combining letters and numbers import randomli = [] for I in range (6): if random. randrange (0, 5) % 2: num = random. randrange (0, 10) li. append (str (num) else: temp = random. randrange () # randomly obtain numbers from 65 to 91 c = chr (temp) # convert a random number to the letter li. append (c) # append to the list result = "". join (li) print (result)
# Compile function, which compiles a string into python code exec to accept: code or string exec ("print ('abc')") >>> abc # execution expression, and get the result ret = eval ("7 + 9 + 8") print (ret) >>> 24
1 # Quick View, objects provide those functions 2 print (dir (list) 3 >>> ['_ add _', '_ class __', '_ ins INS _', '_ delattr _', '_ delitem _', 4' _ dir __', '_ doc _', '_ eq _', '_ format _', '_ ge __', 5 '_ getattribute _', '_ getitem _', '_ gt _', '_ hash __', '_ iadd _', 6' _ imul _ ',' _ init _ ',' _ iter __', '_ le _', '_ len _', '_ lt _', 7' _ mul __', '_ ne _', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr __', 8 '_ reversed _', '_ rmul _', '_ setattr _', '_ setitem __', '_ sizeof _', '_ str _', 9' _ subclasshook _ ', 'append', 'clear', 'copy ', 'Count', 'extend', 'index', 'insert', 10 'pop', 'delete', 'reverse', 'sort ']
# Divmod function, 97 data records in total, 10 data records are displayed on each page. How many pages are required? r = divmod () print (r) >>> (9, 7) # If there are more than 7 data records on 9 pages and r [1]> 0 can be judged, a total of r [0] + 1 pages are required.
1 # isinstance function, judge whether it is a Class Object 2 s = [11, 22, 33] 3 r = isinstance (s, list) 4 print (r) 5 >>> True # The list s is the list object, the string type value is the str class object, and the dictionary type value is the dict object.
1 # The second parameter of the filter function loop is used for each element to execute the function. If the function returns True, the element is valid. 2 is used to filter. 3 def f1 (args) is derived from the following program ): 4 result = [] 5 for item in args: 6 if item> 22: # judge the values of big and 22 in the list 7 result. append (item) # append to the result list. 8 return result 9 li = [11,22, 33,44, 55] 10 ret = f1 (li) #11 print (ret) 12 >>> [33, 44, 55] 13 # The filter function only returns True or False. Use lambda's automatic return function to receive the returned value 14 f1 = lambda a: a + 3015 ret = f1 (30) 16 print (ret) 17 >>> 6018 19 # equivalent to 20 li = [, 22, 55] 21 # loop the second iteratable element, judge whether it is greater than 30 returns True, lambda automatically receives the returned value and puts it into the result for filtering 22 result = filter (lambda a: a> 30, li) 23 # less than the returned Flaselambda does not receive the returned value 24 print (list (result )) 25 26 # map function, loop the second iteratable element, and return value 27 li = [200, 55] 28 result = map (lambda a: a +, li) 29 print (list (result) 30 >>> [211,222,233,244,255] 31 32 # comparison of two functions 33 li = [11, 22, 33, 44] 34 r = map (lambda a: a + 100, list (map (lambda a: a> 30, li) 35 print (list (r )) 36 #>>> [100,100,101,101] #0 or 137 38 li = [11, 22, 33, 44] 39 r = filter (lambda: a + 100, list (filter (lambda a: a> 30, li) 40 print (list (r) 41 # >>> [33, 44] # filter cannot perform expression operations. Only True results can be returned through lambda 42 43 li = [11, 22, 33, 44] 44 r = map (lambda: a + 100, list (filter (lambda a: a> 30, li) 45 print (list (r) 46 # [133,144] combined use, if the value is greater than 30, expression calculation is performed.
1 # len function. Note that in 2.0, the len character is automatically converted to the UTF-8 byte length. In 3.0, the len character is not converted, one character is 2 s = 'Lee Guang Xu '3 print (len (s) 4 # >>> 35 B = bytes (s, encoding = 'utf-8 ') 6 print (len (B) 7 # >>> 9