Chapter 9 of python3, python3

Source: Internet
Author: User
Tags integer division

Chapter 9 of python3, python3

Python supports three different numeric types:

  • INTEGER (Int)-it is usually called an integer or integer. It is a positive or negative integer without a decimal point. Python3 has no size limit and can be used as the Long type. Therefore, Python3 does not have the Long type of Python2.
  • Float: float is composed of integer and decimal parts. float can also be expressed by scientific notation (2.5e2 = 2.5x102 = 250 ). It is called a floating point number because the decimal point position of a floating point number is variable according to the scientific notation. For example, 1.23x109 and 12.3x108 are completely equal. Floating point numbers can be written in mathematics, such as 1.23, 3.14,-9.01, and so on. However, large or small floating point numbers must be represented by scientific notation, replacing 10 with e. 1.23x109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5. Integers and floating-point numbers are stored in computers in different ways. Integer operations are always accurate (is Division accurate? Yes !), Floating-point operations may have rounding errors.
  • Complex (complex)-a complex number is composed of a real number and a virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual part B of a complex number are float. Click here to view the concept of plural

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

var1 = 100print(id(var1))var1 = 200print(id(var1))

The above code outputs:

20129695362012972736

You will find that orz does not want the same memory address twice. It can be seen that the Python numeric data type is used to store numeric values, and the data type cannot be changed. This means that if the numeric data type value is changed, the memory space will be re-allocated.

 

The Number object will be created when the following instances assign values to variables:

var1 = 1var2 = 10

You can also use the del statement to delete some numeric object references. The syntax of the del statement is:

del var1[,var2[,var3[....,varN]]]]

 

Separate multiple objects with commas (,). For example:

var1 = 1var2 = 10del var1, var2

 

Because the computer uses binary data, it is convenient to use the hexadecimal notation to represent integers. The hexadecimal notation uses the 0x prefix (either lower-case x or upper-case X) and 0 ~ 9, ~ F: for example, 0xf0, 0xabc123, and octal are prefixed with 0 o (lowercase O or uppercase o can be used) and 0 ~ 7. For example, 0o37

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

The above code outputs:

257531

Think about how to use a program to implement a 10-to-2 hexadecimal, 8-to-hexadecimal, or any hexadecimal?

 

Numeric type example

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 built-in data types and convert the data types. You only need to use the data type as the function name:

  • Int (x) converts x to an integer.
  • Float (x) converts x to a floating point number.
  • Complex (x) converts x to a plural number. The real part is x, and the virtual part is 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 is output.

1

 

Note: floating point operations on different machines may have different results.
In integer division, Division (/) always returns a floating point number. If you only want to get the result of an integer and discard the possible score, you can use the operator //:

Print (17/3) # integer division returns floating-point print (17 // 3) # integer division returns the result print (17% 3) After an integer division is rounded down # % operator returns the remainder of Division

The above code outputs:

5.66666666666666752

 

You can use ** operations to perform power operations:

Print ('5 square: ', 5 ** 2) print ('5 to the power of 3:', 5 ** 3)

The above code outputs:

5 square: 255 to the power of 3: 125

 

The integer is converted to a floating point number during mixed operation of different types of numbers:

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

The above code outputs:

3*3.75/1.5 value: 7.5

Related Article

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.