Python log10()函数|如何在Python中使用log10()

Python log10(x)是一个内置函数,用于获取以10为底的任何给定数字的对数。
log10(x)函数位于数学库下,因此我们需要导入数学库才能使用此函数。 Python编号方法log10()返回x大于0的x的10为底的对数。

Python log10()

内容概述

  • 1个Python log10()
  • 2个带列表和元组的Python log10()
  • 3结论
  • 4另请参见

Python math.log10()函数是math模块的库方法,用于获取数字的以2为底的对数;它接受一个数字并返回给定数字的以10为底的对数。

句法

math.log10(num)

log10()函数采用两个参数:

num->我们要查找以10为基数的日志。

返回值

Python log10()函数返回以10为底的对数,尤其是给定的数字。

但是,如果将任何值作为参数传递,则此函数将引发ValueError异常。

编程范例

请参阅以下代码。

# Importing math library
import math

# initializing values

# positive value
num = 10
print("Logarithm with base 10 of the value ", num, " is: ", math.log10(num))

# Negative number
num = -10
print("Logarithm with base 10 of the value ", num, " is: ", math.log10(num))

输出量

Logarithm with base 10 of the value  10  is:  1.0
Traceback (most recent call last):
  File "1log10.py", line 12, in 
	print("Logarithm with base 10 of the value ",num," is: ",math.log10(num))
ValueError: math domain error

在此程序中,我们首先初始化该值,然后以10为底计算该数字的对数。在下一行,我们想计算一个负数的对数,但是按照规则,该程序抛出了一个例外ValueError。

程序2

请参阅以下程序。

# app.py

# Importing math library
import math

# taking input from user values

# positive value
num = int(input("Enter a num to find log10(num): "))
print("Logarithm with base 10 of the value ", num, " is: ", math.log10(num))

输出量

Enter a num to find log10(num): 15
Logarithm with base 10 of the value  15  is:  1.1760912590556813

在此程序中,我们从用户处获取了输入,然后计算了以10为底的对数。

带有列表和元组的Python log10()

请参阅以下代码。

import math

Tup = (1, 2, 11, -4, 5)  # Tuple Declaration
Lis = [-1, 2, -11.21, -4, 5]  # List Declaration

print('The log10() value of Positive Number = %.2f' % math.log10(1))
print('The log10() value of Positive Decimal = %.2f' % math.log10(2.5))

print('The log10() value of Tuple Item = %.2f' % math.log10(Tup[2]))
print('The log10() value of List Item = %.2f' % math.log10(Lis[4]))

print('The log10() value of Multiple Number = %.2f' % math.log10(2 + 7 - 5))
print('The log10() value of String Number = ', math.log10('Locke and Key'))

输出量

python3 app.py
The log10() value of Positive Number = 0.00
The log10() value of Positive Decimal = 0.40
The log10() value of Tuple Item = 1.04
The log10() value of List Item = 0.70
The log10() value of Multiple Number = 0.60
Traceback (most recent call last):
  File "app.py", line 13, in 
    print('The log10() value of String Number = ', math.log10('Locke and Key'))
TypeError: must be real number, not str

如果传递字符串,则给出TypeError。

结论

Python log10()函数计算给定基数为10的对数值。我们找到不同数据类型的基数为10的对数值,并显示输出。

也可以看看

Python日志(2)

Python日志(x,基本)

Python log1p(x)

Python exp()

Python trunc()

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢