Log functions in Python

Log functions in Python

Python offers many inbuild logarithmic functions under the module “math” which allows us to compute logs using a single line. There are 4 variants of logarithmic functions, all of which are discussed in this article.

1. log(a,(Base)) : This function is used to compute the natural logarithm (Base e) of a. If 2 arguments are passed, it computes the logarithm of desired base of argument a, numerically value of log(a)/log(Base).

Syntax : math.log(a,Base)

Parameters :

a : The numeric value

Base : Base to which the logarithm has to be computed.

Return Value : Returns natural log if 1 argument is passed and log with specified base if 2 arguments are passed.

Exceptions : Raises ValueError is a negative no. is passed as argument.

 `# Python code to demonstrate the working of` `# log(a,Base)` ` ` `import` `math` ` ` `# Printing the log base e of 14` `print` `(``"Natural logarithm of 14 is : "``, end``=``"")` `print` `(math.log(``14``))` ` ` `# Printing the log base 5 of 14` `print` `(``"Logarithm base 5 of 14 is : "``, end``=``"")` `print` `(math.log(``14``,``5``))`

Output :

```Natural logarithm of 14 is : 2.6390573296152584
Logarithm base 5 of 14 is : 1.6397385131955606
```

2. log2(a) : This function is used to compute the logarithm base 2 of a. Displays more accurate result than log(a,2).

```Syntax :
math.log2(a)
Parameters :
a : The numeric value
Return Value :
Returns logarithm base 2 of a
Exceptions :
Raises ValueError is a negative no. is passed as argument.
```

 `# Python code to demonstrate the working of` `# log2(a)` ` ` `import` `math` ` ` `# Printing the log base 2 of 14` `print` `(``"Logarithm base 2 of 14 is : "``, end``=``"")` `print` `(math.log2(``14``))`

Output :

```Logarithm base 2 of 14 is : 3.807354922057604
```

3. log10(a) : This function is used to compute the logarithm base 10 of a. Displays more accurate result than log(a,10).

```Syntax :
math.log10(a)
Parameters :
a : The numeric value
Return Value :
Returns logarithm base 10 of a
Exceptions :
Raises ValueError is a negative no. is passed as argument.
```
 `# Python code to demonstrate the working of` `# log10(a)` ` ` `import` `math` ` ` `# Printing the log base 10 of 14` `print` `(``"Logarithm base 10 of 14 is : "``, end``=``"")` `print` `(math.log10(``14``))`

Output :

```Logarithm base 10 of 14 is : 1.146128035678238
```

3. log1p(a) : This function is used to compute logarithm(1+a) .

```Syntax :
math.log1p(a)
Parameters :
a : The numeric value
Return Value :
Returns log(1+a)
Exceptions :
Raises ValueError is a negative no. is passed as argument.
```
 `# Python code to demonstrate the working of` `# log1p(a)` ` ` `import` `math` ` ` `# Printing the log(1+a) of 14` `print` `(``"Logarithm(1+a) value of 14 is : "``, end``=``"")` `print` `(math.log1p(``14``))`

Output :

```Logarithm(1+a) value of 14 is : 2.70805020110221
```

Exception

1. ValueError : This function returns value error if number is negative.

 `# Python code to demonstrate the Exception of` `# log(a)` ` ` `import` `math` ` ` `# Printing the log(a) of -14` `# Throws Exception` `print` `(``"log(a) value of -14 is : "``, end``=``"")` `print` `(math.log(``-``14``))`

Output :

```log(a) value of -14 is :
```

Runtime Error :

```Traceback (most recent call last):
print (math.log(-14))
ValueError: math domain error
```
Practical Application

One of the application of log10() function is that it is used to compute the no. of digits of a number. Code below illustrates the same.

 `# Python code to demonstrate the Application of` `# log10(a)` ` ` `import` `math` ` ` `# Printing no. of  digits in 73293` `print` `(``"The number of digits in 73293 are : "``, end``=``"")` `print` `(``int``(math.log10(``73293``) ``+` `1``))`

Output :

```The number of digits in 73293 are : 5
```

Last Updated on October 15, 2021 by admin

Python – Ways to remove duplicates from listPython – Ways to remove duplicates from list

Python – Ways to remove duplicates from list This article focuses on one of the

Initialize an Empty Dictionary in PythonInitialize an Empty Dictionary in Python

Initialize an Empty Dictionary in Python Dictionary in Python is an unordered collection of data values,

Python – shutil.copyfile() methodPython – shutil.copyfile() method

Python | shutil.copyfile() method   # Python program to explain shutil.copyfile() method    # importing

Mutable vs Immutable Objects in PythonMutable vs Immutable Objects in Python

Mutable vs Immutable Objects in Python Every variable in python holds an instance of an

Writing to an excel sheet using PythonWriting to an excel sheet using Python

Writing to an excel sheet using Python Using xlwt module, one can perform multiple operations on spreadsheet.

Python Regex: re.search() VS re.findall()Python Regex: re.search() VS re.findall()

Python Regex: re.search() VS re.findall() A Regular expression (sometimes called a Rational expression) is a sequence of

Bloom Filters – Introduction and ImplementationBloom Filters – Introduction and Implementation

Bloom Filters – Introduction and Implementation Suppose you are creating an account on Geekbook, you

Defaultdict in PythonDefaultdict in Python

Defaultdict in Python Dictionary in Python is an unordered collection of data values that are