Python data type conversion

Source: Internet
Author: User
Tags floor division
Python type conversion function description python type conversion

Function description

Int (x [, base]) converts x to an integer.

Long (x [, base]) converts x to a long integer.

Float (x) converts x to a floating point number.

Complex (real [, imag]) creates a plural number

Str (x) converts object x to a string

Repr (x) converts object x to an expression string

Eval (str) is used to calculate a valid Python expression in a string and return an object.

Tuple (s) converts the sequence s into a tuples

List (s) converts sequence s into a list

Chr (x) converts an integer to a character

Unichr (x) converts an integer to a Unicode character

Ord (x) converts a character into its integer

Hex (x) converts an integer into a hexadecimal string.

Oct (x) converts an integer into an octal string


The sequence supports the following operations:

Operation description

S + r sequential join

S * n, n * s n copies, n is an integer

S % d string formatting (string only)

S [I] Index

S [I: j] slice

X in s, x not in s subordination

For x in s: Iteration

Len (s) length

Min (s) minimum element

Max (s) maximum element

S [I] = x is s [I] re-assigned

S [I: j] = r: returns the list fragment value.

Del s [I] deletes an element from the list.

Del s [I: j] deletes a clip from the list.


Numeric operation:

X <y shifts left

X> y shift right

X & y

X | y by bit or

X ^ y exclusive or)

~ X flip by bit

X + y Plus

X-y subtraction

X * y multiplication

X/y

X // y floor Division

X ** y multiplication (xy)

X % y modulo (x mod y)

-X changes the symbol bit of the operand.

+ X does nothing.

~ X ~ X =-(x + 1)

Abs (x) absolute value

Pmod (x, y) returns (int (x/y), x % y)

Pow (x, y [, modulo]) returns (x ** y) x % modulo

Round (x, [n]) rounding, n is the number of decimal places

X <y is less

X> y is greater

X = y equals

X! = Y is not equal to (same as <>)

X> = y is greater than or equal

X <= y is less than or equal

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.