The Python Number data type is used to store numeric values.
The data type is not allowed to change, which means that if you change the value of the Number data type, the memory space will be redistributed.
The following instance is created when the variable is assigned a value:
var1 = 1
var2 = 10
You can also use the DEL statement to delete some number object references.
The syntax for the DEL statement is:
del var1[,var2[,var3[...,Varn] ]
You can delete single or multiple objects by using the DEL statement, for example:
del var
del var_a, var_b
Python supports four different numeric types:
- Integer (Int) -usually referred to as an integer or integral, is a positive or negative integer, with no decimal points.
- long integers -an integer with an infinite size, and an integer that ends with an uppercase or lowercase L.
- float (floating point real values) -floating-point types consist of integral and fractional parts, and floating-point types can also be represented using scientific notation (2.5e2 = 2.5 x 102 = 250)
- complex numbers ((complex numbers)) -complex numbers are made up of real and imaginary parts, and can be represented by a + BJ, or complex (A, a, b), where both the real and imaginary part of a complex number are floating-point types.
int |
Long |
float |
Complex |
10 |
51924361L |
0.0 |
3.14j |
100 |
-0x19323l |
15.20 |
45.j |
-786 |
0122L |
-21.9 |
9.322e-36j |
080 |
0xDEFABCECBDAECBFBAEl |
32.3+e18 |
.876j |
-0490 |
535633629843L |
-90. |
-.6545+0j |
-0x260 |
-052318172735l |
-32.54e100 |
3e+26j |
0x69 |
-4721885298529l |
70.2-e12 |
4.53e-7j |
- Long integers can also use lowercase "l", but it is recommended that you use uppercase "L" to avoid confusion with the number "1". Python uses "L" to display the long integer type.
- Python also supports complex numbers, which consist of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where the real and imaginary parts of a complex number are floating-point
Python Number Type Conversion
int (x [, base]) converts x to an integer
long (x [, base]) converts x to a long integer
float (x) converts x to a floating point number
complex (real [, imag]) creates a complex number
str (x) converts the object x to a string
repr (x) converts object x to an expression string
eval (str) evaluates a valid Python expression in a string and returns an object
tuple (s) converts the sequence s to a tuple
list (s) converts the sequence s to a list
chr (x) converts an integer to a character
unichr (x) converts an integer to Unicode characters
ord (x) converts a character to its integer value
hex (x) converts an integer to a hexadecimal string
oct (x) converts an integer to an octal string
Python math functions
function |
return value (description) |
ABS (x) |
Returns the absolute value of a number, such as ABS (-10) returns 10 |
Ceil (x) |
Returns the top integer of a number, 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 |
EXP (x) |
Returns the x power of E (ex), such as MATH.EXP (1) returns 2.718281828459045 |
Fabs (x) |
Returns the absolute value of a number, such as Math.fabs (-10) returns 10.0 |
Floor (x) |
Returns the lower integer of a number, such as Math.floor (4.9) returns 4 |
Log (x) |
If Math.log (MATH.E) returns 1.0,math.log (100,10) returns 2.0 |
LOG10 (x) |
Returns the logarithm of x with a base of 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 for a given parameter, which can be a sequence. |
MODF (x) |
Returns the integer portion of x and the fractional part, the two-part numeric symbol is the same as x, and the integer part is represented by a floating-point type. |
Pow (x, y) |
The value after the x**y operation. |
Round (x [, N]) |
Returns the rounded value of the floating-point number x, if given an n value, that represents the 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, such as MATH.SQRT (4) returns 2+0J |
Python random number function
Random numbers can be used in mathematics, games, security and other fields, but also often embedded in the algorithm to improve the efficiency of the algorithm and improve the security of the program.
Python contains the following common random number functions:
function |
Description |
Choice (seq) |
Pick an element randomly from the elements of the sequence, such as Random.choice (range (10)), and randomly pick an integer from 0 to 9. |
Randrange ([Start,] stop [, step]) |
Gets a random number in the collection that increments by the specified cardinality from the specified range, and the base default value is 1 |
Random () |
Randomly generates the next real number, which is within the [0,1] range. |
Seed ([x]) |
Changes the seeding seed of the random number generator. If you don't know how it works, you don't have to specifically set Seed,python to help you choose Seed. |
Shuffle (LST) |
Randomly sort all elements of a sequence |
Uniform (x, Y) |
Randomly generates the next real number, which is within the [x, Y] range. |
Python Trigonometric functions
Python includes the following trigonometric functions:
function |
Description |
ACOs (x) |
Returns the inverse cosine radian value of x. |
ASIN (x) |
Returns the arc value of the arc sine of X. |
|
Atan (x) |
Returns the arc tangent value of x. |
Atan2 (y, x) |
Returns the inverse tangent value of the given X and Y coordinate values. |
COS (x) |
Returns the cosine of the arc of X. |
Hypot (x, y) |
Returns Euclidean norm sqrt (x*x + y*y). |
Sin (x) |
Returns the sine of the X radian. |
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 an angle to radians |
Python Math Constants
Constants |
Description |
Pi |
Mathematical constant Pi (pi, usually denoted by π) |
E |
The mathematical constant e,e is the natural constant (natural constant). |
Python Exercise 29