Python basic tutorial notes eleven: Python Number (number)

Python supports four different numeric types:

  • Integer (Int)  - commonly referred to as Integer or Integer, is a positive or negative integer without a decimal point.
  • long integers  - Integers of infinite size, with an upper or lower case L at the end of the integer.
  • Floating point (floating point real values)  - Floating point consists of integer part and fractional part. Floating point can also be represented in scientific notation (2.5e2 = 2.5 x 10 2  = 250)
  • Complex numbers  - complex numbers consist of a real part and an imaginary part, 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.
    • A lowercase "L" can also be used for long integers, but it is recommended that you use an uppercase "L" to avoid confusion with the number "1". Python uses "L" to display long integers.
    • Python also supports complex numbers. 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 the imaginary part b of the complex number 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 float  
complex(real [,imag ] )   #creates a complex number  
str(x)                  #converts object x to string  
repr(x) #converts                 object x to expression string  
eval(str) #used               to evaluate valid Python expressions in strings, 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 character  
ord(x)                  # Convert a character to its integer value 
hex(x) #Convert                  an integer to a hexadecimal string  
oct(x) #Convert                  an integer to an octal string 

Python math module, cmath module

The functions commonly used in mathematical operations in Python are basically in the math module and cmath module .

The Python math module provides many functions for mathematical operations on floating-point numbers.

The Python cmath module contains some functions for complex arithmetic.

The functions of the cmath module are basically the same as those of the math module. The difference is that the cmath module operates on complex numbers, while the math module operates on mathematical operations.

To use the math or cmath function you must first import:

import math

 

Check out math to see what's in the package:

>>> import math
>>> dir(math)
>>> 省略....

 

Python math functions

abs(x)          #return the absolute value of the number, such as abs(-10) return 10 
ceil(x)         #return the integer of the number, such as math.ceil( 4.1) return 5 
cmp(x, y)       #if x < y returns -1, if x == y returns 0, if x > y returns 1 
exp(x)          # returns e to the power of x (ex), such as math.exp( 1) returns 2.718281828459045 
fabs(x)         # returns a number The absolute value of , 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)          #Such as 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 for the given argument, which can be a sequence. 
min(x1, x2,...) #Return     the minimum value of the given parameter, the parameter can be a sequence.
modf(x) #Return            the integer part and the fractional part of x, the numerical sign of the two parts is the same as x, and the integer part is represented by a floating point type. 
pow(x, y)           #x ** y The value after the operation. 
round(x [,n])       #Returns the rounded value of the floating-point number x, if n is given, it represents the number of digits rounded to the decimal point. 
sqrt(x) #returns            the square root of the number x

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 includes the following commonly used random number functions:

choice(seq)                  #Randomly pick an element from the elements of the sequence, such as random.choice(range(10 )), randomly pick an integer from 0 to 9. 
randrange ([start,] stop [,step]) #Get      a random number from the set that increases by the specified cardinality within the specified range, the default value of the cardinality is 1 
random()                     #Randomly generate the next real number, which is in [ 0 ,1 ) range. 
seed([x])                   #Change the seed 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(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 trigonometry

Python includes the following trigonometric functions:

acos(x)      #Returns the arc cosine of x in radians. 
asin(x) #Returns      the arcsine of x in radians. 
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. 
hypot(x, y) #Return     Euclidean norm sqrt(x *x + y* y). 
sin(x)        # Returns the sine of x in radians. 
tan(x)        #Returns the tangent of x in radians. 
degrees(x)     #Convert radians to degrees, such as degrees(math.pi /2), return 90.0 
radians(x)     #Convert angles to radians

Python math constants

pi #mathematical      constant pi (pi, generally expressed as π) 
e       #mathematical constant e, e is the natural constant (natural constant).

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324668910&siteId=291194637