Program 1
Source Code
Write a Python program to calculate the average
fuel consumption (km/l) given fuel in litres and
distance in km.
fuel = float(input("Enter fuel in litres: "))
distance = float(input("Enter distance in km: "))
average = distance / fuel
print("Average:", round(average, 2), "km/l")
Output
Enter fuel in litres: 5
Enter distance in km: 150
Average: 30.0 km/l
Program 2
Source Code
Write a program to compute the area and
circumference of a circle given the radius.
import math
r = float(input("Enter radius: "))
area = math.pi * r**2
circum = 2 * math.pi * r
print("Area:", round(area, 2))
print("Circumference:", round(circum, 2))
Output
Enter radius: 7
Area: 153.94
Circumference: 43.98
Program 3
Source Code
Write a Python program to determine if a number
is even or odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Output
Enter a number: 4
Even
Program 4
Source Code
Write a program to find the greatest of three
numbers entered by the user.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Greatest:", a)
elif b >= c:
print("Greatest:", b)
else:
print("Greatest:", c)
Output
Enter first number: 12
Enter second number: 5
Enter third number: 20
Greatest: 20
Program 5
Source Code
n = int(input("Enter how many numbers: "))
total = 0
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
total += num
print("Sum =", total)
Output
Enter how many numbers: 4
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Sum = 10
Program 6
Source Code
n = int(input("How many characters? "))
a=d=s=0
for i in range(n):
ch = input("Enter character: ")
if ch.isalpha():
print("Alpha")
a += 1
elif ch.isdigit():
print("Digit")
d += 1
else:
print("Special")
s += 1
print("Alphabets:", a, "Digits:", d, "Special
Characters:", s)
Output
How many characters? 4
Enter character: A
Alpha
Enter character: 4
Digit
Enter character: $
Special
Enter character: b
Alpha
Alphabets: 2 Digits: 1 Special Characters: 1
Program 7
Source Code
num = int(input("Enter number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
Output
Enter number: 5
Factorial: 120
Program 8
Source Code
x = int(input("Enter base: "))
y = int(input("Enter exponent: "))
res = 1
for _ in range(y):
res *= x
print("Result:", res)
Output
Enter base: 2
Enter exponent: 5
Result: 32
Program 9
Source Code
n = int(input("Enter number of students: "))
grade_counts = {'A':0, 'B':0, 'C':0, 'D':0, 'E':0}
for i in range(n):
marks = int(input(f"Enter marks for student
{i+1}: "))
if marks > 90:
grade = 'A'
elif marks > 70:
grade = 'B'
elif marks > 50:
grade = 'C'
elif marks > 32:
grade = 'D'
else:
grade = 'E'
grade_counts[grade] += 1
print(f"Student {i+1} Grade: {grade}")
print("Grade counts:", grade_counts)
Output
Enter number of students: 3
Enter marks for student 1: 95
Student 1 Grade: A
Enter marks for student 2: 60
Student 2 Grade: C
Enter marks for student 3: 20
Student 3 Grade: E
Grade counts: {'A': 1, 'B': 0, 'C': 1, 'D': 0, 'E': 1}
Program 10
Source Code
num = int(input("Enter number: "))
if num < 2:
print("Neither Prime nor Composite")
else:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
print("Composite")
break
else:
print("Prime")
Output
Enter number: 7
Prime
Program 11
Source Code
n = int(input("Enter first number: "))
m = int(input("Enter second number: "))
print("Common factors are:")
for i in range(1, min(n,m)+1):
if n%i==0 and m%i==0:
print(i)
Output
Enter first number: 12
Enter second number: 18
Common factors are:
1
2
3
6
Program 12
Source Code
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
max_val = max(a, b)
while True:
if max_val % a == 0 and max_val % b == 0:
print("LCM:", max_val)
break
max_val += 1
Output
Enter first number: 4
Enter second number: 6
LCM: 12
Program 13
Source Code
def create_file():
with open("friends.txt", "w") as f:
for i in range(5):
name = input(f"Enter name of friend {i+1}:
")
f.write(name + "\n")
create_file()
Output
Enter name of friend 1: Raj
Enter name of friend 2: Simran
Enter name of friend 3: Aman
Enter name of friend 4: Tina
Enter name of friend 5: Ravi
Program 14
Source Code
def display_file(fname):
try:
with open(fname, "r") as f:
print(f.read())
except FileNotFoundError:
print("File not found.")
display_file("friends.txt")
Output
Raj
Simran
Aman
Tina
Ravi
Program 15
Source Code
def display_n_chars(n):
with open("friends.txt", "r") as f:
print(f.read(n))
display_n_chars(int(input("Enter number of
characters to display: ")))
Output
Enter number of characters to display: 10
Raj
Simr
Program 16
Source Code
def create_students():
with open("students.txt", "w") as f:
while True:
name = input("Enter student name (or STOP
to end): ")
if name.lower() == "stop":
break
f.write(name + "\n")
create_students()
Output
Enter student name (or STOP to end): Ravi
Enter student name (or STOP to end): Tanya
Enter student name (or STOP to end): STOP
Program 17
Source Code
def add_students():
with open("students.txt", "a") as f:
while True:
name = input("Enter student name to add
(or STOP to end): ")
if name.lower() == "stop":
break
f.write(name + "\n")
add_students()
Output
Enter student name to add (or STOP to end): Anjali
Enter student name to add (or STOP to end): STOP
Program 18
Source Code
def count_chars():
with open("students.txt", "r") as f:
data = f.read()
lower = sum(1 for c in data if
c.islower())
upper = sum(1 for c in data if
c.isupper())
digits = sum(1 for c in data if
c.isdigit())
spaces = data.count(' ')
print("Lowercase:", lower, "Uppercase:",
upper, "Digits:", digits, "Spaces:", spaces)
count_chars()
Output
Spaces: 2 Digits: 0 Uppercase: 6 Lowercase: 24
Program 19
Source Code
def count_vowels():
with open("students.txt", "r") as f:
text = f.read().lower()
count = sum(text.count(v) for v in
'aeiou')
print("Vowel count:", count)
count_vowels()
Output
Vowel count: 12
Program 20
Source Code
def copy():
with open("story.txt", "r") as f:
data = f.read()
with open("alpha.txt", "w") as a,
open("digit.txt", "w") as d, open("other.txt", "w") as
o:
for ch in data:
if ch.isalpha():
a.write(ch)
elif ch.isdigit():
d.write(ch)
else:
o.write(ch)
copy()
Output
(Alphabetic characters copied to alpha.txt, digits to
digit.txt, others to other.txt)
Program 21
Source Code
def count_is():
with open("students.txt", "r") as f:
words = f.read().split()
print('"is" count:', words.count("is"))
count_is()
Output
"is" count: 2
Program 22
Source Code
def count_four_letter_words():
with open("students.txt", "r") as f:
words = f.read().split()
count = sum(1 for word in words if len(word)
== 4)
print("4-letter words:", count)
count_four_letter_words()
Output
4-letter words: 3
Program 23
Source Code
def display_star_words():
with open("xyz.txt", "r") as f:
words = f.read().split()
for word in words:
print(word + "*", end=" ")
display_star_words()
Output
This* is* a* sample* file* with* words*
Program 24
Source Code
def display():
with open("story.txt", "r") as f:
for line in f:
for word in line.split():
if word.startswith("I"):
print(word[::-1])
display()
Output
s'tI
stnemom
nwonk
Program 25
Source Code
def countuser():
search = input("Enter word to search: ")
count = 0
with open("story.txt", "r") as f:
for word in f.read().split():
if search.lower() in
word.lower():
count += 1
print("Count:", count)
countuser()
Output
Enter word to search: love
Count: 3
Program 26
Source Code
def reverse_lines():
with open("xyz.txt", "r") as f:
for line in f:
print(line.strip()[::-1])
reverse_lines()
Output
.yrts tsrif eht si sihT
.yrts tsal eht si sihT
Program 27
Source Code
def end_with_y():
with open("xyz.txt", "r") as f:
for line in f:
if line.strip().endswith(('y', 'Y')):
print(line.strip())
end_with_y()
Output
This story is very happy
They play joyfully
Program 28
Source Code
def countline():
with open("article.txt", "r") as f:
count = 0
for _ in f:
count += 1
print("Number of lines in the file:",
count)
countline()
Output
Number of lines in the file: 4
Program 29
Source Code
def display_from_6():
with open("friends.txt", "r") as f:
text = f.read()
print(text[5:10])
display_from_6()
Output
ds.na
Program 30
Source Code
def replace_a_b():
with open("friends.txt", "r") as f:
data = f.read()
data = data.replace('a', 'b')
with open("friends.txt", "w") as f:
f.write(data)
print("Replaced all 'a' with 'b'")
replace_a_b()
Output
Replaced all 'a' with 'b'
Program 31
Source Code
def swap_case():
with open("xyz.txt", "r") as f:
data = f.read()
data = data.swapcase()
with open("xyz.txt", "w") as f:
f.write(data)
print("Swapped lowercase to uppercase
and vice versa")
swap_case()
Output
Swapped lowercase to uppercase and vice versa
Program 32
Source Code
def alternate_lines():
with open("xyz.txt", "r") as f:
lines = f.readlines()
for i in range(0, len(lines), 2):
print(lines[i].strip())
alternate_lines()
Output
Line 1 text
Line 3 text