pow(): pow(x,y) returns x raised to the power of y ( input numbers ) .

Here are some examples with different type of numbers.
import math
print(math.pow(2,3))   # 8.0
print(math.pow(2,-3))  # 0.125
print(math.pow(-2,-3)) # -0.125
print(math.pow(-2,3))  # -8.0
Using floats
import math
print(math.pow(2.2,3))   # 10.648000000000003
print(math.pow(2.2,-3))  # 0.09391435011269719
x is negative, and y is not an integer then pow(x, y) is undefined and raise ValueError.
print(math.pow(-2,3.3)) 
print(math.pow(-2,3.3)) 
Above code will generate error
import math
try:
    print(math.pow(-2, 0.5))  
    # Raises ValueError because square root of 
    #a negative number is undefined
except ValueError as e:
    print(e)
Output
math domain error

pow() vs. ** (Exponentiation Operator):

math.pow() always returns a float, while ** can return integers if both inputs are integers.
import math
print(2 ** 3)         # Output: 8 (int)
print(math.pow(2, 3))  # Output: 8.0 (float)

Use Case: Compound Interest Calculation with math.pow()

One common use of math.pow() is in financial calculations like compound interest. The formula is:

import math

# Compound Interest Formula
principal = 1000  # Initial amount
rate = 5 / 100    # Annual interest rate (5%)
time = 10         # Number of years

# Calculating final amount using math.pow()
amount = principal * math.pow(1 + rate, time)
print(f"Final amount: {amount:.2f}")  # Output: Final amount: 1628.89

In this example, the math.pow() function is used to calculate the exponential growth over 10 years.


floor() & ceil() modf() log1p()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer