Skip to content

Python power of 2

  • by

Use the power operator ( ** ) to raise the left value to the power of the second value in Python. Python has three ways to get the power of 2 or any other number:

  • The ** operator. To program 25 we do 2 ** 5.
  • The built-in pow() function. 23 coded becomes pow(2, 3).
  • The math.pow() function. To calculate 35, we do math.pow(3, 5).

Python power of 2 example

Simple example code.

import math

print("2 ** 5 = ", 2 ** 5)
print("pow: ", pow(2, 3))
print("Math pow:", math.pow(3, 5))

Output:

Python power of 2 example

Do comment if you have any doubts or suggestions on this Python power 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