Code in Python
Your How-to Guide

Calculator in Python

25th May 2017

This code snippet below will show you how to create calculator in python. Creating a calcuator in python will touch the concepts in python like arthimetic operations in python, for/while loops in python, conditional statements like if-elif-else. Here we will only create a basic calculator that uses if-else and arthimetic operations like addition, subtraction, multiplication, division and mod.

# basic calculator

#taking input for operation

user = float(input("enter first number:\n"))    
user1 = float(input("enter second number:\n"))

print("1. addition\n2. subtraction\n3. multiplication\n4. division\n5. modulus\n\nenter your choice (1,2,3,4,5)")
opt = int(input())


add = user + user1
sub = user - user1
mul = user * user1
div = user / user1
mod = user % user1

#using if-elif-else statement

if opt == 1:
    print("addition = ", add, "\n")
elif opt == 2:
    print("difference = ", sub, "\n")
elif opt == 3:
    print("product = ", mul, "\n")
elif opt == 4:
    print("division = ", div, "\n")
elif opt == 5:
    print("modulus = ", mod, "\n")
    print("please enter a valid choice\n")

input("press enter to exit")

About Author

Akarsh Singh

Developer, Tech. Evangalist, Loves to read and write about new tech. Madly in love with Python. Follow him on social media.

1. For more Awesome ads-free content help me to stay awake late. Buy me a coffee.

2. Want to know where Code in Python website is hosted. Buy cheapest Web hosting (less than 1.5$/month or Rs.99/month) using this referral link. This is trusted and tested web hosting service Code in Python relies on.

backupWeb hosting
3. Support Akarsh Singh(Founder, Code in Python) so that he can teach you more Python and Machine Learning Concepts for free. Contribute to his page and he will mention you in his next post.

4. Indian Users can donate through Paytm.