Python Lab Rcord
Python Lab Rcord
Output:
sql
CopyEdit
Enter start of range: 10
Enter end of range: 30
Prime numbers between 10 and 30:
11 13 17 19 23 29
Total prime numbers: 6
2. Menu-Driven Calculator
python
CopyEdit
def add(a, b): return a + b
def sub(a, b): return a - b
def mul(a, b): return a * b
def div(a, b): return a / b if b != 0 else "Undefined"
while True:
print("\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit")
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:", add(a, b))
elif choice == 2:
print("Result:", sub(a, b))
elif choice == 3:
print("Result:", mul(a, b))
elif choice == 4:
print("Result:", div(a, b))
else:
print("Invalid choice!")
Output (Example):
mathematica
CopyEdit
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice: 1
Enter first number: 5
Enter second number: 3
Result: 8.0
print("\nStudent Report:")
for name, data in students.items():
grade = "A" if data["avg"] >= 75 else "B" if data["avg"] >= 50
else "C"
print(f"{name}: Marks={data['marks']} Avg={data['avg']:.2f}
Grade={grade}")
Output:
yaml
CopyEdit
Enter number of students: 2
Enter student name: Ravi
Enter marks for 3 subjects: 80 70 75
Enter student name: Meena
Enter marks for 3 subjects: 40 50 45
Student Report:
Ravi: Marks=[80, 70, 75] Avg=75.00 Grade=A
Meena: Marks=[40, 50, 45] Avg=45.00 Grade=C
try:
with open(source, "r") as src:
content = src.read()
with open(dest, "w") as dst:
dst.write(content)
print("File copied successfully.")
except FileNotFoundError:
print("Source file not found.")
Output:
mathematica
CopyEdit
Enter source filename: input.txt
Enter destination filename: backup.txt
File copied successfully.
5. Matrix Addition
python
CopyEdit
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))
print("Resultant Matrix:")
for row in result:
print(row)
Output (Example):
yaml
CopyEdit
Enter number of rows: 2
Enter number of columns: 2
Enter elements for first matrix:
1
2
3
4
Enter elements for second matrix:
5
6
7
8
Resultant Matrix:
[6, 8]
[10, 12]
Output:
yaml
CopyEdit
Enter filename: input.txt
Character frequencies:
T: 2
e: 4
s: 2
t: 3
Output:
sql
CopyEdit
Armstrong numbers between 100 and 999:
153 370 371 407
def display(self):
print(f"Account Holder: {self.name} | Balance:
{self.balance}")
Output:
yaml
CopyEdit
Deposited: 500
Withdrawn: 200
Account Holder: Kiran | Balance: 1300
merged = sorted(a + b)
print("Merged Sorted List:", merged)
Output:
yaml
CopyEdit
Enter first sorted list: 1 3 5
Enter second sorted list: 2 4 6
Merged Sorted List: [1, 2, 3, 4, 5, 6]
Output:
markdown
CopyEdit
Enter number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
def display(self):
print(f"Name: {self.name}, Marks: {self.marks}, Total:
{self.total}, Avg: {self.avg:.2f}")
Output:
yaml
CopyEdit
Name: Arun, Marks: [75, 80, 65], Total: 220, Avg: 73.33
Name: Deepa, Marks: [85, 90, 92], Total: 267, Avg: 89.00
Output:
markdown
CopyEdit
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5