Python code for calculator: If you’re starting your journey in Python programming, building a calculator is one of the best beginner-friendly projects you can try. It helps you understand basic concepts like variables, functions, loops, and user input. In this article, we’ll walk through how to create a simple calculator using Python, step by step.

This guide is optimized for beginners and also includes improvements you can make to turn your calculator into a more advanced project.
| Article Name | Python Code for Calculator – Build a Simple Calculator Using Python |
| Publish Date | 2/5/2026 |
| What is | Python code for calculator |
| Requirements | Python installed (3.x recommended) Pygame library installed |
| Author | Codeswithsam |
Why Build a Calculator in Python?
Creating a calculator helps you:
- Understand basic arithmetic operations
- Learn how to take user input
- Practice conditional statements
- Improve logical thinking
- Build confidence in coding
Basic Python Calculator Code
# Simple Python Calculator
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Error! Division by zero."
return a / b
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", add(num1, num2))
elif choice == '2':
print("Result:", subtract(num1, num2))
elif choice == '3':
print("Result:", multiply(num1, num2))
elif choice == '4':
print("Result:", divide(num1, num2))
else:
print("Invalid input")

Code Explanation
- Functions: Each operation is defined as a separate function.
- Input Handling: The user enters numbers and selects an operation.
- Conditionals:
if-elifstatements determine which operation to perform. - Error Handling: Division by zero is handled safely.
⚙️ How to Run This Code
Follow these steps to run the Python calculator:
1. Install Python
Download and install Python from the official website.
2. Create a File
Create a file named:
calculator.py
3. Paste the Code
Copy and paste the code into the file.
4. Run the Program
Open terminal or command prompt and run:
python calculator.py
Improved Version (Loop-Based Calculator)
To make your calculator more interactive, you can use a loop so users can perform multiple calculations.
while True:
print("\n--- Python Calculator ---")
print("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit")
choice = input("Choose operation: ")
if choice == '5':
print("Exiting calculator...")
break
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4':
if num2 == 0:
print("Cannot divide by zero!")
else:
print("Result:", num1 / num2)
else:
print("Invalid choice")
Advanced Ideas to Upgrade Your Calculator
Once you understand the basics, try adding these features:
- ✅ Scientific calculations (square root, power, etc.)
- ✅ GUI using Tkinter
- ✅ History of calculations
- ✅ Error handling improvements
- ✅ Keyboard shortcuts
💡 Example: Add Power Operation
def power(a, b):
return a ** b
print("Power Result:", power(2, 3)) # Output: 8
Final Thoughts
Building a calculator in Python is a great starting point for beginners. It helps you understand core programming concepts while giving you a practical project to showcase. Once you master this, you can move on to more advanced applications like GUI-based tools or web apps.
If you’re running a blog like codeswithsam.com, this type of tutorial content is perfect for attracting beginner developers and improving your SEO rankings.
Happy Coding! 🚀
Important Links
| Our Website | Codeswithsam.com |
| Join Telegram | Click Here |
If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning.
Thanks! 🙏 for visiting Codeswithsam.com ! Join telegram (link available in bottom) for source code files , pdf and
Any Promotion queries 👇
info@codeswithsam.com

