In scientific and geometric calculations (such as area and volume), you often need to calculate the square, cube, or N-th power of a number.
Python provides the ** operator and the built-in pow() function as standard methods for calculating powers. While they return similar results, they differ in functionality.
This article explains the basic usage of each and the unique “modulo calculation” feature of the pow() function.
1. Calculation Using the ** Operator
In Python, powers are expressed using ** instead of ^ (be careful, as ^ means bitwise XOR). Because it is intuitive and short, this is the most commonly used method for standard calculations.
Specific Usage Example: Area of a Square and Volume of a Cube
Here is an example of calculating the area (squared) and volume (cubed) from the length of a side.
# Length of a side
side_length = 5
# Area = side squared
area = side_length ** 2
# Volume = side cubed
volume = side_length ** 3
print(f"Side: {side_length}")
print(f"Area: {area}")
print(f"Volume: {volume}")
Execution Result:
Side: 5
Area: 25
Volume: 125
Application: Calculating Square Roots
You can also calculate square roots by specifying 0.5 (1/2) as the exponent.
# Find the square root of 100
number = 100
square_root = number ** 0.5
print(f"Square root of {number}: {square_root}")
Execution Result:
Square root of 100: 10.0
2. Calculation Using the pow() Function
You can perform the same calculation using the built-in function pow(base, exp).
Syntax:
result = pow(base, exp)
Specific Usage Example:
Calculating 3 to the power of 4 (3×3×3×3).
base_val = 3
exponent = 4
# 3 to the power of 4
result_pow = pow(base_val, exponent)
print(f"{base_val} to the power of {exponent}: {result_pow}")
Execution Result:
3 to the power of 4: 81
For simple calculations, there is no significant difference between ** and pow().
3. Unique Feature of pow(): Modulo Calculation (Third Argument)
The pow() function has a powerful feature that the ** operator does not have. By specifying a third argument, you can calculate “the remainder of the power result divided by a number” at high speed.
Syntax:
result = pow(base, exp, mod)
# The result is the same as (base ** exp) % mod, but much faster
This is often used in cryptography (such as RSA encryption) and competitive programming when handling very large numbers.
Specific Usage Example:
Here is an example of finding the remainder of a very large calculation result.
x = 123456
y = 789012
z = 1000000007 # Divisor
# Calculate quickly using pow()
# Calculate 123456 to the power of 789012, then find the remainder
result_mod = pow(x, y, z)
print(f"Remainder of huge number: {result_mod}")
Execution Result:
Remainder of huge number: 238711463
If you write this as (x ** y) % z, Python tries to expand a tremendously huge number (x ** y) in memory first, which can take a long time or cause a memory shortage. pow(x, y, z) calculates efficiently without creating the huge number in the process.
Summary
- x ** y: Use for normal power calculations. It is concise and easy to read as a mathematical formula. Can also calculate square roots (0.5 power).
- pow(x, y): Calculates powers in a function format.
- pow(x, y, z): Means the same as
(x ** y) % z, but it is overwhelmingly faster and more memory-efficient for large numbers.
It is recommended to use ** for daily calculations and the third argument of pow() for special calculations requiring remainders.
