Power operator in Python

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))

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved