Python basic in...login
Python basic introductory tutorial
author:php.cn  update time:2022-04-18 16:14:50

Python Number(Number)


Python Number 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 Number data type is changed, the memory space will be reallocated.

The following example Number object will be created when variable assignment:

var1 = 1
var2 = 10

You can also use del statement deletes some Number object references.

The syntax of the del statement is:

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

You Single or multiple objects can be deleted by using the del statement, for example:

del var
del var_a, var_b

Python supports four different numeric types:

  • Integer type (Int) - Usually called an integer or integer, it is a positive or negative integer without a decimal point.

  • Long integers - An infinite-sized integer with an uppercase or lowercase L at the end.

  • Floating point type (floating point real values) - 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 numbers ((complex numbers)) - Complex numbers consist of real and imaginary parts , 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 types.

##100-0x19323L15.2045.j##-786080-0490-0x2600x69
  • 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 long integers.

  • Python also supports complex numbers. Complex numbers are composed of real parts and imaginary parts, and 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

int(x [, base ]) Convert x to an integer
long(x [,base ]) Convert x to a long integer
float(x) Convert x to a floating point number
complex(real [,imag ]) Create a complex number
str(x) Convert the object x to a string
repr(x) Convert the object x to an expression string
eval(str) Used to calculate the expression in the string Valid Python expression 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 a Unicode character
ord(x) Converts a character to its integer value
hex(x) Converts an integer to a hexadecimal character String
oct(x) Convert an integer to an octal string

Python math function

intlongfloatcomplex
1051924361L0.03.14j
0122L#-21.99.322e -36j
0xDEFABCECBDAECBFBAEl32.3+e18.876j
535633629843L-90.-.6545+0J
-052318172735L-32.54e1003e+26J
-4721885298529L70.2-E12 4.53e-7j
##log10(x) returns 10 as The logarithm of x of the base, such as math.log10(100) returns 2.0max(x1, x2,...)Returns the maximum value of the given parameters Value, parameter 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)Returns the absolute value of the number, such as abs(-10) returns 10
ceil(x)Returns the upper integer of the 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 e raised to the x power (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


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

Python 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

Python mathematical constants

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