Python Number

Python Number

The Python Number data type is used to store numeric values.

The data type is not allowed to change, which means that if the value of the Number data type is changed, the memory space will be reallocated.

The following instances of Number objects will be created when the variable is assigned:

var1 = 1
var2 = 10

You can also delete some Number object references using the del statement.

The syntax of the del statement is:

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

You can delete single or multiple objects by using the del statement, for example:

part was
del var_a, var_b

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 an integer part and a fractional part. Floating point can also be represented using scientific notation (2.5e2 = 2.5 x 102 = 250)
  • Complex numbers (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.
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
  • 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 ]) converts x to an integer  
long(x [,base ]) converts x to a long integer  
float(x) converts x to a float  
complex(real [,imag ]) creates a complex number  
str(x) converts 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 a Unicode character  
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 integer of the number, such as math.ceil(4.1) returns 5
cmp(x, y) Returns -1 if x < y, 0 if x == y, 1 if x > y
exp(x) Returns e to the power of x (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 rounded down integer of the number, such as 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
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.
min(x1, x2,...) Returns the minimum value of the given argument, which can be a sequence.
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.
pow(x, y) The value after the x**y 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 a 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:

function describe
choice(seq) Pick a random element from the elements of the sequence, e.g. random.choice(range(10)) , pick a random integer from 0 to 9.
randrange ([start,] stop [, step]) Get 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() Randomly generate the next 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(lst) Randomly sort all elements of a sequence
uniform(x, y) Randomly generate the next real number in the range [x,y].

 

Python trigonometry

Python包括以下三角函数:

函数 描述
acos(x) 返回x的反余弦弧度值。
asin(x) 返回x的反正弦弧度值。
atan(x) 返回x的反正切弧度值。
atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。
cos(x) 返回x的弧度的余弦值。
hypot(x, y) 返回欧几里德范数 sqrt(x*x + y*y)。
sin(x) 返回的x弧度的正弦值。
tan(x) 返回x弧度的正切值。
degrees(x) 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
radians(x) 将角度转换为弧度

 

Python数学常量

常量 描述
pi 数学常量 pi(圆周率,一般以π来表示)
e 数学常量 e,e即自然常数(自然常数)。

Guess you like

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