BASIC POW() EXAMPLES
Python
#!/usr/bin/env python3
"""
BASIC POW() EXAMPLES
Learn the fundamentals of pow() function
"""
print("=" * 60)
print("BASIC POW() - Exponentiation Fundamentals")
print("=" * 60)
# Example 1: Basic exponentiation
print("\n1. Simple Powers")
print("-" * 40)
print(f"pow(2, 3) = {pow(2, 3)}") # 2^3 = 8
print(f"pow(5, 2) = {pow(5, 2)}") # 5^2 = 25
print(f"pow(10, 4) = {pow(10, 4)}") # 10^4 = 10000
# Example 2: Comparing pow() with ** operator
print("\n2. pow() vs ** Operator")
print("-" * 40)
base = 3
exponent = 4
result_pow = pow(base, exponent)
result_operator = base ** exponent
print(f"Using pow({base}, {exponent}) = {result_pow}")
print(f"Using {base} ** {exponent} = {result_operator}")
print(f"Are they equal? {result_pow == result_operator}")
# Example 3: Powers of different numbers
print("\n3. Powers Table (2^n)")
print("-" * 40)
print("n | 2^n")
print("---|-------")
for n in range(0, 11):
result = pow(2, n)
print(f"{n:2d} | {result:4d}")
# Example 4: Square and cube
print("\n4. Common Operations - Square and Cube")
print("-" * 40)
numbers = [2, 5, 10, 12]
for num in numbers:
square = pow(num, 2)
cube = pow(num, 3)
print(f"Number: {num:2d} | Square: {square:3d} | Cube: {cube:4d}")
# Example 5: Zero and one as exponents
print("\n5. Special Exponents (0 and 1)")
print("-" * 40)
test_numbers = [5, 10, 100, 999]
print("Any number to the power of 0 equals 1:")
for num in test_numbers:
result = pow(num, 0)
print(f" pow({num}, 0) = {result}")
print("\nAny number to the power of 1 equals itself:")
for num in test_numbers:
result = pow(num, 1)
print(f" pow({num}, 1) = {result}")
# Example 6: Calculating area and volume
print("\n6. Practical: Area of Square, Volume of Cube")
print("-" * 40)
side_length = 7
area = pow(side_length, 2)
volume = pow(side_length, 3)
print(f"Side length: {side_length} units")
print(f"Area of square: {area} square units")
print(f"Volume of cube: {volume} cubic units")
# Example 7: Powers of 10 (scientific notation base)
print("\n7. Powers of 10 (Magnitude)")
print("-" * 40)
print("Power | Result | Name")
print("------|------------|------------")
magnitude_names = ["one", "ten", "hundred", "thousand", "ten thousand",
"hundred thousand", "million"]
for power in range(0, 7):
result = pow(10, power)
name = magnitude_names[power]
print(f"10^{power} | {result:10d} | {name}")
# Example 8: Simple interest-like calculation
print("\n8. Practical: Compound Growth")
print("-" * 40)
initial_value = 100
growth_rate = 2 # doubles each period
print(f"Initial value: {initial_value}")
print(f"Growth factor: {growth_rate}x per period\n")
print("Period | Value")
print("-------|-------")
for period in range(0, 6):
value = initial_value * pow(growth_rate, period)
print(f" {period} | {value:5d}")
print("\n" + "=" * 60)
print("Key Concepts:")
print(" - pow(base, exp) calculates base^exp")
print(" - pow(x, 2) gives square, pow(x, 3) gives cube")
print(" - pow(n, 0) always equals 1")
print(" - pow(n, 1) always equals n")
print(" - Equivalent to ** operator for basic cases")
print("=" * 60)