Python supports four different numeric types:
- Integer (Int) -usually referred to as an integer or integral, is a positive or negative integer, with no decimal points.
- long integers -an integer with an infinite size, and an integer that ends with an uppercase or lowercase L.
- float (floating point real values) -floating-point types consist of integral and fractional parts, and floating-point types can also be represented using scientific notation (2.5e2 = 2.5 x 102 = 250)
- complex numbers (complex numbers) -complex numbers are made up of real and imaginary parts, and can be represented by a + BJ, or complex (A, a, b), where both the real and imaginary part of a complex number are floating-point types.
- 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 the long integer type.
- Python also supports complex numbers, which consist of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where the real and imaginary parts of a complex number are floating-point
Python Number Type Conversion
int(x [, Base]) #将x转换为一个整数Long(x [, Base]) #将x转换为一个长整数float (x) #将x转换到一个浮点数 complex (real [, Imag]) #创建一个复数 str (x) #将对象 x to string Repr (x) #将对象 x convert to expression string eval (str) #用来计算在字符串中的有效Python表达式 and returns an object tuple (s) #将序列 s into a tuple list ( s) #将序列 S converted to a list chr (x) #将一个整数转换为一个字符 UNICHR (x) #将一个整数转换为Unicode字符 Ord (x) #将一个字符转换为它的整数值 Hex (x) #将一个整数转换为 A hexadecimal string Oct (x) #将一个整数转换为一个八进制字符串
Python Math module, cmath module
The functions commonly used in the mathematical operations of Python are basically in the Math module, the Cmath module .
The Python Math module provides a number of mathematical functions for floating-point numbers.
The Python Cmath module contains a number of functions for complex operations.
The function of the Cmath module is basically the same as the Math module function, except that the Cmath module computes the complex number, and the math module computes the mathematical operation.
To use the math or Cmath function, you must first import:
Import Math
View the contents of the Math View package:
Import Math>>> dir (math)
>>> omitted ....
Python math functions
ABS (x)#返回数字的绝对值, such as ABS (-10) returnceil (x) #返回数字的上入整数, such as Math.ceil ( 4.1) return 5cmp (x, y) #如果 x < y returns-1 if x = = y returns 0 if x > y returns 1exp (x) #返回e的x次幂 (ex), such as Math.exp ( 1) return to 2.718281828459045fabs (x) #返回数字的绝对值, such as Math.fabs ( -10) return to 10.0Floor (x) #返回数字的下舍整数, such as Math.floor ( 4.9) return 4log (x) #如math. log (MATH.E) returns 1. 0,math.log (100,10) return 2.0log10 (x) #返回以10为基数的x的对数, such as MATH.LOG10 ( 100) return to 2.0Max (x1, x2,...) #返回给定参数的最大值, the parameter can be a sequence. min (x1, x2,...) #返回给定参数的最小值, the parameter can be a sequence. modf (x) #返回x的整数部分与小数部分, the two-part numeric symbol is the same as x, and the integer part is represented by a floating-point type. pow (x, y) #x ** The value after the Y operation. round (x [, n]) #返回浮点数x的四舍五入值, if given an n value, represents the number of digits rounded to the decimal point. sqrt (x) #返回数字x的平方根
Python random number function
Random numbers can be used in mathematics, games, security and other fields, but also often embedded in the algorithm to improve the efficiency of the algorithm and improve the security of the program.
Python contains the following common random number functions:
Choice (seq) #从序列的元素中随机挑选一个元素, such as Random.choice (range), randomly selects an integer from 0 to 9. Randrange ([Start,] stop [, step]) #从指定范围内, gets a random number in the collection incremented by the specified cardinality, and the base default value is 1random () #随机生成下一个实数 , which is within the range of [0,1]. seed ([x]) #改变随机数生成器的种子seed. If you don't know how it works, you don't have to specifically set Seed,python to help you choose Seed. the Shuffle (LST) #将序列的所有元素随机排序Uniform (x, y) #随机生成下一个实数, which is within [x/y] range.
Python Trigonometric functions
Python includes the following trigonometric functions:
ACOs (x) #返回x的反余弦弧度值. ASIN (x) #返回x的反正弦弧度值. atan (x) #返回x的反正切弧度值. atan2 (y, x) #返回给定的 The inverse tangent of the x and Y coordinate values. cos (x) #返回x的弧度的余弦值. hypot (x, y) #返回欧几里德范数 sqrt (x *x + y*y). sin (x) #返回的x弧度的正弦值. Tan (x) #返回x弧度的正切值. degrees (x) #将弧度转换为角度, such as degrees (Math.PI/2), return 90.0radians (x) #将角度转换为弧度
Python Math Constants
Pi #数学常量 pi (pi, usually expressed in π)e #数学常量 e,e is the natural constant (natural constant).
Python Basic Tutorial Note 11: Python number (numeric)