BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Add Subtract Multiply and Divide two numbers

Last Updated: June 9, 2018 by Chaitanya Singh | Filed Under: Python Examples

In this tutorial, we will write a Python program to add, subtract, multiply and divide two input numbers.

Program to perform addition, subtraction, multiplication and division on two input numbers in Python

In this program, user is asked to input two numbers and the operator (+ for addition, – for subtraction, * for multiplication and / for division). Based on the input, program computes the result and displays it as output.
To understand this program you should know how to get the input from user and the basics of if..elif..else statement.

# Program published on https://beginnersbook.com

# Python program to perform Addition Subtraction Multiplication
# and Division of two numbers

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

print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation +,-,*,/: ")

result = 0
if ch == '+':
    result = num1 + num2
elif ch == '-':
    result = num1 - num2
elif ch == '*':
    result = num1 * num2
elif ch == '/':
    result = num1 / num2
else:
    print("Input character is not recognized!")

print(num1, ch , num2, ":", result)

Output 1: Addition

Enter First Number: 100
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: +
100 + 5 : 105

Output 2: Division

Enter First Number: 20
Enter Second Number: 5
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: /
20 / 5 : 4.0

Output 3: Subtraction

Enter First Number: 8
Enter Second Number: 7
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: -
8 - 7 : 1

Output 4: Multiplication

Enter First Number: 6
Enter Second Number: 8
Enter which operation would you like to perform?
Enter any of these char for specific operation +,-,*,/: *
6 * 8 : 48

Python Program to Add Subtract Multiply and Divide two numbers

Related Python Examples:

1. Python program to add two matrices
2. Python program to add two binary numbers
3. Python program to add two numbers
4. Python program to swap two numbers

Top Related Articles:

  1. Python Program to Print Calendar
  2. Python Program to Convert Celsius To Fahrenheit and Vice Versa
  3. Python Program to Find Sum of n Natural Numbers
  4. Python Program to Check if a Number is Positive Negative or Zero
  5. Getting Input from User in Python

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap