Python Tutorials

Python Programs

Python Compute the Power of a Number


Python Compute the Power of a Number

In this tutorial, we will learn how to compute the power of a number in Python. We will cover the basic concept of exponentiation and implement a function to perform the calculation.


What is Exponentiation

Exponentiation is a mathematical operation that involves raising a base number to the power of an exponent. For example, 2 raised to the power of 3 is 23 = 8.


Syntax

The syntax to compute the power of a number in Python is:

def compute_power(base, exponent):
    return base ** exponent


Computing the power of a number

We can create a function to compute the power of a given base and exponent using the exponentiation operator (**).

For example,

  1. Define a function named compute_power that takes two parameters base and exponent.
  2. Use the exponentiation operator (**) to raise the base to the power of the exponent.
  3. Return the result.
  4. Call the function with sample values and print the result.

Python Program

def compute_power(base, exponent):
    return base ** exponent

# Sample values
base = 2
exponent = 3

# Compute the power
result = compute_power(base, exponent)

# Print the result
print(f'{base} raised to the power of {exponent} is:', result)

Output

2 raised to the power of 3 is: 8