Python Basic Type operations

Source: Internet
Author: User

Source: http://blog.csdn.net/yuxinleipp/article/details/7868924Type conversion One, type conversion function in Python

Function description

int (x [, Base]) converts x to an integer (where base represents cardinality)

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

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

Complex (real [, Imag]) creates a complex number

STR (x) converts an object x to a string

REPR (x) converts an object x to an expression string

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

Tuple (s) converts a sequence s to a tuple

List (s) converts the sequence s to a list

Chr (x) converts an integer to one character

UNICHR (x) converts an integer to a Unicode character

Ord (x) converts a character to its integer value

Hex (x) converts an integer to a hexadecimal string

Oct (x) converts an integer to an octal string

Add: There is a difference between STR and repr:

The STR () function gets a good readability of the string, and the repr () function gets a string that can often be used to regain the object, which is usually the case with obj = = eval (repr (obj)). These two functions accept an object as its argument, returning the appropriate string. Although Str (), repr () and ' operations are very similar in terms of features and functions, the fact that repr () and ' ' are doing exactly the same thing, they return an "official" string representation of an object, which means that in most cases it can be evaluated (using the built-in function eval () ) to get the object back, but Str () is different. STR () is committed to generating an object's readable string representation, and its return results are usually not available for eval () evaluation, but are well suited for print statement output. Again, not all strings returned by REPR () are able to use the eval () built-in function to get the original object. [The string returned by Repr can be restored back by using eval]

Conclusion: This means that the repr () output is more friendly to Python, while the output of STR () is more user friendly.



Second, numerical operation

operator meaning

x << y shift left

x >> y Shift Right

X & y Bitwise AND

x | Y bitwise OR

x ^ y bitwise XOR (exclusive OR)

~x rollover by bit

x + y Plus

X-y minus

X * y Multiply

X/y general except

X//Y floor except

X * * y-exponentiation (XY)

X% y modulo (x mod y)

-X Change the symbol bit of the operand

+x do nothing.

~x ~x=-(x+1)

ABS (x) absolute value

Divmod (x, y) return (int (× x/y), x% y)

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

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

x < y less than

x > Y greater than

x = = y equals

X! = y Not equal (same as <>)

x >= y is greater than or equal to

X <= y is less than or equal to

Python Basic Type operations

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.