Home  >  Article  >  Backend Development  >  How to express power in Python

How to express power in Python

silencement
silencementOriginal
2019-05-15 14:52:5958742browse

Everyone knows that Python can be used to do data calculations, so how to express exponentiation in Python?

How to express power in Python

Look at the following example:

To calculate the results of 4 * 4 and 5 * 5, you can write like this

How to express power in Python

As you can see, the output results are 16 and 25 respectively. If we want to calculate the multiplication of 4*4*4*4, or the multiplication of 2*2*2*2*2*2*2, it is very cumbersome to use the above method. How can we achieve it?

Here we can use exponentiation!

In Python, ** is used to represent exponentiation (there is no space between the two asterisks). With exponentiation, no matter how many identical numbers are multiplied, it can be easily done. solved!

Look at the following example

What is the result of calculating 4*4*4*4

How to express power in Python

As you can see, Python directly The result of 4 raised to the 4th power is given, and the result is 256

Look at another example: Calculate the 30th power of 2

How to express power in Python

Similarly, Python gives accurate results! Compared with the first example, using exponentiation greatly improves calculation efficiency, saves time, and the code is simpler and clearer!

The above is the detailed content of How to express power in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn