Python's number type

Source: Internet
Author: User
Tags bitwise

1. Type of support

Python supports multiple numeric types: Shaping, Long Integer, Boolean, double-precision floating-point, decimal floating-point, plural
Note: When you reassign a numeric object, you do not actually update the original number of the object ( number object immutable), just regenerate a new numeric object and point to it.

Plural

>>> Acomplex = -3.1-4.2j>>> acomplex (-3.1-4.2j)>>> acomplex.real -3.1>>> acomplex.imag-4.2>>> acomplex.conjugate (-3.1+4.2j)

Boolean type
For any number that has a value of zero or an empty set (empty list, empty tuple, empty dictionary, and so on), the Boolean value in Python is False.

2. Operators

Addition
Python supports the addition of different numeric types, followed by conversion rules: integers to floating-point numbers, non-complex to complex numbers.
Conversions between numeric types are automatic, but you can explicitly specify that a particular type of data is converted by using the coerce () built-in function

Division

>>>0#  Traditional Division #  floor except from __future__  Import#  Importing Real Division >>> 1/20.5

Bitwise operations: Negation (~), Bitwise AND (&), or (|) and XOR (^) and left (<<) and right Shift (>>)

3. Built-in functions and factory functions

Supports standard type built-in function cmp (), str (), and type ()

Numeric type functions

Convert Factory functions

bool (obj)
int (obj, base=10)
long (obj , base=10)
float (obj)
complex (str) or complex ( Real, imag=0.0)

>>> bool ('hello') True>>> int ('}'  )2015>>> long 2015l>>> float2015.0>>> Complex (2015+0j)>>> Complex (5) (2015+5j)

function function

ABS (NUM)
Coerce (NUM1, num2) converts NUM1 and num2 to the same type and then returns a tuple
Divmod (NUM1, num2) returns a tuple (num1/num2, num1%num2)
The POW (NUM1, num2, mod=1) takes the num2 of NUM1, and if the MoD parameter is supplied, then the MoD is taken out.
Round (Fit, ndig=0) accepts floating-point numbers and rounds them, preserving ndig decimal places

>>> ABS (-1)1>>> ABS (3+4j)5.0>>> Coerce (1.5, 3)(1.5, 3.0)>>> Coerce (3+4j, 5)((3+4J), (5+0j))>>> Divmod (10, 3)(3, 1)>>> Divmod (10, 2.5)(4.0, 0.0)>>> Divmod (3+4j, 1+2j)((2+0J), (1 +0j))>>> Pow (2, 3)8>>> Pow (3+4j, 2)(-7+24j)>>> Round (2.4)2.0>>> Round (-2.4)-2.0>>> Round (-2.17, 1)

Functions that are used only for integers (standard integers, long integers)

Binary conversion

Oct (): Convert to octal, return string hex (): Convert to 16 binary, return string >>> Oct (8)'010'> >> Hex (+)'0x10'

ASCII conversion

>>> Ord ('a')97>>> chr'a  '

2015-05-24

Python's number type

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.