Python Standard Data Type - Numbers

Keywords: Python

Python has built-in integer, complex and floating-point numbers.

integer

Integers are numeric values without decimal parts, as in mathematics:

>>> 1
1
>>> -1
-1

Integers have no size limit. As long as your memory is large enough, you can create integers of any size:

>>> 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

In addition to decimal, Python also allows you to create integers using binary, octal and hexadecimal systems:

#Binary:
>>> 0b10
2
>>> 0B10
2
#Octal number system:
>>> 0o10
8
>>> 0O10
8
#Hexadecimal:
>>> 0x10
16
>>> 0X10
16

If you don't know what binary is, you can go to other sources, or just skim through them.

Floating point number

Floating-point numbers are numeric values with decimal parts, similar to mathematical decimal numbers:

>>> 1.1
1.1

If the decimal or integer part of the floating point number you want to create is 0, you can omit it and Python will automatically fill it in for you:

>>> .1
0.1
>>> 1.
1.0

Unlike integers, the effective length of floating-point numbers is only 17 bits, and Python rounds the excess:

>>> 1111.1111111111111111111111111111111111111
1111.111111111111
>>> 1111.8888888888888888888888888888888888888
1111.888888888889

It should be noted that floating-point arithmetic is inaccurate. Please try not to use floating-point arithmetic:

>>> 1.75 * 2.3
4.0249999999999995

Arithmetic operator

Python provides some basic arithmetic operators through which you can perform arithmetic operations:

>>> 5 + 1
6
>>> 5 - 1
4
>>> 5 * 2
10
>>> 5 / 2
2.5
>>> 5 // 2
2
>>> 5 % 2
1
>>> 5 ** 2
25

Note the division operation, / We call it true division. For true division, Python always gets floating-point numbers:

>>> 1 / 2
0.5
>>> 1 / 1
1.0

// We call it truncation. For truncation, if there is a decimal part, Python truncates the decimal part:

>>> 5 // 2
2
>>> 5.0 // 2.0
2.0

Comparison operator

In addition to arithmetic operators, Python also provides comparison operators, which return True or False:

>>> 1 < 5
True
>>> 1 > 5
False
>>> 1 <= 5
True
>>> 1 >= 5
False
>>> 5 == 5
True
>>> 5 != 5
False

True and False understand that if the conditions for comparison are satisfied, they return True or False.

Posted by RDFrame on Mon, 28 Jan 2019 02:24:15 -0800