Built-in functions within Python3

Source: Internet
Author: User
Tags decimal to binary instance method iterable numeric value

(1) ABS (), absolute value or complex number of modules

1 Print (ABS (-6))
#>>>>6

(2) All () accepts an iterator that returns true if all the elements of the iterator are true, otherwise returns false

1 Print (All ([1,0,3,6]))
#>>>>false

(3) any () accepts an iterator that returns true if an element in the iterator is true, otherwise returns false

1 print (Any ([0,0,0,[]])
#>>>>false

(4) ASCII () invokes the __repr__ () method of the object to obtain the return value of the method.

1 Print (ASCII ([1,2,3,1,22,123]))
#>>>>>[1, 2, 3, 1, 22, 123]

(5) Bin (), convert decimal to Binary

1 Print (Bin (10))
#>>>>>0b1010

(6) Oct (), convert decimal to octal

1 Print (Oct (7))
#>>>>>>0o7

(7) Hex (), converting decimal to hexadecimal

1 Print (Hex (15))
#>>>>>>0xf

(8) BOOL () tests whether an object is true or false.

1 print (bool ([]))
#>>>>>false

(9) bytes () converts a string to a byte type

1 s= "Apple"
2 v=bytes (s,encoding= "Utf-8")
3 print (v)
#>>>>>>b ' Apple '

(a) str () converts character type/numeric type to String type

1 s=100
2 print (type (s))
#>>>><class ' int ' >
3 s=str (s)
4 print (type (s))
#>>>><class ' str ' >

(one) challable () determines whether an object can be invoked, and the object that can be invoked is a Callables object, such as a function

1 Print (callable (str))
#>>>>>true

() Chr () view the ASCII character corresponding to the decimal number

() Ord () to view the decimal number for an ASCII

1 Print (ASCII (12323))
#>>>>12323

(Classmethod) A method used to specify a method as a class, executed directly by a class, with only one CLS parameter that automatically assigns the class that calls the method to the CLS when executing the method of the class. The method of the class that is not specified by this parameter is an instance method.

() Complie () compiles the string into code that can be recognized or executed by Python, or it can be read as a string and then compiled.

Compile (source, filename, mode, flags=0, Dont_inherit=false, Optimize=-1)

Compiles source to code or AST object. The code object can be evaluated through EXEC statements or eval ().

Parameter source: A string or an AST (abstract syntax trees) object.

Parameter filename: The name of the code file that passes some recognizable value if it is not read from the file.

Parameter model: Specifies the type of code to compile. You can specify ' exec ', ' eval ', ' single '.

Parameters flag and Dont_inherit: These two parameters are optional parameters.

1 s  = "print (' HelloWorld ')"
2 R = Compile (s, "<string>", "exec")
3 print (R)
#>>>>>>><code object <module> at 0x0000000000b426f0, file ' <string> ', line 1>

() complex () to create a complex number that has a value of real + Imag * J or to convert a string or plural. If the first argument is a string, you do not need to specify a second argument.

1 Print (Complex ("123"))
#>>>> (123+0J)

() delattr () deletes the properties of an object

(dict) Create a data dictionary

1 print (Dict ()) #>>>>>>{}

Dir () returns a list of variables, methods, and defined types in the current scope without parameters, a property of the parameter when the parameter is returned, and a list of methods.

1 Print (dir ()) #>>>>>[' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' _ _package__ ', ' __spec__ ', ' R ', ' s ']

(a) Divmod (), respectively, for the quotient and remainder

1 Print (Divmod (10,3))
#>>>>> (3, 1)

Enumerate () Returns an object that can be enumerated, and the next () method of the object returns a tuple.

1 s = ["A", "B", "C"]
2 for I, V in Enumerate (s,1):
3     print (I,V)
#>>>>>1 a  2 b   3 C

Eval () 1. Evaluates the string str as a valid expression and returns the result of 2. Remove the contents of the string

1 s = "1 + 3 +5"
2 print (eval (s))
#>>>>>>9

(a) Exec () executes a string or a string compiled by the Complie method, with no return value

Filter () filters, constructs a sequence that is equivalent to [item for item in Iterables if function (item)], sets the filter condition in the function, loops through the elements in the iterator, and leaves the element with the return value true. form a filter type data.

Filter (function, iterable)

Parameter function: Returns a function with a value of true or FALSE, which can be none.

Parameter iterable: A sequence or an iteration object.

1 def uno (x):
2 return     x >
3 v=filter (uno,[1,11,2,45,7,6,13])
4 print (v)
#>>>>><filter Object at 0x0000000001143160>

() float () tells a string or integer to be converted to a floating-point number.

1 Print (float (11))
#>>>>>11.0

Format () formats the output string, and format (value, Format_spec) is essentially the __format__ (Format_spec) method that invokes value.

1 Print ("I am {0},age{1}". Format ("Tom", 18))
#>>>>>>i am Tom,age18

(Frozenset) Creates a set that cannot be modified.

Frozenset ([iterable])

The most essential difference between set and Frozenset is that the former is variable and the latter is immutable. When the collection object is changed (for example, deleting, adding elements), you can only use set,

Set can be used where Fronzet is generally used.

Parameter iterable: The object can be iterated.

() GetAttr () Gets the properties of the object

GetAttr (object, name [, Defalut])

Gets an attribute with the object named name, and if object does not contain an attribute named name, throws a Attributeerror exception; if the attribute named name is not included

And the default parameter is supplied, the default is returned.

Parameter object: Object

Parameter name: attribute name of Object

Parameter default: Default return value

(globals) returns a dictionary that describes the current global variable

1 A = "Apple"
2 print (Globals ()) #>>>>>{' __name__ ': ' __main__ ', ' __package__ ': None, ' __cached__ ': None, ' A ': ' Apple ', ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x00000000006a5b70>, ' __file__ ': ' d:/pycharm/pythons3/built-in functions. py ', ' __spec__ ': None, ' __ Builtins__ ': <module ' builtins ' (built-in), ' __doc__ ': None}

(a) hasattr (), hasattr (Object,name) Determines whether an object contains an attribute named name (hasattr by invoking GetAttr (Object,name)) to throw an exception.

1 Print (Hasattr (dict, "get")
#>>>>true

Hash () hash value hash (object) Note: Can be hashed that is immutable data type, not hash can be variable data types

If object is a hash table type, returns the hash value of object. The hash value is an integer, and in the dictionary lookup, the hash value is used for the key of the Express parity dictionary.

Two values if equal, the hash value is equal.

(+) Help () returns the helper document for the object

Call the built-in Help system, and if no parameters are included, the interactive Help system starts at the console. If the argument is a string, it can be a module, class, method, and so on, and the help page will print on the console. Parameter can also be any object

() ID () returns the memory address of the object

1 A = 1
2 print (ID (a))
#>1519780304

() input () get user input

1 Print (Input ("AAA")
#>>>>>aaa

() int () converts a string or numeric value to a

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.