Practical File Solutions for Python Lab
1. Calculate factorial of a number
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"Factorial of {num} is {factorial}")
2. Check if a number is prime
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number")
break
else:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
3. Sum of elements in a list recursively
def recursive_sum(lst):
if not lst:
return 0
return lst[0] + recursive_sum(lst[1:])
lst = [1, 2, 3, 4, 5]
print(f"Sum of the list: {recursive_sum(lst)}")
4. nth term of Fibonacci series
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
n = int(input("Enter the term number: "))
print(f"The {n}th term in Fibonacci series is {fibonacci(n)}")
5. Numbers divisible by 5
lst = [10, 23, 45, 66, 75]
result = [x for x in lst if x % 5 == 0]
print(f"Numbers divisible by 5: {result}")
6. Create a new list with odd numbers from the first list and even from the second
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
result = [x for x in list1 if x % 2 != 0] + [x for x in list2 if x % 2 == 0]
print(f"New list: {result}")
7. Reverse digits of a number
num = int(input("Enter a number: "))
reversed_num = int(str(num)[::-1])
print(f"Reversed number: {reversed_num}")
8. Multiplication table from 1 to 10
for i in range(1, 11):
for j in range(1, 11):
print(f"{i} x {j} = {i * j}", end="")
print()
9. Downward half-pyramid pattern of stars
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
print('*' * i)
10. Cube of all numbers from 1 to a given number
n = int(input("Enter a number: "))
cubes = [i ** 3 for i in range(1, n + 1)]
print(f"Cubes: {cubes}")
11. Calculate compound interest
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = int(input("Enter time: "))
ci = p * (1 + r / 100) ** t
print(f"Compound Interest: {ci}")
12. Swap two numbers without a temporary variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print(f"Swapped numbers: a = {a}, b = {b}")
13. Convert Celsius to Fahrenheit
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
celsius = float(input("Enter temperature in Celsius: "))
print(f"Temperature in Fahrenheit: {celsius_to_fahrenheit(celsius)}")
14. Remove duplicates from a list
lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = list(set(lst))
print(f"List without duplicates: {unique_lst}")
15. Calculate simple interest
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = int(input("Enter time: "))
si = (p * r * t) / 100
print(f"Simple Interest: {si}")
16. Search any word in a string/sentence
sentence = input("Enter a sentence: ")
word = input("Enter the word to search: ")
if word in sentence:
print(f"'{word}' is found in the sentence")
else:
print(f"'{word}' is not found in the sentence")
17. Find the largest item in a list
lst = [10, 20, 5, 8, 100]
largest = max(lst)
print(f"Largest item in the list: {largest}")
18. Count consonants, vowels, uppercase, and lowercase characters in a file
filename = input("Enter filename: ")
with open(filename, 'r') as file:
text = file.read()
vowels = "aeiouAEIOU"
consonants = 0
uppercase = 0
lowercase = 0
for char in text:
if char.isalpha():
if char in vowels:
pass
else:
consonants += 1
if char.isupper():
uppercase += 1
else:
lowercase += 1
print(f"Consonants: {consonants}, Uppercase: {uppercase}, Lowercase: {lowercase}")
19. Binary file: Store Rollno and Name, search Rollno
import pickle
data = {1: "Alice", 2: "Bob", 3: "Charlie"}
with open("data.pkl", "wb") as file:
pickle.dump(data, file)
rollno = int(input("Enter roll number to search: "))
with open("data.pkl", "rb") as file:
data = pickle.load(file)
print(data.get(rollno, "Rollno not found"))
20. Update marks in binary file
import pickle
data = {1: {"Name": "Alice", "Marks": 85}, 2: {"Name": "Bob", "Marks": 90}}
with open("students.pkl", "wb") as file:
pickle.dump(data, file)
rollno = int(input("Enter roll number to update marks: "))
new_marks = int(input("Enter new marks: "))
with open("students.pkl", "rb+") as file:
data = pickle.load(file)
if rollno in data:
data[rollno]["Marks"] = new_marks
file.seek(0)
pickle.dump(data, file)
print("Marks updated")
else:
print("Rollno not found")