Skip to content

Python logarithm base 2

  • by

Use math log2() function to get given number logarithm base 2. This method returns the base-2 logarithm of a number.

math.log2(x)

Note: The functions throw ValueError if we pass a negative number as an argument.

Python logarithm base 2 example

Simple example code calculate the base-2 logarithm of a number.

import math

print("log2(10) :", math.log2(10))

print("log2(2) :", math.log2(2))

Output:

Python logarithm base 2

How to log base 2 in Python numpy?

Answer: Use the numpy.log2() method in Python Numpy.This method returns Base-2 logarithm of x.

import numpy as np

print(np.log2(2))

Output: 1

Do comment if you have any doubts or suggestions on this Python log topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading