Write A Python Script To Perform The Basic Mathematical Operations With Function

Python program should ask user to enter to numbers and then perform the basic mathematical operation such as addition subtraction multiplication and division between these two numbers so let's try to write a Python program to accept two numbers and perform the basic mathematics operations

Let's accept two numbers in the main program and use the function to perform the basic mathematical operations.

 

Write A Python Script To Perform The Basic Mathematical Operations With Function

 

 


Write A Python Script To Perform The Basic Mathematical Operations With Function

 Let's define a function named as basic_operation(),  pass the interior values a and b as the input to the Python function,  now using the print function call print addition, subtraction and multiplication of these two numbers.

Now in order to perform the division you should use try and except block,  because the denominator may be 0,  so in order to avoid zero division error uses that try and  expect block to avoid the zero division error.


def basic_operation(a, b):
   print(f"Basic Mathematical Operations on Two Numbers {a} and {b}")
   print("Addition of Two Numbers is ", a + b)
   print("Subtraction of Two Numbers is ", a - b)
   print("Product of Two Numbers is ", a * b)
   try:
       print("Division of Two Numbers is ", a / b)
   except ZeroDivisionError:
       print("Zero Division is Not Possible !")






In the main program, take variables first and second use the input function call to ask the user to enter to integer values  using input function call and then convert it into an integer.

Once we get to integer values as the input to the Python program call the function name basic_operation() and pass 2 integer values as the input to the Python function

first = int(input("Enter First Number : "))
second = int(input("Enter First Number : "))
basic_operation(first, second)





Write A Python Script To Perform The Basic Mathematical Operations With Function
===============================================================

def basic_operation(a, b):
   print(f"Basic Mathematical Operations on Two Numbers {a} and {b}")
   print("Addition of Two Numbers is ", a + b)
   print("Subtraction of Two Numbers is ", a - b)
   print("Product of Two Numbers is ", a * b)
   try:
       print("Division of Two Numbers is ", a / b)
   except ZeroDivisionError:
       print("Zero Division is Not Possible !")


first = int(input("Enter First Number : "))
second = int(input("Enter First Number : "))
basic_operation(first, second)





Output
==============

Enter First Number : 55
Enter First Number : 11
Basic Mathematical Operations on Two Numbers 55 and 11
Addition of Two Numbers is  66
Subtraction of Two Numbers is  44
Product of Two Numbers is  605
Division of Two Numbers is  5.0






Output
===============

Enter First Number : 21
Enter First Number : 0
Basic Mathematical Operations on Two Numbers 21 and 0
Addition of Two Numbers is  21
Subtraction of Two Numbers is  21
Product of Two Numbers is  0
Zero Division is Not Possible !





Output
================

Enter First Number : 99
Enter First Number : 3
Basic Mathematical Operations on Two Numbers 99 and 3
Addition of Two Numbers is  102
Subtraction of Two Numbers is  96
Product of Two Numbers is  297
Division of Two Numbers is  33.0






Conclusion
==========================

Execute the above python script by providing input integers as the input to the Python program and perform the basic mathematical operations among them.

Comment it down below if you have any suggestions to improve the above python script.





Post a Comment

0 Comments