Latest web development tutorials

# Python3 numbers (Number)

Python type for storing digital data values.

It is not allowed to change the data type, which means that if you change the type of digital data have value, reallocate memory space.

The following examples are Number objects will be created when the variable assignment:

```var1 = 1
var2 = 10
```

You can also use the del statement to remove some of the digital object.

del statement syntax is:

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

You can use the del statement to delete individual or multiple objects of reference, for example:

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

Python supports three different types of values:

• Integer (Int) - often referred to is an integer or an integer is a positive or negative integer, with no decimal point. Python3 is no limit to the size of the integer, it can be used as Long type used, so there is no Python2 Python3 the Long type.
• Float (float) - floating-point and integer part from the fractional part, floats can also be expressed (2.5e2 = 2.5 x 10 2 = 250) using scientific notation
• Complex ((complex)) - complex by the real and imaginary parts, you can use a + bj, or complex (a, b) that the real part and an imaginary part b is a floating-point type.

We can use hexadecimal and octal integers to represent:

```>>> number = 0xA0F # 十六进制
>>> number
2575

>>> number=0o37 # 八进制
>>> number
31
```
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
• Python supports plural number by the real and imaginary parts, you can use a + bj, or complex (a, b) that the real part and an imaginary part b is a floating-point type.

## Python numeric type conversion

Sometimes, we need to built-in data type conversion, data type conversion, you only need to type the data as a function name.

• int (x) Converts x to an integer.

• float (x) Converts x to a float.

• complex (x) Converts x to a complex, real part of x, the imaginary part is zero.

• complex (x, y) x and y converted to a complex number, the real part of x, the imaginary part of y.x and y are numeric expressions.

The following example will convert a floating-point variable is an integer:

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

## Python digital operation

Python interpreter can be used as a simple calculator, you can enter an expression in the interpreter, it will output the value of the expression.

Expression syntax is straightforward: +, -, *, and /, and other languages ​​(such as Pascal or C) the same. E.g:

```>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # 总是返回一个浮点数
1.6
```

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

In integer division, division (/) always returns a floating-point number, if you just want to get the results of an integer, the fractional part may be dropped, you can use the@ operator:

```>>> 17 / 3  # 整数除法返回浮点型
5.666666666666667
>>>
>>> 17 // 3  # 整数除法返回向下取整后的结果
5
>>> 17 % 3  # ％操作符返回除法的余数
2
>>> 5 * 3 + 2
17
```

Equal sign (=) is used to assign values ​​to variables. After this assignment, in addition to the next prompt, the interpreter will not show any results.

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

Python can usethe ** operator for exponentiation:

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

Variable before use must be "defined" (that is, a value assigned to the variable), otherwise an error:

```>>> n   # 尝试访问一个未定义的变量
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
```

Different types of mixed integer operands will be converted to floating point:

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

In interactive mode, the last expression result is output 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 regarded as a read-only user variables.

## Mathematical Functions

function Return Value (description)
abs (x) Returns the absolute value, such as abs (-10) returns 10
ceil (x) Returns on the integers, such as math.ceil (4.1) returns 5

cmp (x, y)

If x <y returns -1 if x == y Returns 0 if x> y returns 1. Python 3 is obsolete. That use (x> y) - (x <y) replaced.
exp (x) Returns e raised to the power of x (e x), as math.exp (1) returns 2.718281828459045
fabs (x) Returns the absolute value, such as math.fabs (-10) returns 10.0
floor (x) Returns the rounded integer, such as math.floor (4.9) returns 4
log (x) As math.log (math.e) returns 1.0, math.log (100,10) returns 2.0
log10 (x) Returns the base 10 logarithm of x, such math.log10 (100) returns 2.0
max (x1, x2, ...) The maximum return for a given parameter, parameter sequence.
min (x1, x2, ...) Returns the minimum value for a given parameter, parameter sequence.
modf (x) Returns the integer part of x and the fractional part, numerical symbols and the two parts of the same x, the integer part in floating-point representation.
pow (x, y) Value after the operation x ** y.
round (x [, n]) Returns the rounded value of float x, as given n value represents the rounding to the number of decimal places.
sqrt (x) Returns the square root of x, numbers can be negative, the return type is a real number, such as math.sqrt (4) returns 2 + 0j

## Random number function

Random number can be used in mathematics, gaming, security and other fields, it is also frequently embedded into the algorithm to improve the efficiency and increase the security of the program.

Python random number functions include the following common:

function description
choice (seq) From the sequence of elements in a randomly selected element, such as random.choice (range (10)), from 0-9 in a randomly selected integer.
randrange ([start,] stop [ , step]) To get a random number from within the specified range specified increments base collection, base defaults to 1
random () Randomly generated under a real number in [0,1) range.
seed ([x]) Changing the random number generator seed seed. If you do not understand the principle, you do not have to set special seed, Python will help you choose the seed.
shuffle (lst) All the elements of a sequence in random order
uniform (x, y) Randomly generated under a real number in [x, y] range.

## Trigonometric functions

Python includes the following trigonometric functions:

function description
acos (x) Returns the arc cosine of x in radians.
asin (x) Returns the arc sine of x radians.
atan (x) Returns the arctangent of x in radians.
atan2 (y, x) Returns the arctangent of X and Y coordinate values.
cos (x) Returns the cosine of x radians.
hypot (x, y) Back Euclidean norm sqrt (x * x + y * y).
sin (x) Returns the sine of x radians.
tan (x) Returns the tangent of x radians.
degrees (x) Converts radians to degrees, such as degrees (math.pi / 2), returned 90.0