Document With Solutions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Date: January 2, 2025

Subject: Submission of Practical File COMPUTER


SCIENCE WITH PYTHON PRACTICAL FILE QUESTIONS:
Flow of Control Conditional Statements:

1. Write a Python program that takes a number as input and checks if it is


positive, negative, or zero.

number = int(input("Number for checking: "))

if number == 0:
print(number, "is zero")
elif number < 0:
print(number, "is negative")
elif number > 0:
print(number, "is positive")

2. Create a program that asks for a student's marks and determines the
grade based on the following criteria:
90 and above: A
80 to 89: B
70 to 79: C
60 to 69: D
Below 60: F

grade = int(input("Grade: "))

if grade >= 90:


print("Grade is A")
elif grade >= 80:
print("Grade is B")
elif grade >= 70:
print("Grade is C")
elif grade >= 60:
print("Grade is D")
elif grade < 50:
print("Grade is F")
Loops:
1. Write a program to print the Fibonacci series up to a given number.

number = int(input("Number for Fibonacci series: "))


a = 1

while a < number:


print(a)
a = a + a

2. Create a program that calculates the factorial of a given number


using a while loop.

number = int(input("Number for factorial: "))


a = number

while a > 1:
a -= 1
number = a*number

print(number)

String Manipulation
1. Write a Python program that takes a string input and prints it in reverse
order.

string = str(input("String for reversal: "))

reversed = string[::-1]
print(reversed)
2.Create a program that counts the number of vowels in a given string.

string = str(input("String for vowel count: "))


vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
count = 0
for a in string:
if a in vowels:
count += 1

print(count)

3. Write a program that checks if a given string is a palindrome.

string = str(input("String for palindrome checking: "))


string.capitalize
reversed = string[::-1]
if string == reversed:
print(string, "is a palindrome")
else:
print(string, "is not a palindrome")

4.Create a program that finds the longest word in a given sentence.

string = str(input("String for finding longest word: "))


words = string.split()
longest = ""

for word in words:


if len(word) > len(longest):
longest = word

print(longest, "is longest")


List Manipulation
5. Write a Python program to find the largest and smallest elements in a list.
def find_largest_smallest(lst):
largest = max(lst)
smallest = min(lst)
return largest, smallest

lst = [int(x) for x in input("Enter a list of numbers:


").split()]
largest, smallest = find_largest_smallest(lst)
print(f"Largest: {largest}, Smallest: {smallest}")

6. Create a program that takes a list of numbers and returns a new list with
each element squared.
def square_elements(lst):
return [x**2 for x in lst]

lst = [int(x) for x in input("Enter a list of numbers:


").split()]
squared_lst = square_elements(lst)
print("Squared list:", squared_lst)

7. Write a program that merges two lists into a single list, removing any
duplicates.
def merge_lists(lst1, lst2):
return list(set(lst1 + lst2))

lst1 = [int(x) for x in input("Enter first list of


numbers: ").split()]
lst2 = [int(x) for x in input("Enter second list of
numbers: ").split()]
merged_lst = merge_lists(lst1, lst2)
print("Merged list without duplicates:", merged_lst)

8. Create a program that sorts a list of strings based on their lengths.


def sort_by_length(lst):
return sorted(lst, key=len)

lst = input("Enter a list of strings (comma-separated):


").split(", ")
sorted_lst = sort_by_length(lst)
print("Sorted list by length:", sorted_lst)

Tuple Manipulation
9. Write a Python program to create a tuple and print its elements.
tup = (1, 2, 3, 4, 5)
for element in tup:
print(element)

10. Create a program that finds the index of a given element in a tuple.
tup = (10, 20, 30, 40, 50)
element = int(input("Enter the element to find its
index: "))
if element in tup:
print(f"Index of {element}: {tup.index(element)}")
else:
print(f"{element} not found in the tuple.")

11. Write a program to convert a list into a tuple and vice versa.
lst = [1, 2, 3, 4, 5]
tup = tuple(lst)
print("Tuple:", tup)

new_lst = list(tup)
print("List:", new_lst)

12. Create a program that joins two tuples and sorts the resulting tuple.
tup1 = (1, 3, 5)
tup2 = (2, 4, 6)
merged_tup = tuple(sorted(tup1 + tup2))
print("Merged and sorted tuple:", merged_tup)

Dictionary Manipulation
13. Write a Python program to create a dictionary and print its keys and values.
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")
14. Create a program that updates the value of a specific key in a dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
key = input("Enter the key to update: ")
new_value = int(input("Enter the new value: "))
if key in my_dict:
my_dict[key] = new_value
print(f"Updated dictionary: {my_dict}")
else:
print(f"Key '{key}' not found.")

15. Write a program that merges two dictionaries.


dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = {**dict1, **dict2}
print("Merged dictionary:", merged_dict)

16. Create a program that finds the key with the maximum value in a
dictionary.
my_dict = {'a': 1, 'b': 3, 'c': 2}
max_key = max(my_dict, key=my_dict.get)
print(f"Key with the maximum value: {max_key}")

Sol. By Aseem and GPT (kindly, don’t


sue me!)

You might also like