Determine Power of Two in Python

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, it is often necessary to determine if a given number is a power of two. This can be useful in a variety of applications, such as optimizing algorithms or checking for errors in data transmission. In this challenge, we will write a Python function to determine if a number is a power of two.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/math_probability("`Math Probability`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") subgraph Lab Skills algorithm/math_probability -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/booleans -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/conditional_statements -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/tuples -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/function_definition -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/classes_objects -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/encapsulation -.-> lab-268852{{"`Determine Power of Two in Python`"}} python/raising_exceptions -.-> lab-268852{{"`Determine Power of Two in Python`"}} end

Power Two

Problem

Write a Python function called is_power_of_two that takes in an integer as its parameter and returns True if the input is a power of two, and False otherwise. A power of two is any number that can be expressed as 2^n, where n is an integer. For example, 2, 4, 8, and 16 are all powers of two.

Requirements

The is_power_of_two function must meet the following requirements:

  • The input number must be an integer.
  • The function must handle invalid inputs gracefully.
  • The output must be a boolean.
  • The function must fit within memory constraints.

Example Usage

Here are some examples of how the is_power_of_two function should behave:

  • is_power_of_two(None) should raise a TypeError.
  • is_power_of_two(0) should return False.
  • is_power_of_two(1) should return True.
  • is_power_of_two(2) should return True.
  • is_power_of_two(15) should return False.
  • is_power_of_two(16) should return True.

Summary

In this challenge, we wrote a Python function to determine if a given number is a power of two. We learned that a power of two is any number that can be expressed as 2^n, where n is an integer. We also learned how to handle invalid inputs and ensure that our function meets memory constraints.

Other Algorithm Tutorials you may like