Number types of Python data structures

Source: Internet
Author: User
Tags integer definition mathematical constants mathematical functions

Number Type
    • Kinds
    • Relationship of numeric types
    • Numeric type conversions
    • Numeric operations
    • Mathematical functions
    • Random number function
    • Trigonometric function
    • Mathematical constants
I. type integer (int)

Integer definition can also be a short integer, theoretically has a range of limitations, the value range is: On the 32-bit machine, the range of int is: -2**31~2**31-1, that is, -2147483648~2147483647

The range of int on a 64-bit machine is: -2**63~2**63-1, which is -9223372036854775808~9223372036854775807, if you can use a long integer over this range, but you must write the uppercase "L" at the end, lowercase , in order to avoid confusion with 1, it is recommended to use L

It is important to note that:

The value of the int theory range above is the reference C language integer range. In the actual environment, the int range is not fixed, we use the above said 64-bit maximum value by 1000 test;

Examples are as follows:

>>> print (9223372036854775807)9223372036854775807000

  We find that there is no error, in fact, because of overflow, Python3 will automatically convert the integer data to a long integer ! This still needs to be noted.

Integers in Python can be represented not only in decimal, but also in octal and hexadecimal notation.

When a binary represents an integer, the value preceded by a prefix "0b or 0 B" is used to indicate binary data

When an integer is represented in octal, the value is preceded by a prefix "0o or 0O" to represent the octal data

When integers are represented in hexadecimal, a prefix "0x or 0X" is preceded by a number to denote hexadecimal data

For example: Here we assign integer 15 to integer variables A, B, C in the form of eight binary and 16 binary and binary, and then export them in decimal form

>>> a = 0o17>>> b = 0xf
Print (a,b,c)15 15 15
Long integer type

Unlike the C language, Python's long integers do not refer to the positioning width, which means that Python does not limit the size of long integer values, but in fact, because of limited machine memory, we cannot use long integer values indefinitely.

How do we differentiate between long integers and integer values in the process of use? It is common practice to add a capital letter L or a lowercase l to the number trailing to indicate that the integer is a long integer, such as:

A = 6812347598L
b = 6812347598l

It may have been found that the lowercase l and the number 1 look difficult to distinguish, so it is generally recommended to use uppercase letters L.

Attention:

 Since Python2.2, Python automatically converts integer data to long integers if an overflow occurs, so there is no serious consequence of not adding the letter L after long integer data. Therefore, it is not possible to distinguish between integral type and long integer, this knowledge point can be understood.

Float type (float)

A floating-point number is used to process real numbers, which are numbers with decimals. There are two representations of real numbers, one in the form of decimal numbers, which consist of numbers and decimal points, and where the decimal point is indispensable, such as 1.23,123.0,0.0, and the other is exponential, such as 789e3 or 789E3, which means 789x. 103, the letter E (or E) must be preceded by a number, the letter E (or e) may be followed by a sign, indicating the exponent of the symbol, if not, a plus, and the exponent must be an integer.

Floating-point notation using scientific notation (2.5e2 = 2.5 x 102 = 250)

Plural (complex)

The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers. Note that the letter J of the imaginary part can be case-sensitive, as 5.6+3.1j,5.6+3.1j is equivalent.

2. Relationship of number types

There is a gradual "extended" relationship for three numeric types: integer –> floating point number –> plural

Mixed operations can be performed between different numeric types, resulting in the widest type after the operation, such as: 23+4.0=127.0 (integer + float = floating-point number)

3. Conversion of data types

Sometimes we need to transform the type of data built-in, the conversion of data types, just the data type as a function name.

Function:

int (x) converts the x in parentheses into an integer type;

Float (x) converts the x in parentheses to a floating-point type;

Complex (x) converts the x in parentheses into a complex number;

For example:

---------------Convert 666 of string types to integer----------------------->>>Print(Type (int ("666")))<class 'int'>---------------Convert 666 of string types to floating-point--------------------->>>Print(Type (float ("666")))<class 'float'>>>>Print(Float ("666"))666.0--------------Convert 666 of string types to plural------------------------>>>Print(Complex ("666"))(666+0J)
4. Digital operations

Please refer to the Operator Chapter system

5. Mathematical Functions

6. Random functions

Random numbers can be used in mathematics, games, security and other fields, but also often embedded in the algorithm to improve the efficiency of the algorithm and improve the security of the program.

Python contains the following common random number functions:

7. Trigonometric Functions

8. Mathematical Constants

  

Number types of Python data structures

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.