Write A Python Script Using Function To Calculate XY

Given two integer/ floating point variables The input to the Python python script,  your task is to find the X ^ Y  using a python function.

In order to  calculate X raise to power of Y Python provides a double star operator (**).   In Python if you want to calculate X ^ y you have to use x ** y


Write A Python Script Using Function To Calculate XY






Write A Python Script Using Function To Calculate x^y

Define a function named as calculate()  and pass the values of X and Y. Using the double star operator, calculate the value of x ^ y.  using the return statement, return the value of of X raise to power off y


def calculate(x , y):
   return x ** y



 In the main program, take a variable   X and Y in upper case variables,  ask the user to enter the values of X and Y using the input function call and then convert it into integer.  you can also use the floating point values to convert  the values of X and Y to slot for the demo purpose i will be using integer.  once we get the values of X and Y using the print function call print the  values of X raise to power  of y by calling the function  calculate()


X = int(input("Enter a Value of X : "))
Y = int(input("Enter a Value of Y : "))

print(f"{X} ^ {Y} = {calculate(X, Y)}")




Complete Code
==================

def calculate(x , y):
   return x ** y


X = int(input("Enter a Value of X : "))
Y = int(input("Enter a Value of Y : "))

print(f"{X} ^ {Y} = {calculate(X, Y)}")



Output
=============
Enter a Value of X : 12
Enter a Value of Y : 5
12 ^ 5 = 248832



Output
=============
Enter a Value of X : 3
Enter a Value of Y : 2
3 ^ 2 = 9




Conclusion
==================
Execute the above program by providing the different set of values and also by converting it to floating point values note down the results.


Comment down below  if you have any  suggestions to improve the above Python program




Post a Comment

0 Comments