Python3 eighth-Number of data types (numbers)

Source: Internet
Author: User
Tags integer division

Python supports three different types of numbers:

    • Integer (INT)-usually referred to as an integer or integral, is a positive or negative integer, with no decimal points. The Python3 integral type is not limited in size and can be used as a long type, so Python3 does not have a long type of Python2.
    • Float (float)-floating-point type consists of integral and fractional parts, and floating-point types can also be represented by scientific notation (2.5e2 = 2.5 x 102 = 250). This is called a floating-point number because, when represented by scientific notation, the decimal position of a floating point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9, or 12.3e8,0.000012 can be written 1.2e-5, and so on. Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.
    • Complex numbers ((complex))-complex numbers are composed of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where both the real and imaginary part of a complex number are floating-point types. Click here for the plural concept

We use the ID () function to output the address referenced by the variable:

var1 = Print=print(ID (var1))

The above code, output:

20129695362012972736

You will find that Orz, two times the memory address does not want the same. As you can see, the Python numeric data type is used to store numeric values, and the data type is not allowed to change, which means that if you change the value of the numeric data type, the memory space will be redistributed.

The following instance is created when the variable is assigned a value:

var1 = 1= 10

You can also use the DEL statement to delete references to some numeric objects. The syntax for the DEL statement is:

del Var1[,var2[,var3[....,varn] []]

Multiple objects to be deleted are separated by commas ",", for example:

var1 = 1= tendel var1, VAR2

Computer because of the use of binary, so, sometimes hexadecimal notation is convenient, hexadecimal with 0x prefix (lowercase x or uppercase x can) and 0~9,a~f, for example:0xf0,0xabc123 , octal is prefixed with 0o (lowercase o or uppercase o are available) and 0~7, for example:0o37

Number = 0xa0f  #  hexadecimal number2 = 0o37  #  octal print(number)  Print(number2)

The above code, output:

257531

Thinking, how to use the program to achieve 10 binary to 2, 8, 16, any of the binary?

Examples of numeric types

int float Complex
10 0.0 3.14j
100 15.20 45.j
-786 -21.9 9.322e-36j
080 32.3+e18 .876j
-0490 -90. -.6545+0j
-0x260 -32.54e100 3e+26j
0x69 70.2-e12 4.53e-7j

Sometimes, we need to convert the data-built type into the data type, you just need to use the data type as the function name:

    • int (x) converts x to an integer.
    • Float (x) converts the x to a floating-point number.
    • Complex (x) converts x to a complex number, the real part is x, and the imaginary part is divided into 0.
    • Complex (x, Y) converts X and Y to a complex number, the real part is x, and the imaginary part is Y. X and y are numeric expressions.
A = 1.0print(int (a))

The above code, output

1

Note: The results of the floating point operations on different machines may vary.
In integer division, Division (/) always returns a floating-point number, and if you want only the result of an integer, discard the possible fractional portion, you can use the operator//:

Print (17/3)  # integer division returns floating-point type Print (+//3)  # integer division Returns the result of rounding down Print (3%)  # The% operator returns the remainder of the Division

The above code, output:

5.66666666666666752

You can use the * * operation to perform a power operation:

Print ('5 squared:', 5 * * 2) Print ('5 of 3 times:', 5 * * 3)

The above code, output:

5 squared:125

Different types of number blending operations convert integers to floating-point numbers:

Print ('3 * value of 3.75/1.5:', 3 * 3.75/1.5)

The above code, output:

3 * Value of 3.75/1.5:7.5

Python3 eighth-Number of data types (numbers)

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.