method | description |
---|---|
ceil() | 将数字向上舍入到最接近的整数值 |
floor() | 将数字向下舍入到最接近的整数值 |
degrees() | 以弧度为单位的角度转换为度数 |
radians | 以度为单位的角度转换为弧度 |
factorial() | 求任何正整数的阶乘 |
fabs | 以浮点数形式查找数字的绝对值 |
trunc() | 将数字截断为最接近的整数 |
pow() | 接受两个参数,x 和 y,并返回 x 的 y 次幂 |
isfinite() and isinf() | 判断一个数是否有限 |
isclose() | 查找两个数字是否接近或不使用 |
getcontext() | 设置小数的精度并设置要提及的标志以向上或向下舍入数字 |
limit_denominator() | 限制分母中的位数 |
from_float() | 从浮点数创建分数 |
Complex Numbers
Python复数是具有实部和虚部的复数,一般表示为:
c==c.real+c.imaginary*1j
#z.real和z.imaginary是笛卡尔坐标
Python 复数可以用直角坐标和极坐标表示。极坐标是表示复数的另一种方法,复数将具有r,phi其中 r是模数,phi是相位角。
Python 提供了一个用函数调用的单独模块cmath,特别是对于这些复数。
import cmath
#to find the phase angle
phi = cmath.phase(complex(-1.0, 1.0))
print(phi)
#to find the rectangular coordinates
c=cmath.rect(1,2)
print("real::",c.real)
print("imaginary::",c.imag)
print("complex number::",c)
#print the polar coordinates
p=cmath.polar(c)
print("polar::",p)
#print
2.356194490192345
real:: -0.4161468365471424
imaginary:: 0.9092974268256817
complex number:: (-0.4161468365471424+0.9092974268256817j)
polar:: (1.0, 2.0)
Hexadecimal and Octal Numbers
我们可以使用该方法将数字从整数转换为八进制,并使用该oct()
方法将数字转换为十六进制hex()
。
i = 10
print("in octal form::", oct(i))
print("in hexa decimal form ::", hex(i))
#print
in octal form:: 0o12
in hexa decimal form :: 0xa
Math Module Methods to work with Numerical Data
为了使用它们,我们需要使用语句导入math模块。import math我们将通过示例看到一些最广泛使用的数学方法。
ceil() 和 floor()
ceil()
method 用于将数字向上舍入到最接近的整数值,而floor()
用于将数字向下舍入到最接近的整数值。
import math as m
a = 1.4
print(m.ceil(a))
print(m.floor(a))
#print
2
1
degrees() and radians()
使用该degrees()
方法将弧度中的角度转换为度数。同样,可以使用radians()
方法将角度以度为单位转换为弧度。
import math
pi = math.pi
print(pi)
#To find the degree equivalent of the radians
print("Degree value of pi is ::",math.degrees(pi))
#Radians for the degrees
print("Radian value of 90 degree is ::",math.radians(90))
#print
3.141592653589793
Degree value of pi is :: 180.0
Radian value of 90 degree is :: 1.5707963267948966
sin() and cos()
#sin value of 90
radians_90=math.radians(90)
print("sin value of pi/2 is ::", math.sin(radians_90))
#cos value of 60
radians_60 = math.radians(60)
print("cos value of pi/2 is ::", math.cos(radians_60))
#print
sin value of pi/2 is :: 1.0
cos value of pi/2 is :: 0.5000000000000001
factorial()
我们可以使用数学模块中的 factorial()
方法找到任何正整数的阶乘。
import math
#factorial of 6 : 6*5*4*3*2*1
print("factorial of 6 is ::", math.factorial(6))
#print
factorial of 6 is :: 720
fabs() and trunc()
可以使用fabs()
数学模块中的方法来找到一个数字的绝对值作为浮点数。此方法将删除负号(如果有),并仅返回绝对浮点值。
trunc()
我们可以使用该方法将数字截断为最接近的整数。此方法不会删除负号(如果有)。
import math
#floating absolute value
print("floating absolute value::", math.fabs(-99.999))
print("floating absolute value of an integer::", math.fabs(-11))
#truncated integer value
print("truncated integer value::", math.trunc(-99.99))
#print
floating absolute value:: 99.999
floating absolute value of an integer:: 11.0
truncated integer value:: -99
#正如我们所见,该fabs()方法返回浮点数和整数的浮点绝对值。该trunc()方法返回给定数字的截断整数值,其中也包括负号。
pow() and log()
可以使用模块中的log()
方法找到数字的自然对数值。
pow()
该方法接受两个参数,x 和 y,并返回 x 的 y 次幂的值。
import math
print("The value of 9^3 is ::", math.pow(9, 3))
print("The log value of 5::", math.log(5))
#print
The value of 9^3 is :: 729.0
The log value of 5:: 1.6094379124341003
isfinite() 和 isinf()
可以使用该方法检查数字是否有限isfinite()
。如果该值是有限的,则此方法将返回 True,如果它是无限的,则返回false。同样,我们可以使用该方法检查一个数是否为无穷大isinf()
。
#checking whether a number is finite or not
print("Finite or not::",math.isfinite(-0.00006))
#checking whether a number is infinite or not
print("Infinite or not::",math.isinf(math.inf))
#print
Finite or not:: True
Infinite or not:: True
Square root, GCD, Power, and Exponentials of a Number
sqrt(): 求平方根
gcd(): 求最大公约数,
pow(x,y): 用于查找数字 x 的 y 次幂和
exp(x): 数 x的指数
#finding sqrt
print("The square root of 25 is ::", math.sqrt(25))
print("The square root of 10 is ::", math.sqrt(10))
#Greatest Common Divisor
print("The greatest common divisor is ::", math.gcd(6,12))
#exponential value
print("The value os e(x) is::", math.exp(-2))
#Number raised to power
print("The value of 9^3 is ::", math.pow(9, 3))
The square root of 25 is :: 5.0
The square root of 10 is :: 3.1622776601683795
The greatest common divisor is :: 6
The value os e(x) is:: 0.1353352832366127
The value of 9^3 is :: 729.0
Python Fraction Module(Python分数模块)
Python 提供了一个单独的模块来处理有理数,称为Fraction Module
#我们必须先导入分数模块。
from fractions import Fraction
from decimal import Decimal
from fractions import Fraction
#From two integers
Fraction(25, -100)
#From another fraction
Fraction(' -3/7 ')
#from a decimal
from decimal import Decimal
Fraction(Decimal('9.9'))
#from a decimal without quotes
from decimal import Decimal
Fraction(9.1)
#Empty Fraction
Fraction()
#print
-1/4
-3/7
99/10
91/10
0/1
from_float()
我们可以使用该方法从浮点数创建分数。
Fraction.from_float(0.25)
#print
Fraction(1, 4)
Printing Numbers in different Formats
我们可以使用
format
函数以不同的格式打印数字。如果是浮点数,我们可以指定要在小数点后打印的位数。借助format
函数和填充运算符,我们可以在负整数的情况下指定符号后的空格。
print("Total 6 digits with 4 after the decimal point :::")
print('{:06.4f}'.format(3.141592653589793))
print("6 digits including the sign:::")
print('{:=6d}'.format((- 234)))
print('{:=6}'.format(1))
Total 6 digits with 4 after the decimal point :::
3.1416
6 digits including the sign:::
- 234
1
Decimal Methods
getcontext()
我们可以使用该getcontext()
方法设置精度,甚至设置标志来提及我们是否需要向上或向下舍入数字。
print(Decimal(3) / Decimal(77))
import decimal
from decimal import Decimal, getcontext
getcontext().prec = 2
getcontext().rounding = decimal.ROUND_DOWN
print(Decimal(3) / Decimal(77))
#print
0.03896103896103896103896103896
0.039
使用 NumPy 的 arange() 的浮点范围
NumPy 具有arange()
获取浮点数范围的功能。它具有与 Python 内置range()
函数相同的语法和功能。此外,它允许我们在 start、stop 和 step 参数中使用浮点数。
np.arange (start, stop, step)
import numpy as np
# range for floats with np.arange()
for i in np.arange(0, 4.5, 0.5):
print(i, end=', ')
# Output 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
# Example 2
for i in np.arange(5.5, 15.5, 2.5):
print(i, end=' ')
# Output 5.5, 8.0, 10.5, 13.0,
使用的浮点数范围numpy.linspace()
np.linspace(start, stop, num, endpoint)
#start:范围的起始位置,默认情况下,如果未指定,则从 0 开始。
#stop: 区间范围的结束。
#num:要生成的样本数,默认为50。不能为负数,即输出范围内您想要的总数。
#endpoint:False如果您不想在结果中包含停止值,请将其设置为。
import numpy as np
# Float range using np.linspace()
# from 2.5 to 12.5
# num = total float numbers in the output
for i in np.linspace(2.5, 12.5, num=5):
print(i, end=', ')
# Output 2.5, 5.0, 7.5, 10.0, 12.5,
print('')
# endpoint=False to not include stop number in the result
for i in np.linspace(2.5, 12.5, num=5, endpoint=False):
print(i, end=', ')
# Output 2.5, 4.5, 6.5, 8.5, 10.5,
可以注意到上述代码中,在没有定义endpoint=False的时候,结果中包括边界值12.5