Python Core Programming number types

Source: Internet
Author: User
Tags bitwise bitwise operators ord cmath

1, the number type simple introduction
    • The number types in Python include: Integer, Long, Boolean, double-precision floating-point, decimal floating-point, plural. These numeric types are immutable types. That is, changing the value of a number creates a new object.

    • Delete a number object in Python, can be a phrase: Del Aint,along,afloat,acomplex
2. Integral type
    • Boolean type

      The range of values is only two values. True and False.

      They correspond to 1 and 0 in mathematical operations.

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

    • Long integer type

      In other programming languages, for example in C + +. int takes up 32 bits. A long int is dependent on the machine word length, and a long long int is 64 bits. and in Python. The standard integral type is generally also occupied by 32 bits. But suppose you compile python on a 64-bit machine with a 64-bit compiler. The integer type occupies 64 bits.

      Python's long-range support can be very large, depending 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 the double type in C, which is a dual-precision floating-point type. 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

Self-initiated type conversions

When you operate on two different types of numbers. Python will take the initiative to type-convert itself.

The main principle is that integers are converted to floating-point types, and non-complex numbers are converted to complex numbers.

Real Division vs Floor Removal

Now the Python version number calls division Sign "/", which runs the real division:

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

Another division is called the floor, which, regardless of the type of operand, always removes the fractional part and can be implemented by conforming to "//":

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, saves the Ndig decimal place. The default feels 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 () accepts 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 operation function. General mathematical operations in math, complex operations in Cmath
    • Operator the function of the mathematical operator, for example Operator.sub (2,1) is 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.