Showing posts with label Python Calculator. Show all posts
Showing posts with label Python Calculator. Show all posts

Top 3 Python Script Mode Projects for Beginners – Calculator, Quiz Game & Number Checker

← Back to Home

 

🐍 Python Script Mode Projects for Students – Simple & Fun Assignments

If you're just starting out with Python, learning through small projects is a great way to build your skills and confidence. Below are three beginner-level Python projects you can write and run using Script Mode.

These projects are ideal for:

  • Practice

  • School/college assignments

  • Building confidence with Python programming


📁 What You’ll Learn:

  • How to write a Python script in Script Mode

  • How to save, run, and test .py files

  • Real examples with download links for .py files


🧮 1. Simple Calculator in Python

This script allows users to perform basic operations like addition, subtraction, multiplication, and division.

💡 Features:

  • Menu-based interface

  • Input from user

  • Error handling for division by zero

✅ Sample Code:

# calculator.py

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 "Cannot divide by zero"
    return x / y

print("Select operation:")
print("1. Add\n2. Subtract\n3. Multiply\n4. 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")

🧠 2. Quiz Game in Python

A fun multiple-choice quiz with score tracking.

✅ Sample Code:

# quiz_game.py

score = 0

print("Welcome to the Python Quiz!\n")

answer1 = input("Q1: What is the keyword to define a function in Python? \n(a) def\n(b) function\n(c) fun\nAnswer: ")
if answer1.lower() == "a" or answer1.lower() == "def":
    score += 1

answer2 = input("Q2: What data type is used to store True or False? \n(a) int\n(b) bool\n(c) str\nAnswer: ")
if answer2.lower() == "b" or answer2.lower() == "bool":
    score += 1

answer3 = input("Q3: What symbol is used for comments in Python? \n(a) //\n(b) <!-- -->\n(c) #\nAnswer: ")
if answer3.lower() == "c" or answer3.lower() == "#":
    score += 1

print(f"\nYour final score is {score}/3")

🔍 3. Even or Odd Number Checker

A very basic utility to check if a number is even or odd.

✅ Sample Code:

# number_checker.py

number = int(input("Enter a number: "))

if number % 2 == 0:
    print("The number is EVEN.")
else:
    print("The number is ODD.")


📚 How to Use These Scripts (Script Mode Guide)

  1. create and SAVE the scripts the files.

  2. Open any file (e.g., calculator.py) in a text editor or Python IDE.

  3. Save it if you make changes.

  4. Run the script:

    • Using Terminal:

      python calculator.py
      
    • Using IDLE:
      F5 to run.


🎓 Final Tip for Students

These small Python projects help you:

  • Practice Script Mode usage

  • Understand input/output

  • Write if-else logic

  • Build confidence in Python basics

Once you're comfortable, try adding:

  • More math operations to the calculator

  • Timer or difficulty levels to the quiz

  • Prime number or palindrome checkers in the number tool

🧠 Click Next:  to explore the list of 100 python projects for beginners, intermediate and Expert level!


Build a Simple Python Calculator Using Lambda and Tkinter (Step-by-Step Guide)

← Back to Home

🧮 Build a Calculator in Python Using Lambda & Tkinter

Creating a calculator is a great way to learn about Python functions, GUI programming, and event handling. In this tutorial, we’ll build a simple calculator using lambda functions for operations and Tkinter for the user interface.


📦 What You’ll Learn

  • How to use lambda functions for basic operations

  • How to build a Tkinter GUI

  • How to wire up buttons with actions dynamically


🛠️ Prerequisites

  • Python 3.x installed

  • Basic knowledge of Python functions and Tkinter

You can install Tkinter (if not already installed) using:

pip install tk

Note: Tkinter comes pre-installed with most Python distributions.


🔣 Step-by-Step: Create a Lambda-Based Calculator

✅ Step 1: Import Tkinter

import tkinter as tk
from functools import partial

✅ Step 2: Define Operations Using Lambda

operations = {
    '+': lambda x, y: float(x) + float(y),
    '-': lambda x, y: float(x) - float(y),
    '*': lambda x, y: float(x) * float(y),
    '/': lambda x, y: float(x) / float(y) if float(y) != 0 else "Error"
}

✅ Step 3: Create the GUI Window

root = tk.Tk()
root.title("Lambda Calculator")
root.geometry("300x300")

✅ Step 4: Create Input Fields and Result Display

entry1 = tk.Entry(root, width=10)
entry1.grid(row=0, column=0, padx=10, pady=10)

entry2 = tk.Entry(root, width=10)
entry2.grid(row=0, column=1, padx=10, pady=10)

result_label = tk.Label(root, text="Result: ")
result_label.grid(row=1, column=0, columnspan=2)

✅ Step 5: Define a Generic Operation Handler

def calculate(op):
    try:
        val1 = entry1.get()
        val2 = entry2.get()
        result = operations[op](val1, val2)
        result_label.config(text=f"Result: {result}")
    except Exception as e:
        result_label.config(text="Error")

✅ Step 6: Create Buttons for Operations

row = 2
col = 0

for symbol in operations:
    action = partial(calculate, symbol)
    tk.Button(root, text=symbol, width=5, command=action).grid(row=row, column=col, padx=5, pady=5)
    col += 1

✅ Step 7: Run the Application

root.mainloop()

🖥️ Full Working Code

Here’s the full code for your reference:

import tkinter as tk
from functools import partial

# Define lambda operations
operations = {
    '+': lambda x, y: float(x) + float(y),
    '-': lambda x, y: float(x) - float(y),
    '*': lambda x, y: float(x) * float(y),
    '/': lambda x, y: float(x) / float(y) if float(y) != 0 else "Error"
}

# Create the window
root = tk.Tk()
root.title("Lambda Calculator")
root.geometry("300x300")

# Entry widgets
entry1 = tk.Entry(root, width=10)
entry1.grid(row=0, column=0, padx=10, pady=10)

entry2 = tk.Entry(root, width=10)
entry2.grid(row=0, column=1, padx=10, pady=10)

# Result label
result_label = tk.Label(root, text="Result: ")
result_label.grid(row=1, column=0, columnspan=2)

# Calculation function
def calculate(op):
    try:
        val1 = entry1.get()
        val2 = entry2.get()
        result = operations[op](val1, val2)
        result_label.config(text=f"Result: {result}")
    except Exception:
        result_label.config(text="Error")

# Buttons for operations
row = 2
col = 0
for symbol in operations:
    action = partial(calculate, symbol)
    tk.Button(root, text=symbol, width=5, command=action).grid(row=row, column=col, padx=5, pady=5)
    col += 1

# Run the app
root.mainloop()

📚 Summary

  • Lambda functions let us define concise, anonymous operations.

  • Tkinter helps us build user interfaces with minimal code.

  • This calculator is fully functional and can be expanded to support more features like clearing fields or advanced operations.


Would you like to watch video explanation to create a calculator using python Lambda ? watch video below: 



← Back to Home

Featured Post

Extra Challenge: Using References Between Documents

  🎯 💡 Extra Challenge: Using References Between Documents Here's an  Extra Challenge  designed to build on the original MongoDB mode...

Popular Posts