Python built-in functions _ Mathematical operations Classes

Source: Internet
Author: User
Tags iterable modulus pow

This article and everyone to share is mainly python built-in function data operations related content, come together to see it, hope to learn Python to help you.

Abs

ABS (x)

Seek absolute value

· X can be an integer, or it can be a complex number

· If x is a complex number, the modulus of the complex number is returned

>>> ABS ( -1) 1>>> abs ( -3+4J) 5.0>>>

Bin

Bin (x)

Converts an integer x to a binary string

>>> Bin (2) ' 0b10 ' >>> bin (3) ' 0B11 '

bool

BOOL ([x])

Convert X to Boolean type

In Python, the number 0 , the empty string , or none all represent False .

So there are the following operating effects

>>> bool (None)false>>> bool (1)True>>> bool (0)false >>> bool (')True>>> bool (')false>>> bool ()false

Complex

Complex ([real[, Imag]])

Creates a complex object that has important two properties, real and imag ,

Representing real and imaginary parts respectively, and calculating the modulus of complex objects can be used ABS function

Related operations for complex numbers and Cmath modules

>>> a = complex (4) >>> a

(4+0j) >>> B = Complex (4,3) >>> b

(4+3j) >>> a.real #实部4 .0>>> b.imag #虚部3 .0>>> Complex (' 2 ')

(2+0J) >>> complex (' 2+1j ')

(2+1j)

Divmod

Divmod (A, B)

A is dividend, B is divisor, and the result of division AB is the quotient and remainder respectively.

Note: integral, floating-point types can be

Returns a tuple that contains quotient and remainder

>>> divmod(5,3)

(1, 2)

Float

float ([x])

Converts a string or number to a float. If no parameter is returned 0.0

>>> float (' 37.21 ') 37.21>>> float (0x33) 51.0>>> float (' -1234\n ')

-1234.0>>> float (' 1e-003 ') 0.001>>> float (' +inf ')

inf>>> float ('-inf ')

-inf>>> float () 0.0

Hex

Hex (x)

Convert a number to 16 binary

>>> Hex (+) ' 0x10 '

Int

int ([x[, Base]])

You can convert a floating-point or numeric character to a specified binary integer

Converts a character to an int type, and base represents the binary

>>> int (' + ', +) 18>>> int (' 2 ', +) 2>>> int (' 2 ', 8) 2>>> int (' 0xa ', +) 10>> > Int (12.0) 12

Long

Long ([x[, Base]])

You can convert a floating-point number or integer character to a long integer that is specified as a binary.

Converts a character to a long type , and base represents the binary

Long and short integers are not strictly distinguished in Python, and the Integer is followed by L when it represents a long integer, or greater than 2 147 483 647 represents the long integer type

>>> Long (' 2 ', 8) 18l>>> long (' 2 ', 2l>>>) 2l>>> long (' 0xa ', 10L) >>> Long (12.0) 12l>>> type (2147483647)

>>> type (2147483648)


Oct

Oct (x)

Convert a number to 8 binary

>>> Oct (16) ' 020 '

Pow

Pow (x, y[, z])

The function is to calculate the Y-side of X, and if z is present, the result is modeled and the result is equivalent to pow (x, y)%z

Note: The POW () is called directly through the built-in method, and the built-in method takes the parameter as an integer, and the math module converts the parameter to float.

>>> Pow (2,2,2) 0>>> pow (2,2) 4>>> import math>>> type (Math.pow (2,2))

>>> type (POW (2,2))


Round

Round (x[, N])

Rounded

X represents the original number, and N represents the number of decimal digits to be obtained.

>>> round (math.pi,2) 3.14>>> round (math.pi,7) 3.1415927

Sum

Sum (iterable[, start])

Sum the collection elements

Iterable represents the set, if there is a start for subscript, the last number does not take

>>> L = Range (5) >>> L

[0, 1, 2, 3, 4]>>> sum (l) 10>>> sum (l,-4) 6

Source: Blog Park

Python built-in functions _ Mathematical operations Classes

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.