find the logarithm of a number in python

To find the logarithm of a number in Python, you can use the math library which contains functions for mathematical operations like logarithmic functions. The math.log() function can be used to calculate the logarithm of a number.

main.py
import math

num = 10
logarithm = math.log(num)

print(logarithm)
66 chars
7 lines

In the code above, we imported the math library and used the math.log() function to find the logarithm of the number 10. The result is then printed to the console. You could also specify the base of the logarithm as a second argument to the function, for example to calculate the base 2 logarithm:

main.py
import math

num = 10
logarithm = math.log(num, 2)  # base 2 logarithm

print(logarithm)
89 chars
7 lines

gistlibby LogSnag