The road to Python learning-----Number and mathematical functions, random number functions and trigonometric functions

Number

Variables must be assigned a value before they can be used, otherwise an error will occur:

>> > n

Traceback ( most recent call last ) : 
  File "<pyshell#0>" , line 1 ,  in  < module >
    n
NameError : name 'n'  is  not defined
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Numeric type:

int, bool, float, complex

Type conversion:

  • int(x) converts x to an integer.
  • float(x) converts x to a float.
  • complex(x) converts x to a complex number with the real part of x and the imaginary part of 0.
  • complex(x, y) converts x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions.

math function

functionreturn value (description)
abs(x)Returns the absolute value of the number, abs(-10) returns 10
ceil(x)Returns the integer of the number, such as math.ceil(4.1) returns 5, you need to import math
exp(x)Returns e raised to the power of x, math.exp(4) returns 54.598150033144236
fabs(x)Returns the absolute value of the number, math.abs(-10) returns 10
floor(x)Returns the rounded down integer of the number, math.floor(4.9) returns 4
log(x)For example, math.log(math.e) returns 1.0, and math.log(100,10) returns 2.0 (the default base is e)
log10(x)Returns the logarithm of x in base 10, eg math.log10(100) returns 2.0
max(x1,x2...)Returns the maximum value for the given argument, which can be a sequence (list, tuple, set, etc.).
min(x1,x2...)Returns the minimum value for the given argument, which can be a sequence (list, tuple, set, etc.).
modf(x)Returns the integer part and the fractional part of x. The numerical sign of the two parts is the same as that of x, and the integer part is represented by a floating point type. math.modf(3.24) returns (0.2400000000000002, 3.0), which returns a tuple!
pow(x,y)Returns the value after x**y operation
round(x,n)Returns the rounded value of the floating-point number x, given the value of n, representing the number of digits rounded to the decimal point
sqrt(x)Returns the square root of a number x.

random number function

need to import random

functiondescribe
choice(seq)Pick a random element from the elements of the sequence, random.choice(range(10)) , pick a random integer from 0 to 9.
randrange ([start,] stop [,step])Gets a random number from a set within the specified range that is incremented by the specified cardinality. The default value of the cardinality is 1. random.randrange(1, 100, 2))
random()Randomly generate a real number in the range (0,1).
seed(x)Change the seed of the random number generator. If you don't understand how it works, you don't have to set the seed, Python will choose the seed for you.
shuffle(list)Randomly sort all elements of a sequence
uniform(x,y)Randomly generate the next real number in the range [x,y]

Trigonometric functions

Need to import math, ie import math

functiondescribe
math.acos(x)Returns the arc cosine of x in radians.
math.asin(x)Returns the arcsine of x in radians.
math.atan(x)Returns the arctangent of x in radians.
math.atan2(y,x)Returns the arctangent of the given X and Y coordinate values.
math.cos(x)Returns the cosine of x in radians.
math.hypot(x,y)Returns the Euclidean norm sqrt(x x + y y).
math.sin(x)Returns the sine of x radians.
math.tan(x)Returns the tangent of x radians.
math.degrees(x)Convert radians to degrees, such as degrees(math.pi/2), return 90.0
math.radian(x)Convert degrees to radians

Related: The road to Python learning-----Number and mathematical functions, random number functions and trigonometric functions