Python Core Programming number types

Source: Internet
Author: User
Tags bitwise operators ord cmath

1. Introduction to Digital Type
    • The number types in Python include Integer, Long, Boolean, double-precision floating-point, decimal floating-point, and plural. These numeric types are immutable types, meaning that changing the value of a number produces a new object.
    • To delete a numeric object in Python, you can use the statement: Del Aint,along,afloat,acomplex
2. Integral type
    • Boolean type

      The value range is only two values, true and false. They correspond to 1 and 0 in mathematical operations.

      For any number or empty set with a value of 0 (null list, empty tuple, empty dictionary, and so on), the Boolean value in Python is false.

    • Long integer type

      In other programming languages, such as C + +, int occupies 32 bits, long int depends on the machine word length, and the long long int is 64 bits. In Python, the standard integer is generally 32-bit, but if you compile Python with a 64-bit compiler on a 64-bit machine, the integer takes up 64 bits.

      Python's long integer can be supported in a wide range, depending only on the size of the virtual memory supported by the machine. Declaration of long integers: add l:12345l directly after the numbers

      The integer and long integers in Python are gradually unified into one, so it is not necessary to take care of the integral type or the long integer type.

3, double-precision floating-point type

The floating-point type in Python is similar to type double in C, which is a double-precision floating-point type and occupies 64 bits.

4. plural

The plural types in python specify:

    • Complex numbers consist of real and imaginary parts: Real + imagj
    • The imaginary part cannot exist alone and must be combined with the real number of 0.0 to form the plural
    • Real and imaginary part imag are floating-point types.
    • Complex types have three built-in properties: Real, Imag, conjugate (return conjugate plural)
>>> 1.02.0j>>> aComplex.real1.0>>> aComplex.imag2.0>>> aComplex.conjugate()(1-2j)
5. Operator

Automatic type conversion

Python automatically converts a type when it operates on two different types of numbers. The basic principle is that the integral type is converted to floating-point type, and the non-complex is converted to plural.

Real Division vs Floor Removal

Now the Python version calls Division sign "/" and performs the real division:

>>> 1/20.5>>> 1.0/2.00.5

There is also a division called the floor in addition, regardless of the number of operands of the type, always leave the fractional part, can be achieved by conforming to the "//" Implementation:

1.0//2.00.0

emerges operations

Implemented by operator * *, note the precedence of various symbols, or eliminate the ambiguity of precedence directly by parentheses ():

>>> -2**4-16>>> (-2)**416

Bitwise operators

Python integer supports standard bit operations: reverse ~, bitwise AND &, bitwise, OR | , bitwise XOR, Shift left <<, right >>

6. Factory function
    • Standard type functions such as CMP (), str (), type ()
    • Numeric type functions
      • Convert factory functions: Int (), long (), float, Conplex ()
      • function function:
        • ABS (num), return absolute value
        • Corece (num1,num2) converts NUM1, num2 to the same type, and then returns in tuples
        • Divmod (NUM1,NUM2), return (NUM1/NUM2,NUM1%NUM2)
        • Pow (num1,num2) to achieve num1**num2
        • Round (flt,ndig=0), rounding the floating-point number flt, saving the Ndig decimal place, the default is 0.
    • Functions for integral types only
      • Binary conversion function, Oct (), Hex ()
      • ASCII conversion function
        • Chr () accepts a single-byte integer value, returning the corresponding character
        • Ord () takes a character, returns the corresponding integer value
>>> chr(97)‘a‘>>> ord(‘a‘)97
7. Related Modules
    • Decimal decimal floating-point arithmetic class decimal
    • Array of arrays of efficient values
    • Math/cmath standard C library Mathematical operations functions, general mathematical operations in math, complex operations in Cmath
    • operator function implementations of mathematical operators, such as Operator.sub (2,1), are equivalent to 2-1
    • Random multiple pseudo-random number generators
>>> import operator>>> operator.sub(2,1)1>>> import random>>> random.randint(0,10)3>>> random.randint(0,10)7

Reprint Please specify source: http://blog.csdn.net/u012162613/article/details/44286655

Python Core Programming number types

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.