Home » Python

math.log() method with example in Python

Python math.log() method: Here, we are going to learn about the math.log() method with example in Python.
Submitted by IncludeHelp, on April 20, 2019

Python math.log() method

math.log() method is a library method of math module, it is used to get the natural logarithm of a given number to base e (by default). It accepts the number and a base (which an optional) and returns the natural logarithm.

Note: If we provide anything else except a number, the method returns a TypeError – "TypeError: a float is required".

Syntax of math.log() method:

    math.log(x [, base])


  • x – is the number whose logarithm to be calculated.
  • base – is an optional parameter, it is used to defined any specific base for the logarithm.

Return value: float – it returns a float value that is the natural or base specific logarithm of the number x.


    x = 21

    # function call


Python code to demonstrate example of math.log() method

# python code to demonstrate example of 
# math.log() method

# importing math module
import math

# log() with 1 parameter
x = 21
print("Natural logarithm of ", x, " is = ", math.log(x))

# log() with 2 parameters
x = 21
base = 5
print("logarithm of ", x, " with base ", base, " is = ", math.log(x, base))


Natural logarithm of  21  is =  3.044522437723423
logarithm of  21  with base  5  is =  1.8916681496081529

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.