Built-in Functions (Python3 built-in function)

Source: Internet
Author: User
Tags string format

Tag: Int () min () sum () built-in functions

There are many built-in functions in Python that make us more efficient and summarize some of the relevant knowledge here.

Website address: https://docs.python.org/3.6/library/functions.html


Built-in Functions


abs () dict () help () min () setattr ()
all () dir () hex () next () slice ()
any () divmod () id () object () sorted ()
ascii () enumerate () input () oct () staticmethod ()
bin () eval () int () open () str ()
bool () exec () isinstance () ord () sum ()
bytearray () filter () issubclass () pow () super ()
bytes () float () iter () print () tuple ()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod () getattr () Code class= "XRef py py-func docutils literal" >locals () REPR () zip ()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()

Number and numeric correlation functions

Numeric and numeric functions The main operands are numbers or numeric values, such as ABS (), complex () functions, etc. these operands are directly numeric.

ABS () function

Returns the absolute value of a number. The parameter may be an integer or a floating-point number. If the argument is a complex number, returns its size.

Print (ABS (3.544564)) print (ABS ( -3.544564)) print (ABS ( -4.8)) print (ABS (8454)) Comp=complex (4,5) #定义复数4 +5jprint (comp) #打印复数4 +5jprint (comp) #对复数取绝对值comp2 =complex ( -4,-5) #定义复数4 +5jprint (COMP2) #打印复数4 +5jprint (ABS (COMP2)) # Absolute value of complex number execution result: 3.5445643.5445644.88454 (4+5j) 6.4031242374328485 ( -4-5j) 6.4031242374328485

Bin () function

Converts an integer to a binary string, and the result is a valid Python expression. If x is not a Python int object, it must define a __index__ () method, which returns an integer.

Print (Bin (9988)) print (bin) print (Bin (2)) 0B100111000001000B101000B10

Complex () function

Defining a complex number requires passing in two parameters such as complex ([X],[y]) The return result is x+5j (in mathematics, I is the plural, but in engineering mathematics inside is J for plural)

Print (complex (3.544,-5)) print (complex (4,0)) print (complex (0,0)) Results: (3.544-5j) (4+0J) 0j

Hex ()

Converts an integer to a lowercase hexadecimal string prefixed with "0 x", for example:

Print (hex) print (Hex ( -20)) print (Hex (2)) execution result 0x63-0x140x2

Oct ()

The integer is converted to a string of eight decimal digits. The result is a valid Python expression. If x is not a Python int object, it must define a __index__ () method, which returns an integer. Returns the octal at the beginning of the value 0o

Example

Print (Oct) print (Oct ( -20)) print (Oct) 0o143-0o240o14

Int ()

the int () function can convert a number to an integer.

Use:

You can convert numbers in string format to integers, such as input values that are used frequently;

>>> a=input ("WJHWHH:") wjhwhh:wjwjj>>> a=input ("Input a number:") input a number:66>>> print (a) 66>>> type (a) <class ' str ' >>>> the number entered is a string format, we can use the Int () function to convert >>> s=int (a) >>> s66>>> type (s) <class ' int ' >>>>

You can convert a floating-point number to an integer

>>> Int (54.44454) 54>>> int (4455) 4455>>> int (-887.66)-887

You can convert 16-, 8-, 2-binary, and other format numbers into 10-binary

2 binary is starting with 0b: For example: 0B11 means 3 decimal

8 binary is starting with 0: For example: 011 means 9 of decimal

16 binary is starting with 0x: For example: 0x11 means 17 decimal


Basic syntax: (Note that the parameters here are usually in the form of a string, so you usually need double quotes)

int(x, base=10)

int(‘010‘, 8)

>>> int ("FF", +) 255>>> >>> int ("FF2", 16) 4082


Float ()

The float () function converts some other types of numbers to floating-point

>>> Float (3) 3.0>>> float (-456)-456.0


Min () and Max ()

The min () and Max () functions can find minimum and maximum values such as characters, list elements, collection elements, and so on. However, the type of the element should be the same type.

For string   is to parse each character according to ASCII code >>> s= "WINNERLOKKJKSBLAFCB" >>> min (s) ' A ' >>>  max (s) ' W ' List of element types are all numbers (shaping and floating point) >>> list2=[2,3,2.45,-100,-99,110]>>> min (LIST2)- 100>>> max (List2) 110>>>  element type contains shaping, floating point, complex number will be error >>> list2=[2,3,2.45,-100 , -99,110,complex (3,6)]>>> list2[2, 3, 2.45, -100, -99, 110,  (3+6j) ]>>> min (List2) traceback  (most recent call last):  file  "< Pyshell#41> ",  line 1, in <module>    min (list2) TypeError:  unorderable types: complex ()  < int () >>> list2=["winner",998]    #元素为不同类型元素 >>> min (list2) traceback  (most recent call last):   file  "<pyshell#43>",  line 1, in <module>    min (List2 ) typeerror: Unorderable types: int ()  < str () 

Parameter object recognition function

Type () function

The type function is generally used to determine the data type of an object and can be judged by the format of the object we define.

>>> type (list2) <class ' list ' >>>> type (a) <class ' int ' >

The Len () function determines the element length of an object

Help () function to see how a content is used

The Dir () function to see which methods are included in a function

ID () to see the address space of an object, the difference between two objects can be distinguished by judging the address space.

Isinstance () function

It can be used to determine the data type, the return value is a bool value, and the passed parameter is the object and data type format to be judged.

Grammar:

Isinstance (obj, class_or_tuple,/)

and the type () function can be used to determine the format of an object, but there are some differences in usage, and the isinstance () return value is a bool, judging by the type of data you enter,

>>> sy={1994: "Baidu", 1995: "Tenxun"}>>> isinstance (sy,dict) true>>> isinstance (sy,dict) True>>> isinstance (sy,list)


Built-in Functions (Python3 built-in function)

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.