Monday, 6 April 2026

๐Ÿš€ Day 13/150 – Simple Calculator in Python

 

๐Ÿš€ Day 13/150 – Simple Calculator in Python

Welcome back to the 150 Days of Python series! ๐Ÿ”ฅ
Today, we’ll build a Simple Calculator — one of the most important beginner projects.

This project helps you understand:

  • Conditional statements
  • Functions
  • User input
  • Error handling

๐ŸŽฏ Problem Statement

Create a Python program that performs basic operations:

  • Addition ➕
  • Subtraction ➖
  • Multiplication ✖️
  • Division ➗

✅ Method 1 – Using if-elif-else

print("Simple Calculator") a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) operation = input("Enter operation (+, -, *, /): ") if operation == "+": print("Result:", a + b) elif operation == "-": print("Result:", a - b) elif operation == "*": print("Result:", a * b) elif operation == "/": if b != 0: print("Result:", a / b) else: print("Division by zero is not allowed") else: print("Invalid operation")


















๐Ÿ” Explanation:
input() → takes user input
float() → converts input into numbers
if-elif-else → checks which operation user selected

๐Ÿ‘‰ Important:
Always check b != 0 before division to avoid errors.

✅ Method 2 – Using Functions

def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): if y != 0: return x / y else: return "Division by zero not allowed" print(add(10, 5)) print(subtract(10, 5)) print(multiply(10, 5)) print(divide(10, 5))









๐Ÿ” Why use functions?

Code becomes modular

Easy to reuse

Cleaner structure

๐Ÿ‘‰ This approach is closer to real-world coding.

✅ Method 3 – Using Lambda Functions

add = lambda x, y: x + y subtract = lambda x, y: x - y multiply = lambda x, y: x * y divide = lambda x, y: x / y if y != 0 else "Cannot divide by zero" print(add(10, 5)) print(subtract(10, 5)) print(multiply(10, 5)) print(divide(10, 5))







๐Ÿ” Explanation:
lambda → creates small anonymous functions
Useful for short, one-line operations

๐Ÿ‘‰ Best for concise code, but not always readable for beginners.

⚠️ Important Things to Remember

✔ Always convert input (int() or float())
✔ Handle division by zero
✔ Validate user input
✔ Keep code clean and readable


๐Ÿง  Summary

MethodConcept
if-elif-elseBasic logic
FunctionsReusability
LambdaShort & compact

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (237) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (3) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (21) data management (15) Data Science (339) Data Strucures (16) Deep Learning (144) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (277) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1283) Python Coding Challenge (1121) Python Mistakes (50) Python Quiz (464) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)