"Python built-in functions"

Source: Internet
Author: User
Tags abs iterable ord pow vars

Built-in functions

In short, it is the function of Python3 itself.

    • ABS (x)

ABS () returns the absolute value of a number. If a complex number is given, the return value is the modulus of the complex

Print (ABS (-1100))

Output: 1100

    • All ()

If all elements of iterable are not 0, ', false, or iterable are empty, all (iterable) returns True, otherwise false is returned;

Print (All ([' A ', ' B ', ' C ', ' d '))  #列表list, elements are not empty or 0
Print (All ([' A ', ' B ', ' ', ' d ')) #列表list, there is an empty element
Print (All ([0,1,2,3])) #列表list, there is an element of 0
Print (All (' A ', ' B ', ' C ', ' d ')) #元组tuple, elements are not empty or 0
Print (All (' A ', ' B ', ', ' d ')) #元组tuple, there is an empty element
Print (All (0,1,2,3)) #元组tuple, there is an element of 0
Print (All ([])) # empty list
Print (All ()) # Empty tuple
Truefalsefalsetruefalsefalsetruetrue

Note: Empty tuple, empty list return value is true, here is to pay special attention to

    • Any ()

If all elements have a value other than 0, ' ' or false, then the result is true, and when all values of iterable are 0, ' ' or false, the result is false,

Print (Any ([' A ', ' B ', ' C ', ' d '])  #列表list, the elements are not empty or 0print (any ([' A ', ' B ', ' ', ' d '))  #列表list, there is an empty element print (any ( [0,1,2,3]))  #列表list, there is a 0 element print (any (' a ', ' B ', ' C ', ' d ')) #元组tuple, the elements are not empty or 0print (any (' a ', ' B ', ' ', ' d '))  #元组tuple, There is an empty element print (any (0,1,2,3))  #元组tuple, there is an element of 0 print (any ([])) # empty list print (any ()) # Empty tuple
Truetruetruetruetruetruefalsefalse

    • ASCII ()

Call the __repr__ () method of the object to get the return value of the method.

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

    • Bin ()

The three function functions are: Convert the decimal number to 2 binary respectively.

Print (BIN)   #0b1010

    • BOOL ()

Tests whether an object is true or false.

Print (bool ([]))    #False    

    • Bytes ()

Converts a string to a byte type

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

    • Callable (object)

The callable () function is used to test whether an object can be called, or 1 (true) if it is possible, otherwise 0 (false) is returned. Callable objects include functions, methods, code objects, classes, and class instances that have defined the calling method.

A = ' 123 ' Print (callable (a))  #False

    • Chr (i)

The CHR () function returns a string corresponding to the ASCII code.

Print (CHR) #A

    • Complex (Real[,imaginary])

The complex () function converts a string or number to a plural.

Print (complex (2,1)) # (2+1J)

    • Delattr ()

Delete an object's properties

    • Dict ()

Create a data dictionary

Print (Dict ())   #{}

    • Dir ()

Returns the current range of variables without parameters, a list of methods and defined types, the properties of the parameters returned with parameters, a list of methods

Print (dir ())
[' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' time ']

    • Divmod (x, y)

The Divmod (x, y) function completes the division operation, returning the quotient and remainder.

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

    • Enumerate ()

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

s = ["A", "B", "C"]for I, v in enumerate (s,1):     
1 A2 b3 c 

    • Eval ()

Evaluates the string str as a valid expression and returns the result of the calculation

s = "1 + 3 +5" Print (eval (s)) #9

    • EXEC ()

Executes a string or a string that is compiled by the Complie method, with no return value

    • Float (x)

The float () function converts a number or string into a floating point.

Print (Float (")") #12.0

    • Format ()

Formatting the output string

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

    • Frozenset ()

Create a non-modifiable collection

setfrozenset最本质的区别是前者是可变的,后者是不可变的。当集合对象会被改变时(例如删除,添加元素),只能使用set

一般来说使用fronzet的地方都可以使用set

Parameter iterable: The object can be iterated.

    • Globals ()

Returns a dictionary that describes the current global variable

A = "Apple" Print (Globals ())
{' __package__ ': None, ' __file__ ': '/users/hexin/pycharmprojects/py3/day4/2.py ', ' __name__ ': ' __main__ ', ' a ': ' Apple ', ' Time ': <module ' time ' (built-in), ' __cached__ ': None, ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x10bd73c88>, ' __builtins__ ': <module ' builtins ' (built-in);, ' __spec__ ': None, ' __ doc__ ': None}

    • Hash ()

Hash valuehash(object)注意:可哈希的即不可变数据类型,不可哈希即可变数据类型

如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数,在字典查找中,哈希值用于快递比价字典的键。

Two values if equal, the hash value is also equal.

    • Help ()

Returns the Help document for an object

Call the built-in Help system, and if it does not contain parameters, the interactive Help system will start in the console. If the argument is a string, it can be a name for the module, class, method, and so on, and the help page will be printed in the console. Parameters can also be any object

    • Hex (x)

The hex () function converts integers to hexadecimal numbers.

Print (hex) #0xc

    • ID ()

Returns the memory address of an object

A = "Apple" Print (ID (a)) #4562197840

    • Input ()

Get user input

    • Int (X[,base])

the int () function converts numbers and strings into an integer, base is an optional cardinality.

    • ITER ()

Returns a Iterator object.

    • The Len () function returns the string and the length of the sequence.
Print (len (' AA ')) #2

    • List (x)

The list () function converts a sequence object to a list.

Print (List ("Hello World"))
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']

    • Locals ()

Print a dictionary of the currently available local variables

    • Max (X[,y,z ...])

The max () function returns the maximum value for a given parameter, which can be a sequence.

Print (max (1,2,3,4)) #4

    • Min (X[,y,z ...])

The min () function returns the minimum value of the given parameter, which can be a sequence.

Print (min (1,2,3,4)) #1

    • Next ()

Returns the next item in a data structure (such as a list) that can be iterated

    • Object ()

获取一个新的,无特性(geatureless)对象。Object是所有类的基类。它提供的方法将在所有的类型实例中共享。

    • Oct (x)

The OCT () function converts an integer given to an octal number.

Print (Oct) #0o14

    • Ord (x)

The Ord () function returns the ASCII or Unicode value of a string parameter.

Print (Ord ("a")) #97

    • Open ()

Open Fileopen(filename [, mode [, bufsize]])

打开一个文件,返回一个file对象。 如果文件无法打开,将处罚IOError异常

    • Pow (x,y[,z])

The POW () function returns a power of x, and y as the exponent. If the z-value is given, the function calculates the value of the Y-power of x being the z-modulo.

Print (POW (2,5)) #32print (POW (2,5,3)) #2

    • Range ([Lower,]stop[,step])

The range () function generates a sequential list of ordered integers by parameter.

Print (range (1,10,2)) #range (1, 10, 2)

    • Repr ()

Converts any value to a string for the form of a timer read

    • Reversed ()

Reverse, Reverse Object

    • Round (X[,n])

The round () function returns the rounding value of the floating-point number x, if given an n value, that represents the digits rounded to the decimal point.

Print (round (5.9)) #6

    • Set ()

Convert an object to a collection

    • Slice ()

Slicing function

s = ["A", "B" "C", "D"]print (Slice (1,3,s))
Slice (1, 3, [' A ', ' BC ', ' d '])

    • Sorted ()

Sort

List sort, row by axis, higher order function, sort by absolute size, string sort, sort by ASCII size, if you need to sort a tuple, you need to use parameter key, which is the keyword. Reverse sort, reserve=true

    • STR (obj)

The STR () function converts an object into a printable string.

Print (str (4)) #4

    • SUM ()

Sum

    • Tuple (x)

The tuple () function converts a sequence object to a tuple.

Print (Tuple ("Hello World"))
(' h ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ')

    • Type (obj)

The type () function returns the data type of the object.

Print (Type (' 123 ')) print (type (1))
<class ' str ' ><class ' int ' >

    • VARs ()

This function is a Dictionary object that implements the property and property values of the object that is returned. If you do not enter parameters by default, the properties and property values of the current call location are printed, equivalent to the functionality of locals (). If there is a parameter input, only the corresponding property and property values of this parameter are printed.

Print(VARs ()) #{' __name__ ': ' __main__ ', ' __spec__ ': None, ' __package__ ': None, ' __builtins__ ': <module ' builtins ' (built-in), ' time ': <module ' time ' (built-in), ' __cached__ ': None, ' __doc__ ': None, ' __file__ ': '/users/hexi n/pycharmprojects/py3/day4/2.py ', ' __loader__ ': <_frozen_importlib_external. Sourcefileloader object at 0x10e5f3c88>}
Print (VARs (time))
View Code

    • Zip ()

Pair the objects individually

s= ' Helloo 'l=[1,2,3,4,5]z=zip (s,l) print(z) for i in z:    print (i)    
<zip object at 0x1051d1608>(' h ', 1) (' E ', 2) (' L ', 3) (' L ',4) ('o ', 5)    

"Python 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.