• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Check if Number is Power of 2 in Python

How to Check if Number is Power of 2 in Python

February 8, 2022 Leave a Comment

In Python, we can easily check if a number is a power of 2 by taking the log base 2 and seeing if the result is a whole number.

import math

def isPowerOfTwo(num):
    if math.log(num,2).is_integer():
        return True
    else:
        return False

print(isPowerOfTwo(2))
print(isPowerOfTwo(12))
print(isPowerOfTwo(32))
print(isPowerOfTwo(94))

#Output:
True
False
True
False

When working with numbers in our programs, sometimes it can be useful to be able to easily check if a number is a power of another number.

In Python, we can check if a number is a power of 2 very easily.

To check if a number is a power of 2, we take the log of that number base 2 and see if the result is a whole number.

To take the log of a number, we use the math module log() function. Then, to see if a number is a whole number, we use the Python float is_integer() function.

Below is the Python code for checking if a number is a power of 2 in Python.

import math

def isPowerOfTwo(num):
    if math.log(num,2).is_integer():
        return True
    else:
        return False

print(isPowerOfTwo(2))
print(isPowerOfTwo(12))
print(isPowerOfTwo(32))
print(isPowerOfTwo(94))

#Output:
True
False
True
False

How to Check if A Number is a Power of Another Number in Python

For the more general case, where you want to check if a number is a power of another number, we can simply adjust our Python function to take another parameter.

Then, in a similar way to above, all we need to do is change the base of the logarithm.

Below is a Python function for you to check if a number is a power of any other number.

import math

def isPower(num1, num2):
    if math.log(num1, num2).is_integer():
        return True
    else:
        return False

print(isPower(2,2))
print(isPower(12,3))
print(isPower(64,4))
print(isPower(81,9))

#Output:
True
False
True
True

Hopefully this article has been useful for you to learn how to check if a number is a power of two in Python.

Other Articles You'll Also Like:

  • 1.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 2.  Using Python to Remove Commas from String
  • 3.  Calculate Sum of Dictionary Values in Python
  • 4.  How to Remove All Occurrences of a Character in a List Using Python
  • 5.  How to Repeat a Function in Python
  • 6.  Remove Last Element from List Using Python
  • 7.  Get pandas Index Values as List in Python
  • 8.  Using Python to Find Second Smallest Value in List
  • 9.  Python Get Yesterday’s Date
  • 10.  Using pandas sample() to Generate a Random Sample of a DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy