C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: We use both the two-star operator and the pow method. We see the results of both syntax forms are equal.
Python program that uses pow, exponents
# Two squared is four.
# ... These syntax forms are identical.
print(2 ** 2)
print(pow(2, 2))
# Two cubed is eight.
print(2 ** 3)
print(pow(2, 3))
Output
4
4
8
8
Here: We take the square of 3 to get 9. And then we divide nine by five, so we have a remainder of 4.
Note: This version of pow may improve performance. But I have never needed this in a real Python program.
Python program that uses pow with 3 arguments
# Compute 3 squared.
# ... This is 9.
# ... Perform 9 modulo 5 to get result of 4.
result = pow(3, 2, 5)
print(result)
Output
4
Quote: The two-argument form pow(x, y) is equivalent to using the power operator.
Built-in Functions: python.org