# 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])
```

Parameter(s):

• 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.

Example:

```    Input:
x = 21

# function call
print(math.log(x))

Output:
3.044522437723423
```

### 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))
```

Output

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

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates