0% found this document useful (0 votes)
9 views

All_Simple_Python_Programs

The document contains various Python code snippets demonstrating different programming concepts, including a road tax calculator, lambda functions for squares and cubes, a menu-driven calculator with error handling, and functions for calculating area and perimeter. It also includes examples of using classes, mapping lists to dictionaries, and checking for elements in tuples. Each section highlights key programming principles and techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

All_Simple_Python_Programs

The document contains various Python code snippets demonstrating different programming concepts, including a road tax calculator, lambda functions for squares and cubes, a menu-driven calculator with error handling, and functions for calculating area and perimeter. It also includes examples of using classes, mapping lists to dictionaries, and checking for elements in tuples. Each section highlights key programming principles and techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Road Tax Calculator


price = float(input("Enter the cost price of the bike: "))

if price > 100000:


tax = 0.15 * price
elif price > 50000:
tax = 0.10 * price
else:
tax = 0.05 * price

final_price = price + tax

print("Road Tax to be paid: Rs.", tax)


print("Final Price of the bike: Rs.", final_price)

Key Points: We use `if-elif-else` to check the price range and apply tax accordingly. Simple comparison logic.

2. Square and Cube using Lambda


nums = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, nums))
cubes = list(map(lambda x: x**3, nums))

print("Original List:", nums)


print("Squares:", squares)
print("Cubes:", cubes)

Key Points: `lambda` creates tiny functions without using `def`. `map` applies that function to each item of list.
Simple and clean.

3. Menu-Driven Calculator with Error Handling


while True:
print("\nMenu: 1.Add 2.Subtract 3.Multiply 4.Divide 5.Exit")
try:
choice = int(input("Enter choice: "))
if choice == 5:
break
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == 1:
print("Result:", a + b)
elif choice == 2:
print("Result:", a - b)
elif choice == 3:
print("Result:", a * b)
elif choice == 4:
print("Result:", a / b)
else:
print("Invalid choice")
except ZeroDivisionError:
print("Can't divide by zero")
except ValueError:
print("Invalid input")
except Exception as e:
print("Error:", e)

Key Points: `try-except` handles errors like wrong input or divide by zero. Menu runs in loop till user exits.

4. Area and Perimeter using Functions


def area_rectangle(l, b):
return l * b

def perimeter_rectangle(l, b):


return 2 * (l + b)

l = int(input("Enter length: "))


b = int(input("Enter breadth: "))

print("Area:", area_rectangle(l, b))


print("Perimeter:", perimeter_rectangle(l, b))

Key Points: Functions make code reusable. We pass values to calculate area and perimeter separately.

6. Total Marks and Percentage


marks = []
for i in range(5):
m = int(input(f"Enter marks for subject {i+1}: "))
marks.append(m)

total = sum(marks)
percentage = total / 5

print("Total Marks:", total)


print("Percentage:", percentage)

Key Points: We use a loop to collect marks, then `sum()` to get total and calculate percentage easily.

7. Area of Rectangle using Class


class Area:
def setDim(self, l, b):
self.length = l
self.breadth = b

def getArea(self):
return self.length * self.breadth

obj = Area()
l = int(input("Enter length: "))
b = int(input("Enter breadth: "))
obj.setDim(l, b)
print("Area:", obj.getArea())

Key Points: Using class to hold dimensions and return area. Easy OOP example with method calling.
8. Map Two Lists into Dictionary
colors = ['red', 'blue']
codes = ['#ff0000', '#00ff00']

color_dict = dict(zip(colors, codes))


print(color_dict)

Key Points: `zip()` combines two lists. `dict()` turns them into a dictionary directly.

9. Check Element in Tuple


t = (1, 2, 3, 4, 5)
elem = int(input("Enter element to search: "))
print(elem in t)

Key Points: `in` keyword checks if element exists inside tuple. Simple and fast.

You might also like