Python Operators Practice: Arithmetic, Logical and Bitwise Problems

In the previous section we discussed the basics of Python Operators. Now let us take a look at some real-life application Problems.

 

1. Online Shopping Cart System

Write a Python Program to build a billing system for an e-commerce store:

  • User buys multiple products.

  • Apply:

    • GST = 18%

    • Discount = 10% if total > ₹5000

  • Check if the user is a Premium Member:

    • If yes then apply extra 5% discount.

  • Display the final payable amount and savings.

				
					laptop_price = 45000
mouse_price = 800
keyboard_price = 1500

laptop_qty = 1
mouse_qty = 2
keyboard_qty = 1

total = (laptop_price * laptop_qty) + (mouse_price * mouse_qty) + (keyboard_price * keyboard_qty)
print("Initial Total:", total)

discount = 0
if total > 5000:
    discount = total * 0.10   

member_type = "Premium"
if member_type == "Premium":
    discount += total * 0.05  

total_after_discount = total - discount

gst = total_after_discount * 0.18
final_amount = total_after_discount + gst

print("Discount Applied:", discount)
print("GST:", gst)
print("Final Amount:", final_amount)

# Initial Total: 48100
# Discount Applied: 7215.0
# GST: 7359.3
# Final Amount: 48244.3

				
			

2. Student Result Analytics System

Write a Python program to create a system that:

  • Accepts marks of 5 subjects.

  • Calculates Total, Average, and Grade using conditions

  • Student passes only if all subjects ≥ 40.

  • Assign grade:

    • Avg ≥ 75 → Distinction

    • Avg ≥ 60 → First Class

    • Avg ≥ 50 → Second Class

    • Else → Fail

				
					marks = [78, 82, 67, 55, 90]
total = sum(marks)
average = total / len(marks)

passed = all(m >= 40 for m in marks)
if passed and average >= 75:
    grade = "Distinction"
elif passed and average >= 60:
    grade = "First Class"
elif passed and average >= 50:
    grade = "Second Class"
else:
    grade = "Fail"

print("Total:", total)
print("Average:", average)
print("Passed:", passed)
print("Grade:", grade)

# Total: 372
# Average: 74.4
# Passed: True
# Grade: First Class

				
			

3. Smart Login & Security System

Write a program for a security login system with the following conditions.

  • Username must exist in the system.

  • Password must match.

  • Account must not be locked.

  • After 3 wrong attempts -> lock account.

				
					stored_username = "admin"
stored_password = "1234"

attempts = 0
account_locked = False

while attempts < 3:
    username = input("Enter Username: ")
    password = input("Enter Password: ")

    # Logical + Comparison Operators
    if username == stored_username and password == stored_password:
        print("Login Successful!")
        break
    else:
        attempts += 1
        print("Wrong Credentials")

# Identity / Assignment Logic
if attempts == 3:
    account_locked = True

print("Account Locked:", account_locked)

# Enter Username: ronnie
# Enter Password: 2345
# Wrong Credentials
# Enter Username: admin
# Enter Password: 1234
# Login Successful!
# Account Locked: False

				
			

4. Inventory Management System

Write a Python program to track warehouse tracks stock with the following conditions:

  • If stock < 10 -> reorder.

  • If stock between 10 and 50 -> normal.

  • If stock > 50 -> overstock (apply 5% storage cost).

				
					
product = "Monitor"
stock = 65
price_per_unit = 7000

if stock < 10:
    status = "Reorder Needed"
elif 10 <= stock <= 50:
    status = "Stock Normal"
else:
    status = "Overstock"

storage_cost = (stock * price_per_unit) * 0.05 if stock > 50 else 0

print("Product:", product)
print("Status:", status)
print("Storage Cost:", storage_cost)

# Product: Monitor
# Status: Overstock
# Storage Cost: 22750.0

				
			

5. Electricity Bill Calculation System

Write a Python program to calculate charges based on units used.

  • First 100 units -> ₹5/unit

  • Next 100 units -> ₹7/unit

  • Above 200 units -> ₹10/unit
    Add 18% tax if bill exceeds ₹2000.

				
					units = 250

if units <= 100:
    bill = units * 5
elif units <= 200:
    bill = (100 * 5) + (units - 100) * 7
else:
    bill = (100 * 5) + (100 * 7) + (units - 200) * 10

tax = bill * 0.18 if bill > 2000 else 0

final_bill = bill + tax

print("Electricity Bill:", bill)
print("Tax:", tax)
print("Final Bill:", final_bill)

# Electricity Bill: 1700
# Tax: 0
# Final Bill: 1700