w3resource logo


Python Exercises

Python Challenges: Check if a given positive integer is a power of two

Python Challenges - 1: Exercise-1 with Solution

Write a Python program to check if a given positive integer is a power of two.

Input : 4
Output : True

Explanation :

Python: Check if a given positive integer is a power of two

Sample Solution :-

Python Code :

def is_Power_of_two(n):
        return n > 0 and (n & (n - 1)) == 0
print(is_Power_of_two(4))
print(is_Power_of_two(36))
print(is_Power_of_two(16))

Output :

True                                                                    
False                                                                   
True

Flowchart:

Python Flowchart: Check if a given positive integer is a power of two

Python Code Editor:

def is_Power_of_two(n):
        return n > 0 and (n & (n - 1)) == 0
print(is_Power_of_two(4))
print(is_Power_of_two(36))
print(is_Power_of_two(16))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.