Python learning diary: day15: ------ built-in functions, pythonday15
1. Scope-related
1) locals () --------- obtain the dictionary of local variables in the namespace in which the method is executed
# Return all names in the local scope
2) globals () -- get the dictionary of global variables
# Return all names in the global scope
2. String-Type Code Execution
Eval () executes the code of the string type and returns the result.
print(eval('1+2+3+4')
Exec () executes code of the string type
print(exec("1+2+3+4"))exec("print('hello,world')")Compile Compilation
# Execcode1 = 'for I in range (): print (I)' compile1 = compile (code1, '', 'exec ') exec (compile1)
# Simple evaluate expression evalcode2 = '1 + 2 + 3 + 4' compile2 = compile (code2, '', 'eval') eval (compile2)
# Singlecode3 = 'name = input ("please input your name:") 'compile3 = compile (code3, '', 'single') name # The name variable does not exist before execution
Exec (compile3) # The interactive command is displayed during execution, prompting you to enter
3. iterator/generator correlation (3)
# Iterator. _ next __()
# Next (iterator)
# Iterator = iter (iteratable)
# Iterator = iteratable. _ iter __()
4. Input/Output
Input () input
S = input ("Enter the content:") # The input content is assigned to the s Variable print (s) # input and print. The data type is str.
Print () output source code parsing
Def print (self, * args, sep = '', end = '\ n', file = None ): # known special case of print "" print (value ,..., sep = '', end = '\ n', file = sys. stdout, flush = False) file: The default value is output to the screen. If it is set to a file handle, output to the file sep: The separator between multiple values is printed. The default value is space end: at the end of each print, the default line break is flush: Immediately output the content to the stream file, not cached """
5. Data Types:
Type (a) returns the Data type of variable.
6. Memory related:
Id (o) o is a parameter and returns the memory address of a variable.
Hash (o) o is a parameter and returns a hash value of the hash variable. If a non-hash variable is hashed, an error is returned.
# Hash-the hash values of the same hash data remain unchanged during a Program Execution
#-Dictionary addressing
T = (, 3) l = [, 3] print (hash (t) # hashprint (hash (l) # The '''result is: TypeError: unhashable type: 'list''' hash instance
7. File Operations
Open () open a file and return a file operator (file handle)
There are 6 file operation modes: r, w, a, r +, w +, and a +. Each mode can be operated in binary format (rb, wb, AB, rb +, wb +, AB +)
You can use encoding to specify the encoding.
8. Module operations
_ Import a module
9. Help
Help
Run help () on the console to enter the help mode. You can enter variable or variable type at will. Enter q to exit
You can also directly execute help (o) and o is a parameter to view the operations related to variable o...
# Differences between help and dir: (summarized by myself)
Dir () view the method name (not very detailed help) help view the method name and usage (more detailed help)
10,And call-related
Callable (o) and o are parameters. Check whether the variable is callable.
If o is a function name, True is returned.
Def func (): passprint (callable (func) # The parameter is the function name and can be called. Trueprint (callable (123) is returned. # The parameter is a number and cannot be called. False is returned.
11. Related to numbers
Number -- Data Type correlation: bool, int, float, complex
Number-hexadecimal conversion related: bin, oct, hex
Number-mathematical operations: abs, divmod, min, max, sum, round, pow # print (bin (10)-binary# Print (oct (10) -- octal
# Print (hex (10) -- hexadecimal
# Abs () returns the absolute value.
# Divmod () Division-> div division, mod Division
# Pow (2, 3)-> 2 ** 3 Power Calculation
# Pow (2, 3, 4) --> 2*3% 4, the last number is the remainder. Remainder after power operation
12, max
print(max([1,2,3,4]))print(max(1,2,3,4))print(max(1,2,3,-4))print(max(1,2,3,-4,key = abs))
13, min
print(min([1,2,3,4]))print(min(1,2,3,4))print(min(1,2,3,-4))print(min(1,2,3,-4,key = abs))