Trusted answers to developer questions

Power operator in Python

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Python has a number of basic operators including some arithmetic operators.

An arithmetic operator takes numerical values as an input and returns a single numerical value, manipulating the input values in a certain way as directed by the operator.


Power (exponent) operator

One important basic arithmetic operator, in Python, is the exponent operator. It takes in two real numbers as input arguments and returns a single number.



The operator that can be used to perform the exponent arithmetic in Python is **.

Given two real number operands, one on each side of the operator, it performs the exponential calculation (2**5 translates to 2*2*2*2*2).

svg viewer

Note: Exponent operator ** in Python works in the same way as the pow(a, b) function.


Examples

Here are some examples that show how to use the ** operator:

print('2**5 evaluates to: ', 2**5)
print('1.1**13 evaluates to: ', 1.1**13)
print('32**2.2 evaluates to: ' , 32**2.2)

Now let's compare the results of the Exponent operator ** and the build-in pow(a,b) function as we stated in the note that they work the same way.

print('Exponent Operator Results\n')
print('2**5 evaluates to:', 2**5)
print('1.1**13 evaluates to:', 1.1**13)
print('32**2.2 evaluates to:', 32**2.2, '\n')
print('Pow() function Results\n')
print('pow(2, 5) evaluates to:', pow(2, 5))
print('pow(1.1, 13) evaluates to:', pow(1.1, 13))
print('pow(32, 2.2) evaluates to:', pow(32, 2.2))

RELATED TAGS

python
operators
arithmetic
exponent
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?