Python’s math module has provided us with many important functions such as sqrt (), which is used to calculate the **square root of a number**. We also have functions to calculate** cos, sin, tan, and exponent **of a number. Not only this, but we can also calculate **Natural Log**, commonly known as** ln** in python. In this article, we will study how to calculate the natural log of a number using the math module and some other ways.

A normal log means base 10 logarithm (or example log10(x)), but a **natural log is a base e algorithm (for example loge(x) or ln(x))**. The formula for calculating natural log is ln(x)= log(x) / log (2.71828). We will also learn how to calculate the natural log of every element of an array.

Note-

log(e(x)) = x

## How to Calculate Ln in python?

We will use a module named math in python, which provides us the direct method to calculate the natural log.

## Using Math Module

### Syntax-

**math.log(x[, base])-**

Parameters-**X**– It is the number whose natural log we want to calculate. It must be a numeric value.**Base-** By default, the value of this is ‘e.’ It means that if we do not provide any base, it will calculate the natural log. But we can change the value of the base according to our needs.

Note- Returns Value Error if negative or zero value is passed.

### Examples of ln in python

Let us learn how to use the above function for calculating ln in python.

```
a=5
x=10
y=20
z=10
# Using log() to find the natural log of the element
print( " number: ", x ," log: ", math.log(x))
print(" number : ", y ," log: ", math.log(y))
print(" number: ", z ," log: ", math.log(z))
print(" number:",a ," log: ", math.log(a))
```

Output-

number: 10 log: 2.302585092994046

number: 20 log: 2.995732273553991

number: 10 log: 2.302585092994046

number: 5 log: 1.6094379124341003

To calculate the standard log, we will use the **base 10**.

```
a=5
x=10
y=20
z=10
print("number:",x,"log:",math.log(x,10))
print("number:",y,"log:",math.log(y,10))
print("number:",z,"log:",math.log(z,10))
print("number:",a,"log:",math.log(a,10))
```

Output-

number: 10 log: 1.0

number: 20 log: 1.301029995663981

number: 10 log: 1.0

number: 5 log: 0.6989700043360187

## Using Numpy module

**numpy.log()**–

**Parameters:**

X- Though there are many parameters in numpy.log(), we will study only one parameter for calculating the natural log of one element. Here, x is that element.

### Examples of ln in python using numpy

```
a=59
x=50
y=200
z=100
print(" number: ", x ," log: ", numpy.log(x))
print(" number: ", y ," log: ", numpy.log(y))
print(" number: ", z ," log: ", numpy.log(z))
print(" number: ", a ," log: ", numpy.log(a))
```

Output-

number: 50 log: 3.912023005428146

number: 200 log: 5.298317366548036

number: 100 log: 4.605170185988092

number: 59 log: 4.07753744390572

## Calculating the ln of elements of an Array in python

**Numpy.log(arr,out_arr)**

**Parameters**:

**Arr**– In this parameter, we have to pass the array, whose ln we have to find.**Out_Arr** – This array should be of the same size as the input array. The log of the elements of the array will be passed in this array

import numpy as np.

```
import numpy as np
# input list
list1=[10,20,30,40,50,60,70,80,90,100,110,120,130,140]
# Converting the list into array
arr=np.array(list1)
# Finding the Natural Log and storing it into another array
out_arr=np.log(arr)
print(out_arr)
```

Output-

[2.30258509 2.99573227 3.40119738 3.68887945 3.91202301 4.09434456 4.24849524 4.38202663 4.49980967 4.60517019 4.70048037 4.78749174 4.86753445 4.94164242]

If you have still not understood the difference between Natural Log and Standard Log, let us plot a graph between the Natural Log and Standard Log of the same input array.

## Plotting Natural Log (ln) vs Standard Log in Python

```
import matplotlib.pyplot as plt
# If we use %matplotlib inline, we do not need to use plt.show() again and again
%matplotlib inline
```

```
import numpy as np
# input list
list1=[10,20,30,40,50,60,70,80,90,100,110,120,130,140]
# Converting the list into array
arr=np.array(list1)
# Finding the Natural Log and storing it into another array
out_arr=np.log(arr)
# Finding the Standard Log (base:10) and storing it into another array
out_arr2=np.log10(arr)
# Plotting input array with Natural Log
plt.plot(arr,out_arr,color="red",marker=".",label="Natural Log")
# Plotting input array with Standard Log
plt.plot(arr,out_arr2,color="blue",marker='.',label="Standard Log")
# Giving the x axis a label
plt.xlabel("input array")
# Giving the y axis a label
plt.ylabel("output array")
# Giving the title to the grapph
plt.title("Natural Log vs Standard Log")
plt.legend()
```

Output-

Observe that as the value of our input is increasing, the output using a natural log is increasing exponentially. But with the Standard log, the increase in value is not that great.

## Must Read

- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python

## Conclusion

There is widespread use of ln in python over Standard Log. It is generally used when we want to measure the growth w.r.t. Time. For example, if we’re going to know the rate of decay or growth as per the time passes, it is an interesting example of Natural Log. Other examples include the Richter Scale (used for measuring the intensity of Earthquake), calculating the population growth, etc

Try to run the programs on your side and let us know if you have any queries.

*Happy Coding!*