In python we have many basic mathematical operators, the exponent operator is considered an arithmetic operator as it takes numerical values as an input and manipulates the input values directed by the operator.
In this article, we will teach you how to use the exponent operator (**) in python.
What Are Python Exponents?
In python programming, unlike maths where a single arrow (^) is used to raise the power, we can use double asterisks (**) to create exponents. This is called the exponent operator where we can take an input number and multiply itself by several times.
This operator helps us with tasks that require fast computation of large positive integer powers of a number.
How to use Python Exponents?
To use python exponents it is just like maths, where on the left side we have the base followed by our two asterisks (**) and on the right-hand side we specify the power number.
Example 1: Raising power of Integers
Take our code for an example to raise the power of our integer we just need to declare 5**3, basically what this means is 5*5*5 which equals 125.
Example 2: Raising power of integers with BODMAS
Let’s take a look at another example, where it includes brackets and exponentials. If you actually go through the code, you’ll realize that BODMAS actually is in effect. Which is the order of mathematical operations where brackets come first followed by powers and etc.
So basically to break down this code we complete the inside brackets first which is 5+3 = 8, then we raise the power by 3.
Example 3: Raising power with variables
A Python variable is a reserved memory location to store values, a variable is created the moment you first assign a value to it. We can use variables to raise the power of the stored value as well.
Take a look at the code below, we assigned the value 5 to our variable number then used our exponent to raise the power by 3. Python automatically knows that you would like to raise the integer 5 since it is stored in our variable.
number = 5 print(number**3)