Practical File CS Sample
Practical File CS Sample
1. Write a Python program to input n marks, add them, and display the sum, average, maximum, and
minimum.
Source Code:
total = sum(marks)
average = total / n
maximum_mark = max(marks)
minimum_mark = min(marks)
print(f"Sum: {total}")
print(f"Average: {average:.2f}")
print(f"Maximum: {maximum_mark}")
print(f"Minimum: {minimum_mark}")
OUTPUT:
2. Write a Python program to input n decimal values, multiply all of them, and display the product
rounded to 3 decimal points.
Source Code:
rounded_product = round(product, 3)
print(f"Product: {rounded_product}")
OUTPUT:
3. Write a Python program to input n integers, add 100 to the odd ones while multiplying 3 to the
even ones. Display the final list in descending order.
Source Code:
result_list.sort(reverse=True)
OUTPUT:
4. Write a Python program to input n integers and get the smallest odd number from a list.
Source Code:
if odd_numbers:
smallest_odd = min(odd_numbers)
else:
OUTPUT:
5. Write a Python program to input n names of cities and display the number of cities whose length
is 4 or more.
Source Code:
OUTPUT:
6. Write a Python program to input phone numbers in a list till the user wants and display the phone
numbers starting with 9.
Source Code:
phone_numbers = []
while True:
if number.lower() == 'stop':
break
phone_numbers.append(number)
OUTPUT:
7. Write a Python program to input n integers and remove duplicates from a list.
Source Code:
unique_integers = list(set(integers))
OUTPUT:
8. Write a Python program to input n integers and an upper value. All the values less than the upper
value should get removed from the list.
Source Code:
print("List after removing values less than the upper value:", filtered_integers)
OUTPUT:
9. Write a Python program to input a line of text and count the number of words in it.
Source Code:
word_count = len(text.split())
OUTPUT:
10. Write a Python program to input a line of text and shrink the number of spaces to one between
the words.
Source Code:
OUTPUT:
11. Write a Python program to input a line of text and count the number of words ending with a
vowel in it.
Source Code:
words = text.split()
OUTPUT:
12. Write a Python program to input a line of text and count the number of words that are
palindrome in it.
Source Code:
words = text.split()
OUTPUT:
13. Write a Python program to input a m*n matrix and display it.
Source Code:
matrix = [[int(input(f"Enter element at ({i + 1}, {j + 1}): ")) for j in range(n)] for i in range(m)]
print("Matrix:")
print(row)
OUTPUT:
14. Write a Python program to input a m*n matrix and display the sum of all odd and even values.
Source Code:
matrix = [[int(input(f"Enter element at ({i + 1}, {j + 1}): ")) for j in range(n)] for i in range(m)]
OUTPUT:
15. Write a Python program to input a m*n square matrix and find the sum of all the diagonal
elements.
Source Code:
matrix = [[int(input(f"Enter element at ({i + 1}, {j + 1}): ")) for j in range(n)] for i in range(n)]
OUTPUT:
16. Write a Python program to input a m*n matrix and find the row sums and col sums.
Source Code:
matrix = [[int(input(f"Enter element at ({i + 1}, {j + 1}): ")) for j in range(n)] for i in range(m)]
OUTPUT:
17. Write a Python program to input n students’ roll numbers and names, search for a name given a
roll number.
Source Code:
students = {}
for _ in range(n):
students[roll_number] = name
if search_roll in students:
print(f"Name
OUTPUT:
18. Write a Python program to input n doctors’ names, fees, and gender. Display the number of male
doctors with fees greater than 1000, also the number of female doctors with fees less than 800.
Source Code:
doctors = []
for _ in range(n):
male_doctors_greater_than_1000 = sum(1 for _, fees, gender in doctors if gender == 'M' and fees >
1000)
female_doctors_less_than_800 = sum(1 for _, fees, gender in doctors if gender == 'F' and fees < 800)
OUTPUT:
19. Write a Python program to find the second smallest number in a list.
Source Code:
unique_numbers.sort()
if len(unique_numbers) >= 2:
second_smallest = unique_numbers[1]
else:
OUTPUT:
20. Write a Python program to input a string and find the frequency of the characters in it.
Source Code:
character_frequency = {}
if char in character_frequency:
character_frequency[char] += 1
else:
character_frequency[char] = 1
print("Character frequencies:")
print(f"{char}: {frequency}")
OUTPUT:
21. Write a Python program to input n marks, add them, and display the sum and average.
Source Code:
total = sum(marks)
average = total / n
print(f"Sum: {total}")
print(f"Average: {average:.2f}")
OUTPUT:
22. Write a Python program to input n integers, and find the number of odd and even integers.
Source Code:
OUTPUT:
23. Write a Python program to input n integers, add them, then display the 2nd half followed by the
first half of integers each separated by "@".
Source Code:
total = sum(integers)
first_half = integers[:n // 2]
OUTPUT:
24. Write a Python program to declare a tuple with 10 integers. Display the count of a value entered
by the user.
Source Code:
count = numbers.count(value_to_count)
OUTPUT:
25. Write a Python program to input n names and display the names (reversed) in the opposite input
order.
Source Code:
print(name)
OUTPUT:
26. Write a Python program to create a telephone database to store n records of phone numbers
and names. Input a phone number to display the corresponding name.
Source Code:
telephone_database = {}
for _ in range(n):
telephone_database[phone_number] = name
if search_number in telephone_database:
else:
OUTPUT:
27. Write a Python program to input roll numbers, names, and marks of 3 subjects of n students and
display a performance report.
Source Code:
students = {}
for _ in range(n):
average = sum(marks) / 3
grade = 'A'
grade = 'B'
grade = 'C'
else:
grade = 'FAIL'
print("\nPerformance Report:")
OUTPUT:
28. Write a Python program to input a line of text and find the frequency of the characters in it.
Source code:
character_frequency = {}
if char in character_frequency:
character_frequency[char] += 1
else:
character_frequency[char] = 1
print("Character frequencies:")
print(f"{char}: {frequency}")
OUTPUT:
29. Write a Python program to input a line of text and find the number of capital and small alphabets
in it.
Source code:
text = input("Enter a line of text: ")
if char.isalpha():
if char.isupper():
alphabet_count['capital'] += 1
else:
alphabet_count['small'] += 1
print("Alphabet counts:")
print(f"Capital: {alphabet_count['capital']}")
print(f"Small: {alphabet_count['small']}")
OUTPUT:
30. Write a Python program to the names and total marks of two different classes in two different
dictionaries. Display the topper names along with classes.
Source code:
class_1 = {}
class_2 = {}
for _ in range(n1):
class_1[name] = total_marks
for _ in range(n2):
class_2[name] = total_marks
OUTPUT:
31. Write a Python program for political parties along with the number of members and party
president name. Display the party names with the maximum and minimum party members.
Source code:
parties = {}
for _ in range(n):
# Find the party names with the maximum and minimum members
OUTPUT:
32. Write a Python program to input n grocery items with item id, name, price, and quantity. Update
the price for a given item id.
Source Code:
grocery_items = {}
for _ in range(n):
if update_id in grocery_items:
grocery_items[update_id]['price'] = new_price
else:
OUTPUT:
33. Write a Python menu-driven program to create a set of n country-capital pairs and then search a
capital for a given country and vice versa.
Source Code:
countries_capitals = {}
for _ in range(n):
countries_capitals[country] = capital
# Menu-driven search
while True:
print("\n1. Search capital for a country")
print("3. Exit")
if choice == 1:
if search_country in countries_capitals:
else:
elif choice == 2:
if found_countries:
else:
elif choice == 3:
break
else:
OUTPUT:
34. Write a Python program to input 20 question numbers in integers along with the questions. Ask a
question randomly.
Source Code:
import random
questions = {}
# Input questions
for i in range(20):
random_question_number = random.choice(list(questions.keys()))
OUTPUT:
35. Write a Python program to input 20 question numbers in integers along with the questions and 4
options along with the correct option. Ask 5 questions randomly (without repetition), add 5 for a
correct answer while deduct 2 for an incorrect attempt. Finally, display the total score achieved.
Source Code:
import random
questions = {}
for i in range(20):
correct_option = int(input(f"Enter correct option number (1-4) for question {i + 1}: "))
selected_questions = random.sample(list(questions.keys()), 5)
# Evaluate and display the total score achieved
total_score = 0
if user_answer == questions[question_number]['correct_option']:
total_score += 5
print("Correct! +5 points")
else:
total_score -= 2
OUTPUT: