Built-in functions

Source: Internet
Author: User
1. Scope-related
  • Locals ()

Function: returns the name in the scope.

  • Globals ()

Function: returns the name in the global scope.

2. Related to the iterator/Generator
  • Range ()

Function: generate data

lst = [i for i in range(10)]print(lst)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

  • ITER ()

Function: gets the iterator. The _ ITER _ () method is used internally to obtain the iterator.

  • Next ()

Function: The iterator is executed once. The _ next _ () method is used internally to return to the next project of the iterator.

LST = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ret = ITER (LST) # Get iterator print (next (RET )) #0 execute iterator
3. Execution of string-type code
  • Eval ()

Function: Run string-type code. And return the final result.

print(eval("[11, 22, 33, 44]"))  # [11, 22, 33, 44]print(eval("2+2"))  # 4def func():    print(666)eval("func()")  # 666
  • Exec ()

Function: executes string-type code.

Exec ("" For I in range (10): Print (I )""") #0 1 2 3 4 5 6 7 8 9 exec ("def func (): Print (" I am selia ") func ()""") # I'm Silea
  • Compile ()

Function: Compile the code of the string type. The code in the form of a string with a return value is evaluated using eval (). The code in the form of a string without a return value is executed using exec ().

Parameter description:

1) Resource: code to be executed, dynamic code snippets

2) file name: the name of the file stored in the Code. When the first parameter is input, this parameter can be left blank.

3) mode, with three values:

A) Exec: when some process statements are put

B) Eval: resource only stores one evaluate expression.

C) Single: when the resource stored code interacts with each other, the mode should be single, but only one line of the Code is allowed. Otherwise, an error is returned.

Code = "For I in range (5): Print (I)" "C1 = compile (Code," ", mode =" EXEC ") exec (C1) #0 1 2 3 4code2 = "name = input (" enter your name: ")" "C2 = compile (code2 ,"", mode = "single") exec (C2) print (name) # Tom
4. Input and Output
  • Input ()

Function: Get user input

  • Print ()

Function: print the output.

5. Memory-related
  • Hash ()

Function: obtains the hash value (INT, bool, STR, tuple) of an object)

Note:

1) the hash value of a number is itself

2) the hash value obtained by calling Hash () is different (except for numbers), so the hash value cannot be used as a password.

Num = 10 s = "hello" Print (Hash (Num) #10 # print (Hash (s) for the first call #3718209680369926059 # print (Hash (s) for the second call )) #263127024043316082
  • ID ()

Function: gets the object's memory address.

6. File-related
  • Open ()

Function: used to open a file and create a file handle.

7. Module-related
  • _ Import __()

Function: Used to dynamically load classes and functions

8. Help
  • Help ()

Function: view detailed descriptions of functions or modules.

9. Call related
  • Callable ()

Function: checks whether an object is callable. If true is returned, the object may fail to be called, but if false is returned, the call will never succeed.

n = 10def func():    passprint(callable(n)) # Falseprint(callable(func)) # True
10. View built-in attributes
  • Dir ()

Function: view the built-in attributes and methods of an object. The _ DIR _ () method of the object is accessed.

11. Basic Data Types 1) and numbers a) Data Types
  • Bool ()

Function: converts a given data to a bool value. If no value is specified, false is returned.

  • INT ()

Function: converts a given data to an int value. If no value is given, 0 is returned.

  • Float ()

Function: converts a given data to a float value, that is, a decimal number.

  • Complex ()

Function: create a complex number. The first parameter is the real part, the second parameter is the virtual part, or the first parameter is used to describe the complex number directly.

B) hexadecimal conversion
  • Bin ()

Function: converts a parameter to a binary value.

  • OTC ()

Function: converts a parameter to an octal value.

  • Hex ()

Function: converts a parameter to a hexadecimal value.

C) mathematical operations
  • ABS ()

Function: returns the absolute value.

  • Divmod ()

Function: Return operator and remainder

  • Round ()

Function: Rounding

For X.5, if the number of digits of an integer is an even number near the even number, the number of digits of an integer is discarded if the number of digits of an integer is an odd number near the even number.

print(round(4.5)) # 4print(round(5.5)) # 6print(round(5.4)) # 5
  • Pow (A, B)

Function: Calculate the power of B of A. If there are three parameters, the remainder of the third number is obtained after the power is obtained.

print(pow(2, 3)) # 8print(pow(2, 3, 5)) # 3
  • Sum (iteratable object)

Function: Sum

  • Min ()

Function: Minimum value

  • Max ()

Function: calculates the maximum value.

2) A) List and metadata related to the Data Structure
  • List ()

Function: converts an iteratable object to a list.

  • Tuple ()

Function: converts an iteratable object to a metagroup.

  • Reversed ()

Function: returns the iterator of the flip sequence.

lst = [1, 2, 3, 4, 5, 6]ret = reversed(lst)print(ret)  # <list_reverseiterator object at 0x000001B589038208>for i in ret:    print(i)  # 6 5 4 3 2 1
  • Slice

Function: slice of the List

lst = [1, 2, 3, 4, 5, 6]s = slice(1, 5, 2)print(lst[s])  # [2, 4]
B) string-related
  • STR ()

Function: converts data into strings.

  • Format ()

Function: related to specific data, used to calculate various decimals, actuarial, etc.

# String print (format ('test', '<20') # Left-aligned print (format ('test','> 20 ')) # Right-aligned print (format ('test', '^ 20') # center # value print (format (3,' B '))#? Hexadecimal print (format (97, 'C') # convert to the Unicode Character print (format (11, 'D '))#? Hexadecimal print (format (11, 'O '))#? Hexadecimal print (format (11, 'x '))#? Hexadecimal (? Write letters) print (format (11, 'x '))#? Hexadecimal (? Write letters) print (format (11, 'n') # print (format (11) like D # Same as D # print (format (123456789, 'E') # scientific notation. print (format (123456789, '0. 2e') # scientific notation. retain 2 decimal places (lower case) print (format (123456789, '0. 2e') # scientific notation. retain 2 decimal places (uppercase) print (format (1.23456789, 'F') # decimal point count. retain 6 decimal places print (format (1.23456789, '0. 2f ') # number of decimal points. retain 2 decimal places print (format (1.23456789, '0. 10f') # number of decimal points. print (format (1.23456789e + 10000, 'F') with 10 decimal places.
  • Bytes ()

Function: converts a string to a bytes type.

S = "hello" BS = bytes (S, encoding = "UTF-8") print (BS) # B '\ xe4 \ xbd \ xa0 \ xe5 \ xa5 \ xbd'
  • Bytearray ()

Function: returns a new byte array. The elements in this array are variable and the value range of each element is [0,256].

ret = bytearray(‘alex‘, encoding=‘utf-8‘)print(ret)  # bytearray(b‘alex‘)ret[0] = 65print(ret) # bytearray(b‘Alex‘)
  • Memoryview ()

Function: view the memory usage of bytes.

S = "hello" ret = memoryview (S. encode ("UTF-8") print (RET) # <memory at 0x0000022fc0071048>
  • Ord ()

Function: Enter characters to find the location with character encoding

  • CHR ()

Function: enter a number to find the corresponding character

  • ASCII ()

Function: return this value in the ASCII code. If the value is not in the ASCII code, \ U... is returned...

S = "hello" Print (ASCII (s) # '\ u4f60 \ u597d'
  • Repr ()

Function: returns the official representation of an object.

S = "Hello \ My Name Is cyliya" Print (Repr (s) # 'Hello \ My Name Is cyliya 'print (s) # Hello \ My Name Is cyliya
C) Data Set
  • Dict ()

Function: Creates a dictionary.

  • Set ()

Function: Creates a set.

  • Frozenset ()

Function: Creates a frozen set. Frozen sets cannot be added or deleted.

D) other related
  • Len ()

Function: returns the number of elements in an object.

  • Sorted ()

Function: sorts iteratable objects.

  • Enumerate (iteratable object, start = 0)

Function: obtains enumeration objects of a set.

LST = [11, 22, 33] For index, El in enumerate (LST): Print (index, El) Result: 0 111 222 33
  • All ()

Function: all objects that can be iterated are true, and the result is true.

print(all([1, 2, 0, True])) # False
  • Any ()

Function: if one of the iteratable objects is true, the result is true.

print(any([1, 2, 0, True])) # True
  • Zip ()

Function: a function is used to package the corresponding elements of an object into tuples and return a zip object consisting of these tuples. If the elements of each iterator are inconsistent, the returned object length is the same as that of the shortest object.

lst1 = [1, 2, 3]lst2 = ["a", "b", "c", "d"]lst3 = (11, 22, 33, 44, 55)print(zip(lst1, lst2, lst3))       #  <zip object at 0x000001E7D275BF48>print(list(zip(lst1, lst2, lst3))) #[(1, ‘a‘, 11), (2, ‘b‘, 22), (3, ‘c‘, 33)]for i in zip(lst1, lst2, lst3):    print(i)                       # (1, ‘a‘, 11)  (2, ‘b‘, 22) (3, ‘c‘, 33)

The built-in function structure is as follows:

Https://www.processon.com/mindmap/5bdbfba6e4b0e452133837a3

Built-in functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.