Mathematical functions
function | Return value ( describe ) | abs(x) Returns the absolute value of the number , Such as abs(-10) return 10ceil(x) Returns an upinteger of a number , Such as math.ceil(4.1) return 5cmp(x, y) If x < y return -1, If x == y return 0, If x > y return 1.
Python 3 obsolete , Use (x>y)-(x<y) Replace .exp(x) return e Of x The next power (ex), Such as math.exp(1) return 2.718281828459045fabs(x) Returns the absolute value of the number , Such as math.fabs(-10) return 10.0floor(x) Returns the rounded value of a number , Such as math.floor(4.9) return 4log(x) Such as math.log(math.e) return 1.0,math.log(100,10) return 2.0log10(x) Return to 10 For the base x The logarithmic , Such as math.log10(100) return 2.0max(x1, x2,...) Returns the maximum value of the given parameter , Parameters can be sequences .min(x1, x2,...) Returns the minimum value for a given parameter , Parameters can be sequences .modf(x) return x The integral and fractional parts of , Two parts of the numerical symbols and x identical , The integer part is represented as a floating point .pow(x, y)x**y The calculated value .round(x [,n]) Return floating point number x Round the value of , Such as given n value , Represents the number rounded to the decimal point .sqrt(x) Return to digital x The square root of .
fabs() The function is similar to abs() function , But he has two differences :
- abs() It's a built-in function . fabs() Function in math Defined in module .
- fabs() Function is only valid for floating-point and integer values . abs() It can also be used in the plural .
Random function
function | describe | choice(seq) Pick one element at random from the elements of the sequence , such as random.choice(range(10)), from 0 To 9 Pick an integer at random .randrange ([start,] stop [,step]) From the specified range , Gets a random number from a set incremented by the specified cardinality , The default value of cardinality is 1random() I'm going to randomly generate the next real number , It's in [0,1) Within the scope of .seed([x]) Change the seed of the random number generator seed. If you don't know how it works , You don't have to specify seed,Python Will help you choose seed.shuffle(lst) Sort all the elements of the sequence at random uniform(x, y) I'm going to randomly generate the next real number , It's in [x,y] Within the scope of .
seed([x]) Reading :
When seed() Without parameters , Every time I generate random number It's different , And when seed() When there are parameters , The random number generated each time is the same , At the same time, the random number generated by selecting different parameters is also different .
Trigonometric functions
function | describe | acos(x) return x Of arc cosine radians .asin(x) return x Of arc sine radians .atan(x) return x The arctangent of PI radians .atan2(y, x) Returns the given X And Y The inverse tangent of the coordinate value .cos(x) return x Cosine of PI radians .hypot(x, y) Returns the Euclidean norm sqrt(x
x + yy).sin(x) Back to x Sine of radians .tan(x) return x Tangent of radians .degrees(x) Convert radians to degrees , Such as degrees(math.pi/2) , return 90.0radians(x) Convert the Angle to radians