Python Power Operator and Function

January 23, 2023

Introduction

All high-level programming languages include mathematical operations. Math helps create models, simulations, and calculation-based applications. One crucial operation is raising a number to a power or exponentiation.

Powers are a quicker way to write iterative multiplication. Python offers two ways to calculate the power of a number.

This guide shows how to use the power operator and function in Python with examples.

Python Power Operator and Function

Prerequisites

  • Python version 3 installed.
  • A code editor to write the code.
  • An IDE or terminal to run and test the code examples.

Python Power pow() Function Syntax

The power function is a built-in method for calculating powers and modulo (division remainder). The method performs a different calculation depending on the number of variables provided.

The syntax for the command is:

pow(base, exponent, modulo)

The function takes two or three arguments. When used with two arguments, the method calculates an exponential expression. For example:

pow(base, exponent)

The function outputs the operation result of the base value to the power of the exponent value.

Note: Python packages, such as NumPy and SciPy, enable performing advanced math calculations.

Python Power pow() Example

To use the Python pow() function, provide two values directly or through variable reference. The example below demonstrates how the use the pow() function: 

print(pow(2, 3))
print(pow(5, 2))
base = 10
power = 2
print(print(pow(base, power)))
Python pow() example output

Each line does the following:

  • The expression in line 1 calculates 2 to the power of 3, which is equivalent to 2*2*2.
  • The second expression in line 2 calculates 5 to the power of 2, which is the same as 5*5.
  • The final expression in line 5 calculates 10 to the power of 2 using variables from lines 2-3.

The program prints the calculated result of all three operations.

Python Power ** Operator Syntax

The ** operator is a built-in operator for calculating powers in Python. The syntax for the operator is:

base**exponent

The operator calculates the exponential expression directly and outputs the result.

Note: The ** operator works in the same way as the pow() method.

Python Power Operator Example

Use the Python power operator directly on two numbers or variables. For example:

print(2**3)
print(5**2)
base = 10
power = 2
print(base**power)
Python power operator example output

Each line performs a different exponential operation using the ** operator. The program prints the result for each power operation to the console.

Conclusion

After reading this guide, you know two ways to perform exponentiation in Python: the pow() method or the ** power operator.

For more Python guides, see our comprehensive guide on Python comments.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Install NumPy
May 8, 2024

NumPy (Numerical Python) is an open-source library for the Python programming language. It is used for scientific computing and working with arrays. This tutorial...
Read more
Python SciPy Tutorial - A Guide for Beginners
February 25, 2021

When NumPy is not enough, SciPy has you covered. SciPy is a Python library used for scientific computing. It includes advanced computation...
Read more
Bash Math Operations (Bash Arithmetic) Explained
April 14, 2022

You can perform math and arithmetic operations in Bash directly. Although the functionalities are basic, the examples from this tutorial...
Read more
Linux bc Command with Examples
January 12, 2023

The bc command is a command-line utility in Linux allowing users to perform complex mathematical and arithmetic calculations. This...
Read more