Python Reference Library →

Python pow() Function – Raise a Number to a Power

Let’s look at pow() function – raise a number to a power

  1. Notice

    pow() computes exponentiation. Its three-argument form can calculate an integer power modulo another integer.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3
print(pow(number, 2))
print(pow(number, 2, 5))
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

pow() computes exponentiation. Its three-argument form can calculate an integer power modulo another integer.

Picture pow() function – raise a number to a power

pow() computes exponentiation. Its three-argument form can calculate an integer power modulo another integer.

Image brief

Alt text
A diagram for Python pow() Function – Raise a Number to a Power. Instructions: number = 3 print(pow(number, 2)) print(pow(number, 2, 5)). Displayed result: 9 4. The final example bindings are number --> object 1 (int). object 1: 3
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python pow() Function – Raise a Number to a Power". Teaching focus: pow() computes exponentiation. Its three-argument form can calculate an integer power modulo another integer. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 3 print(pow(number, 2)) print(pow(number, 2, 5)) | v DISPLAYED RESULT 9 4 FINAL BINDINGS number --> object 1 (int) object 1: 3 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python pow() Function – Raise a Number to a Power. Instructions: number = 3 print(pow(number, 2)) print(pow(number, 2, 5)). Displayed result: 9 4. The final example bindings are number --> object 1 (int). object 1: 3

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3
print(pow(number, 2))
print(pow(number, 2, 5))
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
9
4

What do you expect this program to print?

What does this example display?

number = 3
print(pow(number, 2))
print(pow(number, 2, 5))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

9 4

Follow the printed values in order. The output is: 9 4

More examples and reference

Python pow() Function

The pow() function in Python returns the value of a number raised to a given power. It can also return the result modulo another number. This makes it very useful in both basic arithmetic and advanced algorithms like cryptography.

Syntax

pow(base, exponent[, modulus])

Parameters:

  • base – The number to be raised (required)
  • exponent – The power to raise the base to (required)
  • modulus – The number to divide the result by and return remainder (optional)

Returns:

  • If 2 arguments: base ** exponent
  • If 3 arguments: (base ** exponent) % modulus

Example 1: Basic Power Calculation

print(pow(2, 3))
8

Example 2: Power with Negative Exponent

print(pow(2, -2))
0.25

Example 3: Power with Modulus

print(pow(2, 3, 5))
3

This returns (2**3) % 5 = 8 % 5 = 3.

Use Cases

  • Quickly raise a number to a power
  • Perform modular exponentiation efficiently (especially in large number computations)
  • Used in cryptographic algorithms like RSA

Important Notes

  • pow(a, b, c) is more efficient than (a ** b) % c
  • If modulus is provided, all values must be integers
  • For floating-point results, omit the modulus

Common Mistakes

  • Using pow() with three arguments and passing floats — will raise TypeError
  • Forgetting to use ** instead of ^ for exponentiation (Python uses ^ as bitwise XOR)

Interview Tip

In competitive programming and coding interviews, pow(a, b, c) is a fast and safe way to compute large powers under a modulus, avoiding overflow errors.

Summary

  • pow(x, y) returns x**y
  • pow(x, y, z) returns (x**y) % z efficiently
  • Useful in math, encryption, and large number problems

Practice Problem

Write a program to compute the last digit of ab for given input values:

a = int(input("Enter base: "))
b = int(input("Enter exponent: "))
print("Last digit is:", pow(a, b, 10))

What to remember

Connect what you noticed in the example with the idea below.

Remember

pow() computes exponentiation. Its three-argument form can calculate an integer power modulo another integer.

Watch out for this idea

pow(a, b) adds a and b.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →