Python3 Number (Number)


Python numeric data type is used to store numerical values.

The data type is not allowed to be changed, which means that if the value of the numeric data type is changed, the memory space will be reallocated.

The following examples of Number objects will be created when variables are assigned:

var1 = 1
var2 = 10

You can also use the del statement to delete references to some number objects.

The syntax of the del statement is:

del var1[,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 values Type:

  • Integer type (Int) - Usually called an integer or integer, it is a positive or negative integer without a decimal point. Python3 integers have no size limit and can be used as Long types, so Python3 does not have the Long type of Python2.

  • Floating point type (float) - The floating point type consists of an integer part and a decimal part. The floating point type can also be expressed using scientific notation (2.5e2 = 2.5 x 102 = 250)

  • Complex number ((complex)) - A complex number consists of a real part and an imaginary part, you can use a + bj, or complex(a,b) means that the real part a and the imaginary part b of the complex number are both floating point types.

We can use hexadecimal and octal to represent integers:

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

>>> number=0o37 # 八进制
>>> number
31
##-0490-90.-.6545+0J-0x260-32.54e1003e+26J0x6970.2-E124.53e-7j
  • Python supports complex numbers. Complex numbers are composed of real parts and imaginary parts. They can be represented by a + bj, or complex(a,b), The real part a and the imaginary part b of complex numbers are both floating point types.



Python number type conversion

Sometimes, we need to convert the built-in type of data. To convert the data type, you only need to The data type can be used as the function name.

  • int(x) Convert x to an integer.

  • float(x) Convert x to a floating point number.

  • complex(x) Convert x to a complex number, with the real part being x and the imaginary part being 0.

  • complex(x, y) Convert x and y to a complex number, with the real part being x and the imaginary part being y. x and y are numeric expressions.

The following example converts a floating point variable a into an integer:

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

Python number operation

The Python interpreter can be used as a simple Calculator, you can enter an expression into the interpreter and it will output the value of the expression.

The syntax of expressions is straightforward: +, -, * and / just like in other languages ​​(such as Pascal or C). For example:

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

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

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 fractional part, you can use the operator //:

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

The equal sign (=) is used to assign values ​​to variables. After the assignment, the interpreter displays no results except the next prompt.

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

Python can use the ** operation to perform exponentiation operations:

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

Variables must be "defined" (that is, given a value to the variable) before use, otherwise it will An error occurred:

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

When mixed operations with different types of numbers, integers will be converted into floating point numbers:

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

In interactive mode, the last output expression result is assigned to the variable_ . For example:

>>> 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.



Mathematical function

intfloatcomplex
100.03.14j
10015.2045.j
-786-21.99.322e-36j
08032.3+e18.876j
##max(x1, x2,...)Returns the maximum value of the given parameter, which can be a sequence. min(x1, x2,...)Returns the minimum value of the given parameter, which can be a sequence. modf(x)Returns the integer part and decimal part of x. The numerical signs of the two parts are the same as x, and the integer part is expressed in floating point type. pow(x, y)x**y Value after operation. round(x [,n])Returns the rounded value of the floating point number x. If the n value is given, it represents the number of digits rounded to the decimal point. . sqrt(x)Returns the square root of the number x. The number can be negative and the return type is real number. For example, math.sqrt(4) returns 2+0j
FunctionReturn value (description)
abs(x)Return the absolute value of the number, such as abs(-10) returns 10
ceil(x)Return The number is an integer, 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 deprecated. Use Replace with (x>y)-(x<y).
exp(x) Returns the x power of e (ex), such as math.exp(1) returns 2.718281828459045
fabs(x)Returns the absolute value of the number, such as math.fabs(-10) returns 10.0
floor(x) Returns the rounded down integer of the number, such as math.floor(4.9) returns 4
log(x)For example, math.log(math .e) returns 1.0, math.log(100,10) returns 2.0
log10(x)Returns the logarithm of x based on 10, such as math.log10(100) returns 2.0


Random number function

Random numbers can be used in mathematics, games, security and other fields, and are often embedded in algorithms , to improve algorithm efficiency and improve program security.

Python contains the following commonly used random number functions:

FunctionDescriptionchoice (seq)Randomly select an element from the elements of the sequence, such as random.choice(range(10)), randomly select an integer from 0 to 9. randrange ([start,] stop [,step])Get a random number from the set in the specified range, incremented by the specified base, the base is default The value is 1random()Randomly generates the next real number, which is in the range [0,1). seed([x])Change the seed of the random number generator. If you don't understand the principle, you don't have to set the seed specifically, Python will choose the seed for you. shuffle(lst)Randomly sort all elements of the sequenceuniform(x, y) Randomly generate the next real number, which is in the range [x, y].

Trigonometric functions

Python includes the following trigonometric functions:

##hypot(x, y)Returns the Euclidean norm sqrt(x*x + y*y) . sin(x)Returns the sine value of x radians. tan(x)Returns the tangent of x in radians. ##degrees(x)radians(x)
FunctionDescription
acos(x)Returns the arc cosine of x in radians.
asin(x)Returns the arcsine radians value of x.
atan(x)Returns the arc tangent of x in radians.
atan2(y, x)Returns the arctangent of the given X and Y coordinate values.
cos(x)Returns the cosine of x in radians.



Convert radians to angles, such as degrees(math.pi/2), return 90.0
Convert angles to radians

Mathematical constants

Constantpie
Description
Mathematical constant pi (pi, generally expressed as π)
Mathematical constant e, e is the natural constant (natural constant ).