PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to Find Power of a Number (a^n)

Python Programs to Find Power of a Number (a^n)

Updated on: March 27, 2025 | Leave a Comment

The power of a number (also called exponentiation) refers to multiplying a number by itself a specified number of times. It is represented as: a^n. Where:

  • a is the base (the number being multiplied),
  • n is the exponent (the number of times a is multiplied by itself).
  • Mathematical Representation: a^n = a × a ×⋯× a (n times)
  • Example: 3^2 = 3 × 3 = 9

This article covers the various methods to calculate the power of a number in Python, along with explanations and examples.

Table of contents

  • 1. Using a Loop – Manual approach
  • 2. Using the ** Operator
  • 3. Using the pow() Function
  • 4. Using the math.pow() Function
  • 5. Using Recursion
  • Summary

1. Using a Loop – Manual approach

A manual approach to calculate the power of a number is to use a loop. This method multiplies the base by itself repeatedly for the given exponent.

For Example, result = base^exponent = base * base * base = 2 * 2 * 2 = 8

Code Example

base = 2
exponent = 3
result = 1

for _ in range(exponent):
    result *= base
print(result)  # Output: 8Code language: Python (python)

Explanation

  • range(exponent): It generates a sequence of numbers from 0 to exponent - 1, controlling how many times the loop runs. In this example, the exponent is 3, so it loops from 0 to 2.
  • *= operator: It is a compound assignment operator that performs multiplication and then assigns the result back to the variable. result *= base is equivalent to result = result * base.

2. Using the ** Operator

The ** operator is the most straightforward way to compute the power of a number in Python.

Syntax: base ** exponent

Code Example

base = 2
exponent = 3
result = base ** exponent  # 2^3 = 8
print(result)  # Output: 8Code language: Python (python)

3. Using the pow() Function

The pow() function is a built-in function in Python that calculates the value of a number raised to a power.

Syntax: pow(base, exponent[, mod])

  • The optional third argument (mod) calculates (base ** exponent) % mod.
  • Modulo Operator (%): It returns the remainder of dividing the left-hand operand by the right-hand operand.

For Example:

pow(2, 3, 5) = (2^3) % 5 = (2 * 2 * 2) % 5 = 8 % 5 = 3 (i.e., 8 divided by 5, so the reminder is 3.

Code Example without modulus

result = pow(2, 3)  # 2^3 = 8
print(result)  # Output: 8Code language: Python (python)

Code Example with modulus

result = pow(2, 3, 5)  # (2^3) % 5 = 3
print(result)  # Output: 3Code language: Python (python)

4. Using the math.pow() Function

The math.pow() function is part of the math module and always returns a float, even if the result is a whole number.

Syntax: math.pow(base, exponent)

For Example: 2^3 = 2 * 2 * 2 = 8 but math.pow(2, 3) = 8.0

Code Example

import math
result = math.pow(2, 3)  # 2^3 = 8.0
print(result)  # Output: 8.0Code language: Python (python)

5. Using Recursion

Recursion is a programming technique where a function calls itself to solve smaller instances of a problem until a base condition is met.

Recursion is another way to calculate the power of a number. This method defines a function that calls itself until the exponent reaches zero.

For Example:

  • For power(2, 3):
    • power(2, 3) → 2 * power(2, 2)
    • power(2, 2) → 2 * power(2, 1)
    • power(2, 1) → 2 * power(2, 0)
    • power(2, 0) → 1 (base case)
  • As the recursion returns:
    • power(2, 1) → 2 * 1 = 2
    • power(2, 2) → 2 * 2 = 4
    • power(2, 3) → 2 * 4 = 8
  • The final result is 8

Code Example

def power(base, exponent):
    if exponent == 0:
        return 1
    return base * power(base, exponent - 1)

result = power(2, 3)  # 2^3 = 8
print(result)  # Output: 8Code language: Python (python)

Summary

Each of these methods offers unique benefits, depending on the use case:

  • For simplicity and readability: Use the ** operator or pow() function.
  • For floating-point results: Use math.pow().
  • For manual control: Use loops or recursion.
  • For modular arithmetic: Use the third parameter of pow().

Note:

  • For advanced computation: To calculate the power of scalars and arrays numpy library offers a power() function (numpy.power(base, exponent)).
  • The sympy library, designed for symbolic mathematics, provides a Pow() function for computing powers.
  • Both numpy and sympy are not available in the default Python installation. We need to install them separately.

By understanding these methods, you can select the best approach to compute the power of a number in Python for your specific needs.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com

Advertisement
Advertisement