Python 简明教程
Python - Numbers
Python 具有存储和处理数字数据 ( Python Numbers ) 的内置支持。在几乎每个 Python application 中,你都会经常使用数字。显然,任何计算机应用程序都涉及数字。本教程将讨论不同类型的 Python 数字及其属性。
Python has built-in support to store and process numeric data (Python Numbers). Most of the times you work with numbers in almost every Python application. Obviously, any computer application deals with numbers. This tutorial will discuss about different types of Python Numbers and their properties.
Python - Number Types
Python 中有三种内置数字类型:
There are three built-in number types available in Python:
-
integers (int)
-
floating point numbers (float)
-
complex numbers
Python 还具有内置布尔 data type 称为 bool 。它可以被视为 int 类型的子类型,因为它的两个可能值 True 和 False 分别表示整数 1 和 0。
Python also has a bult-in Boolean data type called bool. It can be treated as a sub-type of int type, since it’s two possible values True and False represent the integers 1 and 0 respectively.
Python − Integer Numbers
在 Python 中,任何没有小数部分存储的数字都是整数。(请注意,如果数字的小数部分是 0,这并不意味着它是一个整数。例如数字 10.0 不是整数,它是一个浮点数,小数部分为 0,数字值为 10。)整数可以是零、正整数或负整数。例如,1234、0、-55 在 Python 中都表示整数。
In Python, any number without the provision to store a fractional part is an integer. (Note that if the fractional part in a number is 0, it doesn’t mean that it is an integer. For example a number 10.0 is not an integer, it is a float with 0 fractional part whose numeric value is 10.) An integer can be zero, positive or a negative whole number. For example, 1234, 0, -55 all represent to integers in Python.
有三种方法来形成整数对象。使用 (a) 文字表示,(b) 任何求值为整数的表达式,以及 (c) int() 函数。
There are three ways to form an integer object. With (a) literal representation, (b) any expression evaluating to an integer, and (c) using int() function.
文字是用于直接在源代码中表示常量的符号。例如 −
Literal is a notation used to represent a constant directly in the source code. For example −
>>> a =10
但是,请看以下对整数变量 c 的赋值。
However, look at the following assignment of the integer variable c.
a = 10
b = 20
c = a + b
print ("a:", a, "type:", type(a))
print ("c:", c, "type:", type(c))
它将生成以下 output −
It will produce the following output −
a: 10 type: <class 'int'>
c: 30 type: <class 'int'>
在此,c 确实是一个整数变量,但是首先求值表达式 a + b,然后将它的值间接赋值给 c。
Here, c is indeed an integer variable, but the expression a + b is evaluated first, and its value is indirectly assigned to c.
形成整数对象的第三种方法是 int() 函数的返回值。它将浮点数或 string 转换为整数。
The third method of forming an integer object is with the return value of int() function. It converts a floating point number or a string in an integer.
>>> a=int(10.5)
>>> b=int("100")
您可以用二进制、八进制或十六进制数字表示整数。然而,对象在内部以整数形式存储。
You can represent an integer as a binary, octal or Hexa-decimal number. However, internally the object is stored as an integer.
Binary Numbers in Python
仅由二进制位(1 和 0)组成且前缀为 "0b" 的数字是二进制数。如果将二进制数分配给变量,它仍然是 int 变量。
A number consisting of only the binary digits (1 and 0) and prefixed with "0b" is a binary number. If you assign a binary number to a variable, it still is an int variable.
为以二进制形式表示整数,可直接将其以字面形式存储,或使用 int() 函数,其中基数设置为 2
A represent an integer in binary form, store it directly as a literal, or use int() function, in which the base is set to 2
a=0b101
print ("a:",a, "type:",type(a))
b=int("0b101011", 2)
print ("b:",b, "type:",type(b))
它将生成以下 output −
It will produce the following output −
a: 5 type: <class 'int'>
b: 43 type: <class 'int'>
Python 中还有一个 bin() 函数。它返回整数对应的二进制字符串。
There is also a bin() function in Python. It returns a binary string equivalent of an integer.
a=43
b=bin(a)
print ("Integer:",a, "Binary equivalent:",b)
它将生成以下 output −
It will produce the following output −
Integer: 43 Binary equivalent: 0b101011
Octal Numbers in Python
八进制数仅由数字 0 至 7 构成。为指定整数使用八进制记数法,其前缀必须是 "0o" (小写 O)或 "0O" (大写 O)。八进制数的字面表示如下 −
An octal number is made up of digits 0 to 7 only. In order to specify that the integer uses octal notation, it needs to be prefixed by "0o" (lowercase O) or "0O" (uppercase O). A literal representation of octal number is as follows −
a=0O107
print (a, type(a))
它将生成以下 output −
It will produce the following output −
71 <class 'int'>
请注意对象在内部以整数形式存储。八进制数 107 的十进制等价数为 71。
Note that the object is internally stored as integer. Decimal equivalent of octal number 107 is 71.
由于八进制数系统有 8 个符号(0 至 7),因此其基数为 7。因此,在使用 int() 函数将八进制字符串转换为整数时,需将 base 参数设置为 8。
Since octal number system has 8 symbols (0 to 7), its base is 7. Hence, while using int() function to covert an octal string to integer, you need to set the base argument to 8.
a=int('20',8)
print (a, type(a))
它将生成以下 output −
It will produce the following output −
16 <class 'int'>
八进制 30 的十进制等价数为 16。
Decimal equivalent of octal 30 is 16.
在以下代码中,两个 int 对象从八进制记数法中获取,并对其进行加法。
In the following code, two int objects are obtained from octal notations and their addition is performed.
a=0O56
print ("a:",a, "type:",type(a))
b=int("0O31",8)
print ("b:",b, "type:",type(b))
c=a+b
print ("addition:", c)
它将生成以下 output −
It will produce the following output −
a: 46 type: <class 'int'>
b: 25 type: <class 'int'>
addition: 71
为获取整数的八进制字符串,请使用 oct() 函数。
To obtain the octal string for an integer, use oct() function.
a=oct(71)
print (a, type(a))
Hexa-decimal Numbers in Python
顾名思义,十六进制数系统中有 16 个符号。它们是 0-9 和 A 至 F。前 10 个数字与十进制数字相同。字母 A、B、C、D、E 和 F 分别等于 11、12、13、14、15 和 16。这些字母符号可以使用大写或小写。
As the name suggests, there are 16 symbols in the Hexadecimal number system. They are 0-9 and A to F. The first 10 digits are same as decimal digits. The alphabets A, B, C, D, E and F are equivalents of 11, 12, 13, 14, 15, and 16 respectively. Upper or lower cases may be used for these letter symbols.
要以十六进制记数法表示整数,请在前面加上 "0x" 或 "0X" 。
For the literal representation of an integer in Hexadecimal notation, prefix it by "0x" or "0X".
a=0XA2
print (a, type(a))
它将生成以下 output −
It will produce the following output −
162 <class 'int'>
要将十六进制字符串转换为整数,请在 int() 函数中将基数设置为 16。
To convert a Hexadecimal string to integer, set the base to 16 in the int() function.
a=int('0X1e', 16)
print (a, type(a))
试用以下代码段。它采用十六进制字符串,并返回整数。
Try out the following code snippet. It takes a Hexadecimal string, and returns the integer.
num_string = "A1"
number = int(num_string, 16)
print ("Hexadecimal:", num_string, "Integer:",number)
它将生成以下 output −
It will produce the following output −
Hexadecimal: A1 Integer: 161
然而,如果该字符串包含任何十六进制符号以外的符号,则会生成错误。
However, if the string contains any symbol apart from the Hexadecimal symbol chart an error will be generated.
num_string = "A1X001"
print (int(num_string, 16))
上述程序将生成以下错误 −
The above program generates the following error −
Traceback (most recent call last):
File "/home/main.py", line 2, in
print (int(num_string, 16))
ValueError: invalid literal for int() with base 16: 'A1X001'
Python 的标准库具有 hex() 函数,您可以使用它获取整数的十六进制等价数。
Python’s standard library has hex() function, with which you can obtain a hexadecimal equivalent of an integer.
a=hex(161)
print (a, type(a))
它将生成以下 output −
It will produce the following output −
0xa1 <class 'str'>
整数可表示为二进制、八进制或十六进制,但它在内部仍是整数。因此,在执行算术操作时,表示形式不重要。
Though an integer can be represented as binary or octal or hexadecimal, internally it is still integer. So, when performing arithmetic operation, the representation doesn’t matter.
a=10 #decimal
b=0b10 #binary
c=0O10 #octal
d=0XA #Hexadecimal
e=a+b+c+d
print ("addition:", e)
它将生成以下 output −
It will produce the following output −
addition: 30
Python − Floating Point Numbers
浮点数字具有整数部分和小数部分,由小数点符号(.)分隔。默认情况下,数字为正数,对负数加前缀减号(-)符号。
A floating point number has an integer part and a fractional part, separated by a decimal point symbol (.). By default, the number is positive, prefix a dash (-) symbol for a negative number.
浮点数是 Python 的 float 类的对象。若要存储 float 对象,可以使用文字表示法、使用算术表达式的值或使用 float() 函数的返回值。
A floating point number is an object of Python’s float class. To store a float object, you may use a literal notation, use the value of an arithmetic expression, or use the return value of float() function.
使用文字是最直接的方式。只需将带分数部分的数字分配给变量即可。以下每个语句都声明一个 float 对象。
Using literal is the most direct way. Just assign a number with fractional part to a variable. Each of the following statements declares a float object.
>>> a=9.99
>>> b=0.999
>>> c=-9.99
>>> d=-0.999
在 Python 中,浮点数小数点后可以有多少位数没有限制。但是,为了缩短表示法,使用了 E 或 e 符号。E 表示以 10 为底的指数运算。例如,E4 是以 10 为底的 4 次方(或 10 的 4 次方),e-3 是以 10 为底的 -3 次方。
In Python, there is no restriction on how many digits after the decimal point can a floating point number have. However, to shorten the representation, the E or e symbol is used. E stands for Ten raised to. For example, E4 is 10 raised to 4 (or 4th power of 10), e-3 is 10 raised to -3.
在科学记数法中,数字有一个系数部分和一个指数部分。系数应大于或等于 1 但小于 10 的浮点数。因此,1.23E+3、9.9E-5 和 1E10 是带有科学符号的浮点数的示例。
In scientific notation, number has a coefficient and exponent part. The coefficient should be a float greater than or equal to 1 but less than 10. Hence, 1.23E+3, 9.9E-5, and 1E10 are the examples of floats with scientific notation.
>>> a=1E10
>>> a
10000000000.0
>>> b=9.90E-5
>>> b
9.9e-05
>>> 1.23E3
1230.0
形成 float 对象的第二种方法是间接的,使用表达式的结果。此处,将两个浮点数的商分配给一个变量,该变量引用 float 对象。
The second approach of forming a float object is indirect, using the result of an expression. Here, the quotient of two floats is assigned to a variable, which refers to a float object.
a=10.33
b=2.66
c=a/b
print ("c:", c, "type", type(c))
它将生成以下 output −
It will produce the following output −
c: 3.8834586466165413 type <class 'float'>
Python 的 float() 函数返回一个 float 对象,如果它有适当的内容,则解析一个数字或一个字符串。如果括号中没有给定参数,则返回 0.0,对于一个 int 参数,添加分数部分 0。
Python’s float() function returns a float object, parsing a number or a string if it has the appropriate contents. If no arguments are given in the parenthesis, it returns 0.0, and for an int argument, fractional part with 0 is added.
>>> a=float()
>>> a
0.0
>>> a=float(10)
>>> a
10.0
即使以二进制、八进制或十六进制表示整数,float() 函数也会返回分数部分为 0 的浮点数。
Even if the integer is expressed in binary, octal or hexadecimal, the float() function returns a float with fractional part as 0.
a=float(0b10)
b=float(0O10)
c=float(0xA)
print (a,b,c, sep=",")
它将生成以下 output −
It will produce the following output −
2.0,8.0,10.0
float() 函数从一个字符串中获取浮点数,该字符串包含一个浮点数,无论是标准小数点格式还是科学符号。
The float() function retrieves a floating point number out of a string that encloses a float, either in standard decimal point format, or having scientific notation.
a=float("-123.54")
b=float("1.23E04")
print ("a=",a,"b=",b)
它将生成以下 output −
It will produce the following output −
a= -123.54 b= 12300.0
在数学中,无穷大是一个抽象概念。从物理上讲,无限大的数字永远无法存储在任何数量的内存中。但是,对于大多数计算机硬件配置,一个以 10 为底的 400 次方的非常大的数由 Inf 表示。如果将“无穷大”用作 float() 函数的参数,它将返回 Inf。
In mathematics, infinity is an abstract concept. Physically, infinitely large number can never be stored in any amount of memory. For most of the computer hardware configurations, however, a very large number with 400th power of 10 is represented by Inf. If you use "Infinity" as argument for float() function, it returns Inf.
a=1.00E400
print (a, type(a))
a=float("Infinity")
print (a, type(a))
它将生成以下 output −
It will produce the following output −
inf <class 'float'>
inf <class 'float'>
另一种这样的实体是 Nan(代表非数字)。它表示任何未定义或不可表示的值。
One more such entity is Nan (stands for Not a Number). It represents any value that is undefined or not representable.
>>> a=float('Nan')
>>> a
Nan
Python − Complex Numbers
在本节中,我们将详细了解 Python 中的复杂数据类型。复数在数学方程式中以及电磁学、电子学、光学和量子论中的定律中得到了应用。傅里叶变换使用复数。它们用于波函数计算、滤波器设计、数字电子产品中的信号完整性、射电天文学等。
In this section, we shall know in detail about Complex data type in Python. Complex numbers find their applications in mathematical equations and laws in electromagnetism, electronics, optics, and quantum theory. Fourier transforms use complex numbers. They are Used in calculations with wavefunctions, designing filters, signal integrity in digital electronics, radio astronomy, etc.
复数由实部和虚部组成,用“+”或“-”分隔。实部可以是任何浮点数(或本身是一个复数)。虚部也是一个浮点数/复数,但乘以一个虚数。
A complex number consists of a real part and an imaginary part, separated by either "+" or "−". The real part can be any floating point (or itself a complex number) number. The imaginary part is also a float/complex, but multiplied by an imaginary number.
在数学中,虚数“i”定义为 -1 的平方根 ($\sqrt{−1}$)。因此,复数表示为“x+yi”,其中 x 是实部,“y”是虚部的系数。
In mathematics, an imaginary number "i" is defined as the square root of -1 ($\sqrt{−1}$). Therefore, a complex number is represented as "x+yi", where x is the real part, and "y" is the coefficient of imaginary part.
为了避免与电学理论中电流的使用相混淆,通常使用符号“j”代替“I”表示虚数。Python 还使用“j”作为虚数。因此,“x+yj”是 Python 中复数的表示形式。
Quite often, the symbol "j" is used instead of "I" for the imaginary number, to avoid confusion with its usage as current in theory of electricity. Python also uses "j" as the imaginary number. Hence, "x+yj" is the representation of complex number in Python.
就像 int 或 float 数据类型一样,可以使用文字表示法或使用 complex() 函数来形成一个复杂的对象。以下所有语句都形成一个复杂的对象。
Like int or float data type, a complex object can be formed with literal representation or using complex() function. All the following statements form a complex object.
>>> a=5+6j
>>> a
(5+6j)
>>> type(a)
<class 'complex'>
>>> a=2.25-1.2J
>>> a
(2.25-1.2j)
>>> type(a)
<class 'complex'>
>>> a=1.01E-2+2.2e3j
>>> a
(0.0101+2200j)
>>> type(a)
<class 'complex'>
请注意,实部和虚部的系数必须是浮点数,并且可以用标准小数点符号或科学符号表示。
Note that the real part as well as the coefficient of imaginary part have to be floats, and they may be expressed in standard decimal point notation or scientific notation.
Python 的 complex() 函数有助于形成复杂类型的对象。该函数接收实部和虚部的参数,并返回复数。
Python’s complex() function helps in forming an object of complex type. The function receives arguments for real and imaginary part, and returns the complex number.
complex() 函数有两个版本,一个有两个参数,一个有一个参数。使用带有两个参数的 complex() 很简单。它使用第一个参数作为实部,使用第二个参数作为虚部的系数。
There are two versions of complex() function, with two arguments and with one argument. Use of complex() with two arguments is straightforward. It uses first argument as real part and second as coefficient of imaginary part.
a=complex(5.3,6)
b=complex(1.01E-2, 2.2E3)
print ("a:", a, "type:", type(a))
print ("b:", b, "type:", type(b))
它将生成以下 output −
It will produce the following output −
a: (5.3+6j) type: <class 'complex'>
b: (0.0101+2200j) type: <class 'complex'>
在上面的示例中,我们使用了 x 和 y 作为浮点参数。它们甚至可以是 complex 数据类型。
In the above example, we have used x and y as float parameters. They can even be of complex data type.
a=complex(1+2j, 2-3j)
print (a, type(a))
它将生成以下 output −
It will produce the following output −
(4+4j) <class 'complex'>
对上述示例感到惊讶?将“x”设为 1+2j,“y”设为 2-3j。尝试手动计算“x+yj”,您就会明白了。
Surprised by the above example? Put "x" as 1+2j and "y" as 2-3j. Try to perform manual computation of "x+yj" and you’ll come to know.
complex(1+2j, 2-3j)
=(1+2j)+(2-3j)*j
=1+2j +2j+3
=4+4j
如果您仅对 complex() 函数使用一个数字参数,它会将其视为实部的值;虚部设为 0。
If you use only one numeric argument for complex() function, it treats it as the value of real part; and imaginary part is set to 0.
a=complex(5.3)
print ("a:", a, "type:", type(a))
它将生成以下 output −
It will produce the following output −
a: (5.3+0j) type: <class 'complex'>
如果 complex() 函数的唯一参数是一个具有复数表示的字符串,那么它还可以将字符串分析为复数。
The complex() function can also parse a string into a complex number if its only argument is a string having complex number representation.
在下面的代码段中,系统会要求用户输入一个复数。它用作参数。由于 Python 会将输入读为字符串,因此该函数会从中提取复数对象。
In the following snippet, user is asked to input a complex number. It is used as argument. Since Python reads the input as a string, the function extracts the complex object from it.
a= "5.5+2.3j"
b=complex(a)
print ("Complex number:", b)
它将生成以下 output −
It will produce the following output −
Complex number: (5.5+2.3j)
Python 的内置 complex 类具有两个属性 real 和 imag - 它们返回来自该对象的实部和虚部系数。
Python’s built-in complex class has two attributes real and imag − they return the real and coefficient of imaginary part from the object.
a=5+6j
print ("Real part:", a.real, "Coefficient of Imaginary part:", a.imag)
它将生成以下 output −
It will produce the following output −
Real part: 5.0 Coefficient of Imaginary part: 6.0
complex 类还定义了一个 conjugate() 方法。它返回另一个虚部分量的符号相反的复数。例如,x+yj 的共轭是 x-yj。
The complex class also defines a conjugate() method. It returns another complex number with the sign of imaginary component reversed. For example, conjugate of x+yj is x-yj.
>>> a=5-2.2j
>>> a.conjugate()
(5+2.2j)
Number Type Conversion
在包含混合类型的表达式中,Python 会在内部将数字转换为一个共同类型以进行求值。但是有时,您需要显式地将一个类型中的数字强制转换为另一种类型,以满足运算符或函数参数的要求。
Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But sometimes, you need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.
-
Type int(x) to convert x to a plain integer.
-
Type long(x) to convert x to a long integer.
-
Type float(x) to convert x to a floating-point number.
-
Type complex(x) to convert x to a complex number with real part x and imaginary part zero. In the same way type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions
让我们看看各种数字和与数学相关的函数。
Let us see various numeric and math-related functions.
Theoretic and Representation Functions
Python 在 math 模块中包含以下理论和表示函数 −
Python includes following theoretic and representation Functions in the math module −
Sr.No. |
Function & Description |
1 |
math.ceil(x) The ceiling of x: the smallest integer not less than x |
2 |
math.comb(n,k) This function is used to find the returns the number of ways to choose "x" items from "y" items without repetition and without order. |
3 |
math.copysign(x, y) This function returns a float with the magnitude (absolute value) of x but the sign of y. |
4 |
math.cmp(x, y) This function is used to compare the values of to objects. This function is deprecated in Python3. |
5 |
math.fabs(x) This function is used to calculate the absolute value of a given integer. |
6 |
math.factorial(n) This function is used to find the factorial of a given integer. |
7 |
math.floor(x) This function calculates the floor value of a given integer. |
8 |
math.fmod(x, y) The fmod() function in math module returns same result as the "%" operator. However fmod() gives more accurate result of modulo division than modulo operator. |
9 |
math.frexp(x) This function is used to calculate the mantissa and exponent of a given number. |
10 |
math.fsum(iterable) This function returns the floating point sum of all numeric items in an iterable i.e. list, tuple, array. |
11 |
math.gcd(*integers) This function is used to calculate the greatest common divisor of all the given integers. |
12 |
math.isclose() This function is used to determine whether two given numeric values are close to each other. |
13 |
math.isfinite(x) This function is used to determine whether the given number is a finite number. |
14 |
math.isinf(x) This function is used to determine whether the given value is infinity (+ve or, -ve). |
15 |
math.isnan(x) This function is used to determine whether the given number is "NaN". |
16 |
math.isqrt(n) This function calculates the integer square-root of the given non negative integer. |
17 |
math.lcm(*integers) This function is used to calculate the least common factor of the given integer arguments. |
18 |
math.ldexp(x, i) This function returns product of first number with exponent of second number. So, ldexp(x,y) returns x*2**y. This is inverse of frexp() function. |
19 |
math.modf(x) This returns the fractional and integer parts of x in a two-item tuple. |
20 |
math.nextafter(x, y, steps) This function returns the next floating-point value after x towards y. |
21 |
math.perm(n, k) This function is used to calculate the permutation. It returns the number of ways to choose x items from y items without repetition and with order. |
22 |
math.prod(iterable, *, start) This function is used to calculate the product of all numeric items in the iterable (list, tuple) given as argument. |
23 |
math.remainder(x,y) This function returns the remainder of x with respect to y. This is the difference x − n*y, where n is the integer closest to the quotient x / y. |
24 |
math.trunc(x) This function returns integral part of the number, removing the fractional part. trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x. |
25 |
math.ulp(x) This function returns the value of the least significant bit of the float x. trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x. |
Power and Logarithmic Functions
Sr.No. |
Function & Description |
1 |
math.cbrt(x) This function is used to calculate the cube root of a number. |
2 |
math.exp(x) This function calculate the exponential of x: ex |
3 |
math.exp2(x) This function returns 2 raised to power x. It is equivalent to 2**x. |
4 |
math.expm1(x) This function returns e raised to the power x, minus 1. Here e is the base of natural logarithms. |
5 |
math.log(x) This function calculates the natural logarithm of x, for x> 0. |
6 |
math.log1p(x) This function returns the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero. |
7 |
math.log2(x) This function returns the base-2 logarithm of x. This is usually more accurate than log(x, 2). |
8 |
math.log10(x) The base-10 logarithm of x for x> 0. |
9 |
math.pow(x, y) The value of x**y. |
10 |
math.sqrt(x) The square root of x for x > 0 |
Trigonometric Functions
Python 包含在 math 模块中执行三角计算的以下函数 −
Python includes following functions that perform trigonometric calculations in the math module −
Sr.No. |
Function & Description |
1 |
math.acos(x) This function returns the arc cosine of x, in radians. |
2 |
math.asin(x) This function returns the arc sine of x, in radians. |
3 |
math.atan(x) This function returns the arc tangent of x, in radians. |
4 |
math.atan2(y, x) This function returns atan(y / x), in radians. |
5 |
math.cos(x) This function returns the cosine of x radians. |
6 |
math.sin(x) This function returns the sine of x radians. |
7 |
math.tan(x) This function returns the tangent of x radians. |
8 |
math.hypot(x, y) This function returns the Euclidean norm, sqrt(x*x + y*y). |
Angular conversion Functions
以下是 Python math 模块提供的角度转换函数 −
Following are the angular conversion function provided by Python math module −
Sr.No. |
Function & Description |
1 |
math.degrees(x) This function converts the given angle from radians to degrees. |
2 |
math.radians(x) This function converts the given angle from degrees to radians. |
Mathematical Constants
Python math 模块定义了以下数学常量 −
The Python math module defines the following mathematical constants −
Sr.No. |
Constants & Description |
1 |
math.pi This represents the mathematical constant pi, which equals to "3.141592…" to available precision. |
2 |
math.e This represents the mathematical constant e, which is equal to "2.718281…" to available precision. |
3 |
math.tau This represents the mathematical constant Tau (denoted by τ ). It is equivalent to the ratio of circumference to radius, and is equal to 2Π. |
4 |
math.inf This represents positive infinity. For negative infinity use "−math.inf". |
5 |
math.nan This constant is a floating-point "not a number" (NaN) value. Its value is equivalent to the output of float('nan'). |
Hyperbolic Functions
双曲函数是基于双曲线而不是圆的三角函数的类似物。以下是 Python math 模块的双曲函数 −
Hyperbolic functions are analogs of trigonometric functions that are based on hyperbolas instead of circles. Following are the hyperbolic functions of the Python math module −
Sr.No. |
Function & Description |
1 |
math.acosh(x) This function is used to calculate the inverse hyperbolic cosine of the given value. |
2 |
math.asinh(x) This function is used to calculate the inverse hyperbolic sine of a given number. |
3 |
math.atanh(x) This function is used to calculate the inverse hyperbolic tangent of a number. |
4 |
math.cosh(x) This function is used to calculate the hyperbolic cosine of the given value. |
5 |
math.sinh(x) This function is used to calculate the hyperbolic sine of a given number. |
6 |
math.tanh(x) This function is used to calculate the hyperbolic tangent of a number. |
Special Functions
以下是 Python math 模块提供的特殊函数 −
Following are the special functions provided by the Python math module −
Sr.No. |
Function & Description |
1 |
math.erf(x) This function returns the value of the Gauss error function for the given parameter. |
2 |
math.erfc(x) This function is the complementary for the error function. Value of erf(x) is equivalent to 1-erf(x). |
3 |
math.gamma(x) This is used to calculate the factorial of the complex numbers. It is defined for all the complex numbers except the non-positive integers. |
4 |
math.lgamma(x) This function is used to calculate the natural logarithm of the absolute value of the Gamma function at x. |
Random Number Functions
随机数用于游戏、模拟、测试、安全和隐私应用程序。Python 在 random 模块中包含以下函数。
Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes following functions in the random module.
Sr.No. |
Function & Description |
1 |
random.choice(seq) A random item from a list, tuple, or string. |
2 |
random.randrange([start,] stop [,step]) A randomly selected element from range(start, stop, step) |
3 |
random.random() A random float r, such that 0 is less than or equal to r and r is less than 1 |
4 |
random.seed([x]) This function sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None. |
5 |
random.shuffle(seq) This function is used to randomize the items of the given sequence. |
6 |
random.uniform(a, b) This function returns a random floating point value r, such that a is less than or equal to r and r is less than b. |
Built-in Mathematical Functions
以下数学函数内置在 Python interpreter 中,因此您不必从任何模块导入它们。
Following mathematical functions are built into the Python interpreter, hence you don’t need to import them from any module.