First, built-in functions
1.callable () Determines whether the function can be executed and returns True/false
2.CHR () The correspondence between ASCII based on numbers
r = Chr (65) Print (R) Output: A |
3.ord () The corresponding relationship of ASCII based on letters
R = Ord (' A ') Print (R) Output: 65 |
#产生随机数字 Import Random #导入random模块 i = Random.randrange (1,5) #给一个数字范围 Print (i) #会随机生成1-5 number, not including 5 |
#产生随机字母 Import Random #导入random模块 i = Random.randrange (65,91) #65-90 is a correspondence table of uppercase A to Z ASCII c = Chr (i) #将对应的数字转换成ASCII对应字母 Print (c) #会随机生成A-Z Letters |
#产生随机6个字母验证码 Import Random #导入random模块 Li = [] #定义一个空列表 for receiving random six random letters For I in range (6): #括号6代表循环6次 temp = Random.randrange (65,91) #65-90 is an ASCII correspondence table of uppercase A to Z, the randomly generated number is assigned to TEMP c = Chr (temp) #将temp的数字转换成ASCII对应字母 Li.append (c) #将随机生成的字母附加到li列表中 result = '. Join (LI) #将li的元素添加到result字符串中 Print (result) #打印会随机生成A-Z 6 letters
|
#产生随机6个 digit + Letter verification Code Import Random #导入random模块 Li = [] #定义一个空列表 for receiving random six random letters For I in range (6): #括号六代表循环6次 R = Random.randrange (0,5) if r = = 2 or r = = 4: num = Random.randrange (0,10) Li.append (num) #列表append不能是数字, so use STR to convert Else temp = Random.randrange (65,91) #65 90 is the uppercase A-to-Z ASCII table, and the randomly generated number is assigned to the TEMP c = Chr (temp) #将temp的数字转换成ASCII对应字母 Li.append (c) #将随机生成的字母附加到li列表中
result = '. Join (LI) #将li的元素添加到result字符串中 Print (Result) #打印随机生成 0-10 and A-Z of 6 |
4.compile () Compile the string into Python code
s = "Print (123)" R = Compile (s, "<string>", ' exec ') Print (R) The compiled code has three parameters: Single compiles into one-line program Eval is compiled into an expression EXEC compiles the exact same thing as the Python code. |
5.eval () specializes in expressions (eval execution has a return value)
#例如 s = "8*8" R = eval (s) #eval处理表达式, is actually calculated 8*8; eval execution has a return value Print (R) #输出: 64 |
|
6.exec () execution code (receive code or string, exec executes no return value)
| s = "Print (123)" A = EXEC (r) Print (a) #exec执行没有返回值 Output: 123 None |
7.complex Processing Negative numbers
8.delattr getattr setattr hasattr reflex
9.dict () dictionary
10.dir () quickly get the functionality provided by a class or object
11.help () See the detailed Help
12.divmod () seeking quotient and remainder
#例子 #网页显示新闻条数, the total number of pages is displayed, #共98页, 10 pages per page, total number of pages. Ye = Divmod (98,10) # Print (Ye) #输出: (9,8) #9是商, 8 is the remainder
When the remainder is 0 o'clock, the representation can be divided evenly, the quotient represents the total number of pages; When the remainder is not 0 o'clock, the quotient needs to add 1 equals the total number of pages. |
13.enumerate ()
14.isinstance () Determine if an object is an instance of a class
First, the relationship between the object and the class Example: s = ' abc ' S is an object, and the value of S is ABC; While performing the function of S, the use of STR str is a class; S is an example of STR
Determine if S is not an instance of STR s = ' abc ' R = isinstance (S,STR) Print (R) #输出True, otherwise false
|
|
15.filter () internal loop, parameter comparison, (in accordance with the need, non-conforming to discard)
The function returns true to add the element to the result
| #例子 #有一个列表, put all lists greater than 22, less than 22 No, first write a function according to your own idea # 1. General wording def f1 (args): result = [] for item in args: &N Bsp If item >: Result.append (item) RET Urn result Li = [11,22,33,44] ret = F1 (LI) Print (ret) #输出: [33,44] # 2, using filter #fileter Format: Filter (function name, parameter) Def f1 (args): If args >22: return True Li = [11,22,33,44] Ret = filter (F1,li) Print (list (ret)) #filter首先循环第二个参数; second, do not loop once to execute the F1 function; #filter就是帮我们做了筛选的功能, if it is legally assigned to RET, throw it away. #3, applying FILTER+LAMBDA expression Li = [11,22,33,44] Result = Filter (lambda arg:arg > 22, Li) Print (list result) |
16.map () Adds a function return value to the result
| #一个列表, plus 100 per element #1, Common methods Li = [11,22,33] Def f1 (args): result = [] For i in args: Result.append (i + 100) return result R = F1 (LI) Print (R) #输出: [111,122,133]
#2, using map: Map (functions, objects that can be iterated (things for loops)) Li = [11,22,33] Def f1 (args): return args + 100 R = Map (F1,li) Print (list (R)) #map首先循环第二个参数; second, every loop is executed once F1 function; #map将返回值赋值给r
#3, using MAP+LAMBDA expressions Li = [11,22,33] R = Map (lambda Arg:arg + P, Li) Print (list (R)) |
17.float () converts numbers to floating-point
18.format () string formatting
19.frozenset () Immutable collection
20.getsttr () Reflection
21.globals () All global variables
22.locals () all local variables
23.hash () gives a value, generating a hash value;
Commonly used for Dictionary key preservation Python first converts the key into a hash value and then saves it in memory. |
24.help () Help
25.hex () decimal Turn hex
26.id () View memory address
27.iter () Creating iterators
28.len () Viewing length
Python 3.0 default length is calculated by character Python 2.7 default length per byte
#python3.0 also supports byte calculation: s = "Yangyang" b = Bytes (s,encoding= ' Utf-8 ') Print (len (b)) Output: 6 |
29.max () Maximum value
30.min () Minimum value
31.sum () summation
32.memoryview () python3.0, view a class related to memory address.
33.next () and ITER ()-iterator-related
34.object () A class
35.OCT () hex () int () bin ()
36.open () file operation
37.pow () Seek the second party # 2**10 = = R=pow (2,10)
38.range () Range
39.REPR () Execute the method inside the object class
40.reverse () inversion (also available in list)
41.rount () rounding
42.set () setattr () set;
43.slice () python3.0 new, slicing function
44.sorted () sort (same as List li.sort ())
45.vars () What variables are available in the current module
46.zip () combines the elements of a list and a list, respectively
| #例如: L1 = [' This ', 11,22] L2 = [' is ', 33,44] L3 = [' car ', 55,66] R = Zip (L1,L2,L3) Print (list (R))
L1 = [' This ', 11,22] L2 = [' is ', 33,44] L3 = [' car ', 55,66] R = Zip (L1,L2,L3) temp = List (r) [0] ret = '. Join (temp) Print (ret) |
47.__import__ () Import module with
Second, the decoration device
1, the outline of the adorner
(Add a function without changing the contents of the function)
(When writing a program, for a function, there is an open closure principle:
Add content do not re-function internal modification, this time with the adorner to solve)
#原来是这样 Def f1 (): Print ("F1") def f2 (): Print ("F2") #例1: Now add an output log before executing the function #定义装饰器 def outer (func): def inner (): Print ("Log") return func () return inner @outer #应用装饰器 Def f1 (): Print ("F1") @outer #应用装饰器 def f2 (): Print ("F2")
F1 () F2 () ‘‘‘ Output: Log F1 Log F2 ‘‘‘
#例2: Now add an output log before executing the function, and then add an after #定义装饰器 def outer (func): def inner (): Print ("Log") ret = func () Print ("after") return ret return inner @outer #应用装饰器 Def f1 (): Print ("F1") @outer #应用装饰器 def f2 (): Print ("F2")
F1 () F2 () ‘‘‘ Output: Log F1 After Log F2 After ‘‘‘ |
2, decorator Analysis Reserve knowledge
3, the adorner process analysis
4. The return value of the adorner analysis
5, the parameters of the adorner analysis
6, the decoration analysis of the parameters of the second
7. User Management Program
This article is from "Yangdong Hao" blog, please make sure to keep this source http://506554897.blog.51cto.com/2823970/1823521
Python day four