The Python numeric data type is used to store numeric 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 example will create a Number object when the variable is assigned:
var1 = 1
var2 = 10
You can also use the del statement to delete references to some digital objects.
The syntax of the del statement is:
You can delete references to single or multiple objects by using the del statement, for example:
del var_a, var_b
Python supports three different numeric types:
Integer (int) - Usually referred to as an integer, it is a positive or negative integer without a decimal point. Python3 integer types have no size limit and can be used as Long types, so Python3 does not have Python2's Long types. Boolean (bool) is a subtype of integer.
Floating point (float) - Floating point is composed of integer part and decimal part. Floating point type can also be expressed in scientific notation (2.5e2 = 2.5 x 10 2 = 250)
Complex number ((complex)) -A complex number consists of a real part and an imaginary part. It can be represented by a + bj, or complex(a,b). The real part a and imaginary part b of the complex number are both floating-point types.
We can use hexadecimal and octal to represent integers:
>>> number = 0xA0F # hexadecimal
>>> number=0o37 # octal
Python supports complex numbers, which are composed of a real part and an imaginary part, which can be represented by a + bj, or complex(a,b). The real part a and imaginary part b of a complex number are both floating-point types.
Python Number Type Conversion
Sometimes, we need to convert the built-in type of the data. For 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, the real part is x and the imaginary 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.
The following example converts the floating-point variable a to an integer:
>>> a = 1.0
Python Number Operations
The Python interpreter can be used as a simple calculator. You can enter an expression in the interpreter and it will output the value of the expression.
The syntax of expressions is straightforward: + , - , * and / , the same as in other languages (such as Pascal or C). E.g:
>>> 2 + 2
>>> (50-5*6) / 4
>>> 8/5 # Always return a floating point number
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 # Integer division returns floating point
>>> 17 // 3 # Integer division returns the result of rounding down
>>> 17% 3 # The% operator returns the remainder of the division
>>> 5 * 3 + 2
Note: // The number obtained is not necessarily an integer type, it is related to the data type of the denominator numerator.
The equal sign = is used to assign values to variables. After assignment, the interpreter will not display any results except for the next prompt.
>>> width = 20
>>> height = 5*9
>>> width * height
Python can use the ** operation to perform exponentiation:
>>> 5 ** 2 # 5 square
>>> 2 ** 7 # 2 to the 7th power
Variables must be "defined" (assign a value to the variable) before being used, 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
When mixing different types of numbers, integers are converted to floating-point numbers:
>>> 3 * 3.75 / 1.5
>>> 7.0 / 2
In the interactive mode, the last output expression result is assigned to the variable _ . E.g:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
>>> price + _
>>> round(_, 2)
Here, the _ variable should be regarded as a read-only variable by the user.
|abs(x)||Returns the absolute value of the number, such as abs(-10) returns 10|
|ceil(x)||Returns the upward integer of the number, such as math.ceil(4.1) returns 5|
|If x <y, return -1, if x == y, return 0, if x> y, return 1. Note: Python 3 is deprecated, use (x>y)-(x<y) instead .|
|exp(x)||Return e to the power of x (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 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 in base 10, such as math.log10(100) returns 2.0|
|max(x1, x2,...)||Returns the maximum value of a given parameter, which can be a sequence.|
|min(x1, x2,...)||Returns the minimum value of a given parameter, which can be a sequence.|
|modf(x)||Returns the integer part and the decimal part of x. The numerical symbols of the two parts are the same as x, and the integer part is expressed in floating-point type.|
|pow(x, y)||x**y The calculated value.|
Returns the rounded value of the floating-point number x. If a value of n is given, it represents the number of digits rounded to the decimal point.
In fact, it is accurate to say that the retained value will be retained to the end closer to the previous one.
|sqrt(x)||Returns the square root of the number x.|
Random Number Functions
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 includes the following commonly used random number functions:
|choice(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 that increases by the specified base within the specified range, the base default value is 1|
|random()||Randomly generate 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 need to set the seed specifically, Python will help you choose the seed.|
|shuffle(lst)||Randomly sort all elements of the sequence|
|uniform(x, y)||Randomly generate the next real number, which is in the range [x,y].|
Python includes the following trigonometric functions:
|acos(x)||Returns the arc cosine of x in radians.|
|asin(x)||Returns the arc sine of x in radians.|
|atan(x)||Returns the arc tangent of x in radians.|
|atan2(y, x)||Returns the arctangent value of the given X and Y coordinate values.|
|cos(x)||Returns the cosine of x in radians.|
|hypot(x, y)||Returns the Euclidean norm sqrt(x*x + y*y).|
|sin(x)||The returned sine of x radians.|
|tan(x)||Returns the tangent of x radians.|
|degrees(x)||Convert radians to angles, such as degrees(math.pi/2), returns 90.0|
|radians(x)||Convert angle to radians|
|pi||Mathematical constant pi (pi, generally expressed by π)|
|e||The mathematical constant e, e is the natural constant (natural constant).|