ScanSkill

Add, Subtract, Multiply, and Divide using User Input in Python

In python, you can add, subtract, multiply and divide two numbers using the arithmetic operator +, -, *, and /respectively. Every operator takes two operands and returns the result of the operation.

The number in python could be of any data type like int, float, or complex. To find the result of the different operations performed, you can take the same data type or combination of any of the supported numeric data types: int, float, or complex.

Here are the examples that perform Addition, Subtraction, Multiplication, and Division of two numbers entered by the user at run-time.

Prerequisites

Example

This program asks the user to enter two numbers and based on those two numbers(int format, converted using int() function) and also the operator(+ for addition, – for subtraction, * for multiplication, and / for division) then the result will be printed for the respective operation.

num1 = int(input("Enter The First Number: "))
num2 = int(input("Enter The Second Number: "))

print("Which operation would you like to perform?")
operator = input("Enter any these operator +,-,*,/ : ")

result = 0
if operator == '+':
    operation = 'Addition'
    result = num1 + num2
elif operator == '-':
    operation = 'Subtraction'
    result = num1 - num2
elif operator == '*':
    operation = 'Multiplication'
    result = num1 * num2
elif operator == '/':
    operation = 'Division'
    result = num1 / num2
else:
    print("Invalid input operatior character!")

print(f"The {operation} of {num1} and {num2} is {result}.")

Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing variables or expressions that will be replaced with its value.

Now run the code and when prompt enter the first number say 20 and press ENTER key, then enter the second number say 10, and press ENTER key and select the operator to perform respective operations. It will print the result on the output.

Addition:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : +
The Addition of 20 and 10 is 30.

Subtraction:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : -
The Subtraction of 20 and 10 is 10.

Multiplication:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : *
The Multiplication of 20 and 10 is 200.

Division:

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : /
The Division of 20 and 10 is 2.0.

When user input is other than the characters which are mentioned, it will print "Invalid input operator character!".

Enter The First Number: 20
Enter The Second Number: 10
Which operation would you like to perform?
Enter any these operator +,-,*,/ : x
Invalid input operatior character!

Conclusion

In this, we demonstrated different examples in Python to perform different arithmetic operations like addition, subtraction, multiplication, and division using user inputs.