Built-in functions
Python built-in function is provided, the function can be used directly, such as print, input, etc.
First, the scope of the relevant
locals () returns the name of the current scope
globals () Returns the name of the global scope
Second, the associated iterator
range () generates data
next (iterator) iterator performs a down, the actual call inside the __next __ () method
ITER (iterables) obtaining an iterator, the actual call inside the __iter __ () method
Third, other
1. The input associated with the output
input () get user input the amount of content
print () print output, print () Default parameter sep spaces, showing the connection of each variable print, end default \ n
2. memory-related
hash () of the object acquired hash value (int, str, bool, tuple), hash algorithm is the only purpose of the hash table is a space for space, so the memory consuming
id () to get the memory address of an object
3. File-related operations
open () to open a file, create a file handle
4. Related module
__inport __ () function for dynamically loading classes and
5. Help
help () function or module for viewing purposes detailed description
6. Check the built-in properties
Built-in attributes dir () to view objects
7. Call related
callable () is used to check whether an object can be called. If it returns True, object likely call failed, but returns False, it must not be called
8. The execution of the code string type
eval () execution string type code, and returns the final result, so that code must return a value. You can only perform simple code execution
exec () execution string type of code, can perform complex code execution, when the write internet or the coding will be used by the user exec ()
() Statement is executed or evaluated eval compile () of the string type code compilation, code objects through exec ()
# The compile (parameter 1, parameter 2, parameter 3) '' ' Parameters: 1.resource, to execute the code, dynamic code fragment 2. file name, the file name stored in the code, when the first parameter passed when this parameter from time to 3. mode, the value there are 3 ways 1.exec: when the general put a program statements. 2.eval: resource store a evaluated expression. 3.single: the code stored there when the resource interact. '' ' Of code1 = " for I in Range (10): Print (I) " C1 = the compile (of code1, "" , MODE = " Exec " ) Exec (C1) code2 = " . 1 + 2 +. 3 " C2 = the compile (code2, ""eval " ) A = eval (c2) Print (A) code3 = " name = the INPUT ( " Please enter your name: " ) " c3 = the compile (code3, " " , the MODE = " SINGLE " ) Exec (c3) Print (name)
Return a string of code with the value of the eval (), does not return a value code string with Exec (), seldom used compile (), compile the source code for the package, the user does not know the internal code.
Fourth, the basic data types
1. Digital Correlation
bool () given bool value data into, if not to a value, returns False
int () given data into int value, if not to a value, returns 0
float () given data into float value, i.e. the decimal
Complex () creates a complex, the real part of the first parameter, the second imaginary part. Or the first argument string directly complex described
2. hexadecimal conversion
bin () will be converted into a binary parameter
otc () parameter will be converted into octal
hex () parameter will be converted into a hexadecimal
3. Math
abs () returns the absolute value of
divmode (dividend, divisor) Returns the quotient and the remainder in the form of tuples
round () rounding (five off and six)
POW (a, b) of a b-th power demand, if there are three parameters, the time required after completion of the power take more than the third number
sum () sums
min () selecting the maximum value
max () for the minimum
4. lists and tuples associated
list () will be converted into a list iterable
tuple () will be converted into a iterable tuple
the reversed () a reverse sequence, flipped returns an iterator sequence (returns a new, original does not change)
slice () slice of the list
= ST " Hello everyone, I am a small black " S = Slice (1,5,2 ) Print (ST [S])
5. Related string
str () to convert the data into a string
the repr () Returns the string form of the object, i.e. is output, and quote escape character does not work as \ n, \ t, \ r, but whatever%.
the ord () character input, to find the position of the character in the encoding table
CHR () position of the digital input, find the corresponding character in the encoding table
ascii () determines whether ascii table, returns to itself, is not returned \ u.
bytes (str, encoding = "code") the string into bytes Type
ByteArray () Returns a new byte array, a variable byte elements, and each element value is a range (0,256)
memoryview () view the situation bytes in memory
format () associated with the specific data used to calculate the various decimal, actuarial
# String S = " little black " Print (the format (S, " ^ 20 is " )) # centrally Print (the format (S, " <20 is " )) # Left Print (the format (S, " > 20 is " ) ) # right justified # value Print (the format (. 3, ' B ' )) # binary Print (the format (97, ' C ' )) # converted to unicode characters Print (the format (. 11, 'D ' )) # decimal Print (the format (. 11, ' O ' )) # octal Print (the format (. 11, ' X ' )) # hexadecimal (lowercase letters) Print (the format (. 11, ' X- ' )) # hexadecimal (capital letters) Print (the format (. 11, ' n- ' )) # and d as Print (the format (. 11)) # and d as # float Print (the format (123456789, ' E ' )) # Scientific notation, a default retention six decimal places Print (the format (123456789, ' 0.2 e ' )) # scientific notation, the default 2 decimal places (lowercase E) Print (the format (123456789, ' 0.2 e ' )) # SCIENCES counting method, the default 2 decimal places (uppercase E) Print (the format (1.23456789, ' F ' )) # decimal notation, to retain six decimal places Print (the format (1.23456789, ' 0.2f ' )) # decimal notation, reserved 2 decimals print (the format (1.23456789, ' 0.10f ' )) # decimal notation, to retain 10 decimal print(the format (1.23456789e + 10000, ' F. ' )) # decimal notation, is large outputs INF infinity
6. Data collection
dict () to create a dictionary
set () to create a collection
frozenset () to create a collection of frozen, freeze the collection can not add and delete operations
7. Other relevant
enumerate () Gets an enumeration object collection
LST = [ " little black 1 " , " little black 2 " , " little black. 3 " ] for index, EL in the enumerate (LST, 1): # acquiring and indexing elements together, the default indexes starting from 0, where into 1. Print (index) Print (EL) results: 1 small black 1 2 small black 2 3 small black 3
all () iterables in all True, result is True
any () iteration object can be as long as one is True, the result is True
zip () function is used as a parameter iterables, the corresponding element of the object packed into a tuple and returns a list of tuples opening thereof. If the number of elements of each iterator inconsistency, the length of the shortest list list object is returned the same.
l1 = [1,2,3,] l2 = ['a','b','c',5] l3 = ('*','**',(1,2,3)) for i in zip(l1,l2,l3): print(i) 结果: (1, 'a', '*') (2, 'b', '**') (3, 'c', (1, 2, 3))