Friday, November 15, 2024
Google search engine
HomeGuest BlogsPython Power Operator and Function

Python Power Operator and Function

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 FunctionPython 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 outputPython 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 outputPython 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

RELATED ARTICLES

Most Popular

Recent Comments