# Python3 number (Number)

The Python numeric data type is used to store numeric values.

Data types are not allowed to change, which means that if you change the value of a numeric data type, the memory space will be reassigned.

The following instance will be created when the variable is assigned:

```var1 = 1
Var2 = 10```

You can also use the del statement to remove references to some numeric objects.

The syntax of the

del statement is:

`del var1[,< /span>var2[,var3[....,varN]]]]`

You can delete references to single or multiple objects by using the del statement, for example:

```del var
del var_a, var_b```

Python supports three different numeric types:

• Int - Usually referred to as an integer or integer, is a positive or negative integer without a decimal point. The Python3 integer is unrestricted and can be used as a Long type, so Python3 does not have a Python2 Long type.
• Float - The float is composed of an integer part and a fractional part, and the float type can also be represented by scientific notation (2.5e2 = 2.5 x 102< /sup> = 250)
• Complex) - A complex number consists of a real part and an imaginary part, which can be represented by a + bj, or complex(a, b), the real part a and the imaginary part of the complex number b They are all floating point types.

We can use hexadecimal and octal to represent integers:

```>>> number = 0xA0F # Hexadecimal
>>> number
2575

>>> number=0o37 # Octal
>>> number
31```
intfloatcomplex
100.03.14j
10015.2045.j
-786-21.99.322e-36j
08032.3+e18.876j
-0490-90.-.6545+0J
-0x260-32.54e1003e+26J
0x6970.2-E124.53e-7j
• Python supports complex numbers. The complex number consists of a real part and an imaginary part, which can be represented by a + bj, or complex(a,b). The real part a and the imaginary part b of the complex number are both floating point type.

## Python numeric type conversion

Sometimes, we need to convert the type of data built in, the conversion of the data type, 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 complex number with a real part of x and an imaginary part of 0.

• complex(x, y) Converts x and y to a complex number with a real part of x and an imaginary part of y. x and y are numeric expressions.

The following example converts the floating point variable a to an integer:

```>>> a = 1.0
>>> int(a)
1```

## Python Digital Operations

The Python interpreter works as a simple calculator, and you can type an expression into the interpreter that will output the value of the expression.

The syntax of expressions is straightforward: +, -, * and /, as in other languages >>> 8 / 5 # always returns a floating point number 1.6

Note: The results of floating point operations on different machines may vary.

In integer division, the division / always returns a floating point number. If you only want to get the result of an integer, discard the possible fractional part, you can use the operator // :

```>>> 17 / 3 # Integer division returns to float
5.666666666666667
>>>
>>> 17 // 3 # integer division returns the result of rounding down
5
>>> 17 % 3 # % operator returns the remainder of the division
2
>>> 5 * 3 + 2
17```

Note:// The resulting number is not necessarily an integer type, it is related to the data type of the denominator.

```>>> 7//2
3
>>> 7.0//2
3.0
>>> 7//2.0
3.0
>>> ```

The equal sign = is used to assign values to variables. After the assignment, the interpreter will not display any results except the next prompt.

```>>> width = 20
>>> height = 5*9
>>> width * height
900```

Python can use ** Operation to power：

```>>> 5 ** 2 # 5 的平方
25
>>> 2 ** 7 # 2的7次方
128```

The variable must be "defined" before it is used (ie a variable is assigned to it), otherwise an error will occur:

```>>> n # Try to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined```

Converting integers to floating point numbers when mixing different types of numbers：

```>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5```

In interactive mode, the result of the last output expression is assigned to the variable _ . E.g:

```>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06```

Here, the _ variable should be treated as a read-only variable by the user.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.