By now, you probably know how to multiply and divide numbers in Python. Multiplication in Python is fairly simple and easy to do. But what about using exponents? How would you raise a number to the second power, for example? If you're not sure, you'll probably find the answer pretty straightforward. To raise a number to the power of another number, you need to use the "**" operator. Where multiplying two numbers only uses one * symbol, the operator for raising one number to the power of another uses two: **.

Let's see an example. To find 4 squared (4 raised to the power of two is another way of saying it), your code would look like this:


Easy, right?

To print the results of the equation above, don't forget to use the print command.:


The output of the code would be:


The snippet below will give you an example of how we would use exponents in a real context. In the snippet, we raise two to the power of the numbers 0-5 using an anonymous function (lambda), and print the results.

squares = 5

result = list(map(lambda x: 2 ** x, range(terms)))

for i in range(squares):
 print("2 raised to the power of",i,"is",result[i])

So the output of the snippet above would be:

2 raised to the power of 0 is 1
2 raised to the power of 1 is 2
2 raised to the power of 2 is 4
2 raised to the power of 3 is 8
2 raised to the power of 4 is 16
2 raised to the power of 5 is 32

To use the snippet for yourself and get different results, change the value of the squares variable (this will give you more or less results, depending on how big or small the number is that you choose), or change the 2 value to another number. If you're looking for a way to understand how to handle exponents properly in Python, this code snippet is a great option for exploring that skill.

To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.