- Syntax
- Example 1: Square of a Number
- Example 2: Cube of a Number
- Example 3: Any Power
- Example 4: Zero Exponent
- Example 5: Exponentiation with Floating-Point Numbers
- Example 6: Negative Exponent
- Example 7: User Input
- Example 8: Exponentiation in an Expression
- Example 9: Exponentiation with Variables
- Example 10: Checking the Return Type
- Common Mistakes
- Best Practices
- Key Points to Remember
Exponentiation is the mathematical operation of raising one number (called the base) to the power of another number (called the exponent).
In Python, exponentiation is performed using the ** operator.
For example:
2 ** 3means2 ร 2 ร 2, which equals8.5 ** 2means5 ร 5, which equals25.
Exponentiation is commonly used in mathematics, physics, engineering, finance, and scientific computing.
Syntax #
result = base ** exponent
Components #
| Component | Description |
|---|---|
base |
The number to be raised. |
** |
Exponentiation operator. |
exponent |
The power to which the base is raised. |
result |
The computed value. |
Example 1: Square of a Number #
main.py
number = 5
print(number ** 2)
Output #
25
Explanation #
5 ** 2means5 ร 5.- The result is
25.
Example 2: Cube of a Number #
main.py
number = 4
print(number ** 3)
Output #
64
Explanation #
4 ** 3means4 ร 4 ร 4.- The result is
64.
Example 3: Any Power #
main.py
print(3 ** 5)
Output #
243
Explanation #
3is multiplied by itself five times.- Therefore:
3 ร 3 ร 3 ร 3 ร 3 = 243
Example 4: Zero Exponent #
main.py
print(12 ** 0)
Output #
1
Explanation #
- Any non-zero number raised to the power of zero equals
1. - Therefore,
12 ** 0returns1.
Example 5: Exponentiation with Floating-Point Numbers #
main.py
print(2.5 ** 2)
Output #
6.25
Explanation #
- The base is a floating-point number.
2.5 ร 2.5 = 6.25.- The result is also a floating-point number.
Example 6: Negative Exponent #
main.py
print(2 ** -3)
Output #
0.125
Explanation #
- A negative exponent represents the reciprocal.
2 ** -3means1 / (2 ** 3).- Since
2 ** 3is8, the result is1/8, or0.125.
Example 7: User Input #
main.py
base = int(input("Enter the base: "))
power = int(input("Enter the exponent: "))
print(base ** power)
Sample Input #
3
4
Output #
81
Explanation #
- The user enters the base and exponent.
- Python raises the base to the specified power.
3 ** 4equals81.
Example 8: Exponentiation in an Expression #
main.py
result = (2 + 3) ** 2
print(result)
Output #
25
Explanation #
- Parentheses are evaluated first.
2 + 3equals5.- Then
5 ** 2equals25.
Example 9: Exponentiation with Variables #
main.py
length = 7
area = length ** 2
print(area)
Output #
49
Explanation #
- Variables can be used with the exponentiation operator.
- Here, the square of the variable is calculated.
Example 10: Checking the Return Type #
main.py
print(type(5 ** 2))
print(type(2 ** -2))
Output #
<class 'int'>
<class 'float'>
Explanation #
5 ** 2produces an integer because the result is a whole number.2 ** -2produces a floating-point value because it represents a fraction (0.25).
Common Mistakes #
1. Using ^ Instead of ** #
Incorrect
print(2 ^ 3)
Output #
1
Reason
The ^ operator performs a bitwise XOR, not exponentiation.
Correct
print(2 ** 3)
2. Confusing Multiplication with Exponentiation #
Incorrect
print(5 * 2)
Incorrect Expectation #
25
Actual Output #
10
Reason
* performs multiplication.
Use ** for exponentiation.
3. Forgetting Operator Precedence #
Incorrect
print(-2 ** 2)
Output #
-4
Reason
Exponentiation has higher precedence than the unary minus.
Python evaluates:
-(2 ** 2)
Correct
print((-2) ** 2)
Output #
4
4. Expecting the Original Variable to Change #
Incorrect
number = 4
number ** 2
print(number)
Output #
4
Reason
The exponentiation operator returns a new value.
It does not modify the original variable.
Correct
number = number ** 2
print(number)
Best Practices #
- Use
**instead of repeated multiplication when raising numbers to powers. - Use parentheses to make complex expressions easier to read.
- Remember that negative exponents produce floating-point results.
- Do not confuse
**with the bitwise XOR operator (^). - Store the result if it will be used later in the program.
Key Points to Remember #
**is the exponentiation operator in Python.- It raises the base to the specified exponent.
- A power of
0always returns1for any non-zero base. - Negative exponents produce the reciprocal of the positive power.
- Exponentiation works with integers and floating-point numbers.
^is not the exponentiation operator; it performs bitwise XOR.- The exponentiation operator returns a new value and does not modify the original variable.