• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Python power function – Exponentiate Numbers with math.pow()

Python power function – Exponentiate Numbers with math.pow()

January 29, 2022 Leave a Comment

The Python power function pow() from the math module allows us to perform exponentiation and find roots of numbers easily.

import math

square_of_4 = math.pow(4,2)
sqrt_of_4 = math.pow(4,1/2)

The Python math module has many powerful functions which make performing certain calculations in Python very easy.

One such calculation which is very easy to perform in Python is finding the value of numbers to the certain power.

We can use the Python power function pow() from the math module to do exponentiation and find roots of numbers easily.

The pow() function takes two numbers as input, the first number is the base and the second number is the exponent.

For example, for squaring in Python, we pass “2” to the second parameter in the pow() function.

Below are some examples of how to use the pow() function to find the squares of various numbers.

import math

print(math.pow(4, 2))
print(math.pow(9, 2))
print(math.pow(13, 2))
print(math.pow(90, 2))
print(math.pow(2182, 2))

#Output:
16.0
81.0
169.0
8100.0
4761124.0

Exponentiating Numbers in Python with math.pow()

We can use the math pow() function in Python to perform exponentiation for any exponent.

For example, if we want to find the value of a number to the 3rd power, we pass “3” as the second argument.

import math

print(math.pow(4, 3))
print(math.pow(9, 3))

#Output:
64.0
729.0

To find the value of a number to the 5th power, we pass “5” as the second argument.

import math

print(math.pow(4, 5))
print(math.pow(9, 5))

#Output:
1024.0
59049.0

Finding the Square Root of a Number in Python with math.pow()

The pow() function from the Python math module also lets us compute square roots.

For a square root, we pass “1/2” to the second parameter in the pow() function. For roots, we must pass positive numbers to pow() for the first argument.

Below are some examples of how to use the pow() function to find square roots.

import math

print(math.pow(4, 1/2))
print(math.pow(9, 1/2))
print(math.pow(13, 1/2))
print(math.pow(90, 1/2))
print(math.pow(2182, 1/2))

#Output:
2.0
3.0
3.605551275463989
9.486832980505138
46.71188285650665

Finding the nth Root of a Number in Python with math.pow()

The pow() function from the Python math module also lets us compute nth roots.

For a nth root, given any n, we pass “1/n” to the second parameter in the pow() function. Again, for roots, we must pass positive numbers to pow() for the first argument.

Below are some examples of how to use the pow() function to find nth roots.

import math

#n=2
print(math.pow(4,1/2))

#n=3
print(math.pow(9,1/3))

#n=5
print(math.pow(13,1/5))

#n=6
print(math.pow(90,1/6))

#n=9
print(math.pow(2182,1/9))

#Output:
2.0
2.080083823051904
1.6702776523348104
2.1169328630254585
2.3495455051249885

Finding the Roots of Negative Numbers in Python

If you try to pass a negative number to the math pow() function, you will get a ValueError.

There are two ways you can find the roots of negative numbers in Python.

If the root is odd (3rd, 5th, 7th, etc.), you can use the built in exponentiation operator **.

import math
#n=9
print(math.pow(-2182,1/9))

#Output:
-2.3495455051249885

If the root is 2, you can use the square root function sqrt() from Python cmath module.

import cmath
print(cmath.sqrt(-2182))

#Output:
46.71188285650665j

Hopefully this article has been beneficial for you to learn how to use the Python power function pow() from the math module to exponentiate and find roots of numbers.

Other Articles You'll Also Like:

  • 1.  Print Time Elapsed in Python
  • 2.  How to Check if String Contains Lowercase Letters in Python
  • 3.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 4.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 5.  Remove Every Nth Element from List in Python
  • 6.  Convert List to Set with Python
  • 7.  Python turtle Colors – How to Color and Fill Shapes with turtle Module
  • 8.  Python max float – What’s the Maximum Float Value in Python?
  • 9.  How to Clear Turtle Screen in Python with clear() Function
  • 10.  Read Pickle Files with pandas read_pickle Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy