PYTHON PRACTICAL FILE
High School Computer Science Practical File
SECTION A: 10 PROGRAMS ON LISTS
#1. Python Program to Find Average of a List
Input:
a = [10, 25, 30, 45, 50]
s=0
for i in a:
s=s+i
avg = s / len(a)
print("List:", a)
print("Average of the list:", avg)
Output:
PS C:\Users\PracticalFile> python -u "list_01.py"
List: [10, 25, 30, 45, 50]
Average of the list: 32.0
#2. Python Program to Print Sum of Negative Numbers, Positive Even & Odd Numbers
in a List
Input:
a = [12, -7, 5, -3, 8, 11, -2, 4]
sn = 0
se = 0
so = 0
for i in a:
if i < 0:
sn = sn + i
elif i % 2 == 0:
se = se + i
else:
so = so + i
print("List:", a)
print("Sum of Negative Numbers:", sn)
print("Sum of Positive Even Numbers:", se)
print("Sum of Positive Odd Numbers:", so)
Output:
PS C:\Users\PracticalFile> python -u "list_02.py"
List: [12, -7, 5, -3, 8, 11, -2, 4]
Sum of Negative Numbers: -12
Sum of Positive Even Numbers: 24
Sum of Positive Odd Numbers: 16
#3. Python Program to Count Occurrences of Element in List
Input:
a = [10, 20, 30, 20, 40, 20, 50]
x = 20
c=0
for i in a:
if i == x:
c=c+1
print("List:", a)
print("Occurrences of 20:", c)
Output:
PS C:\Users\PracticalFile> python -u "list_03.py"
List: [10, 20, 30, 20, 40, 20, 50]
Occurrences of 20: 3
#4. Python Program to Find the Sum of Elements in a List using Recursion
Input:
def fn(a):
if len(a) == 0:
return 0
return a[0] + fn(a[1:])
a = [5, 10, 15, 20]
s = fn(a)
print("List:", a)
print("Sum using recursion:", s)
Output:
PS C:\Users\PracticalFile> python -u "list_04.py"
List: [5, 10, 15, 20]
Sum using recursion: 50
#5. Python Program to Find the Length of a List using Recursion
Input:
def fn(a):
if a == []:
return 0
return 1 + fn(a[1:])
a = [100, 200, 300, 400, 500, 600]
print("List:", a)
print("Length using recursion:", fn(a))
Output:
PS C:\Users\PracticalFile> python -u "list_05.py"
List: [100, 200, 300, 400, 500, 600]
Length using recursion: 6
#6. Python Program to Merge Two Lists and Sort it
Input:
a = [40, 10, 70]
b = [30, 60, 20]
c=a+b
[Link]()
print("List 1:", a)
print("List 2:", b)
print("Merged and Sorted List:", c)
Output:
PS C:\Users\PracticalFile> python -u "list_06.py"
List 1: [40, 10, 70]
List 2: [30, 60, 20]
Merged and Sorted List: [10, 20, 30, 40, 60, 70]
#7. Python Program to Remove Duplicates from a List
Input:
a = [1, 2, 2, 3, 4, 4, 4, 5, 1]
b = []
for i in a:
if i not in b:
[Link](i)
print("Original List:", a)
print("List after removing duplicates:", b)
Output:
PS C:\Users\PracticalFile> python -u "list_07.py"
Original List: [1, 2, 2, 3, 4, 4, 4, 5, 1]
List after removing duplicates: [1, 2, 3, 4, 5]
#8. Python Program to Swap the First and Last Element in a List
Input:
a = [99, 20, 30, 40, 11]
print("Original List:", a)
t = a[0]
a[0] = a[-1]
a[-1] = t
print("List after Swapping:", a)
Output:
PS C:\Users\PracticalFile> python -u "list_08.py"
Original List: [99, 20, 30, 40, 11]
List after Swapping: [11, 20, 30, 40, 99]
#9. Python Program to Sort a List According to the Second Element in Sublist
Input:
a = [['Apple', 50], ['Banana', 20], ['Cherry', 35], ['Date', 10]]
n = len(a)
for i in range(n):
for j in range(0, n - i - 1):
if a[j][1] > a[j + 1][1]:
t = a[j]
a[j] = a[j + 1]
a[j + 1] = t
print("Sorted List by Second Element:", a)
Output:
PS C:\Users\PracticalFile> python -u "list_09.py"
Sorted List by Second Element: [['Date', 10], ['Banana', 20], ['Cherry', 35],
['Apple', 50]]
#10. Python Program to Return the Length of the Longest Word from the List of Words
Input:
a = ["Python", "Computer", "Science", "Programming", "Code"]
m = a[0]
for i in a:
if len(i) > len(m):
m=i
print("List of Words:", a)
print("Longest Word:", m)
print("Length of Longest Word:", len(m))
Output:
PS C:\Users\PracticalFile> python -u "list_10.py"
List of Words: ['Python', 'Computer', 'Science', 'Programming', 'Code']
Longest Word: Programming
Length of Longest Word: 11
SECTION B: 10 PROGRAMS ON TUPLES
#1. Create and display a tuple containing integers, strings, and mixed data types
Input:
t1 = (1, 2, 3, 4)
t2 = ("Python", "Data", "Structures")
t3 = (101, "Alice", 85.5, True)
print("Integer Tuple:", t1)
print("String Tuple:", t2)
print("Mixed Data Tuple:", t3)
Output:
PS C:\Users\PracticalFile> python -u "tuple_01.py"
Integer Tuple: (1, 2, 3, 4)
String Tuple: ('Python', 'Data', 'Structures')
Mixed Data Tuple: (101, 'Alice', 85.5, True)
#2. Input elements from the user and create a tuple, then display the tuple and its
length
Input:
s = input("Enter elements separated by spaces: ")
t = tuple([Link]())
print("Created Tuple:", t)
print("Length of Tuple:", len(t))
Output:
PS C:\Users\PracticalFile> python -u "tuple_02.py"
Enter elements separated by spaces: Red Green Blue Yellow
Created Tuple: ('Red', 'Green', 'Blue', 'Yellow')
Length of Tuple: 4
#3. Find the largest and smallest element in a tuple using max() and min()
Input:
t = (45, 12, 89, 3, 67, 24)
print("Tuple:", t)
print("Largest Element:", max(t))
print("Smallest Element:", min(t))
Output:
PS C:\Users\PracticalFile> python -u "tuple_03.py"
Tuple: (45, 12, 89, 3, 67, 24)
Largest Element: 89
Smallest Element: 3
#4. Calculate the sum and average of all numerical elements of a tuple
Input:
t = (10, 20, 30, 40, 50)
s=0
for i in t:
s=s+i
avg = s / len(t)
print("Numerical Tuple:", t)
print("Sum:", s)
print("Average:", avg)
Output:
PS C:\Users\PracticalFile> python -u "tuple_04.py"
Numerical Tuple: (10, 20, 30, 40, 50)
Sum: 150
Average: 30.0
#5. Search for a given element in a tuple using the membership operator in
Input:
t = (10, 20, 30, 40, 50)
x = 30
print("Tuple:", t)
if x in t:
print("Search Result:", f"Element {x} is present in the tuple.")
else:
print("Search Result:", f"Element {x} is not present in the tuple.")
Output:
PS C:\Users\PracticalFile> python -u "tuple_05.py"
Tuple: (10, 20, 30, 40, 50)
Search Result: Element 30 is present in the tuple.
#6. Count the occurrences of a particular element in a tuple using the count() method
Input:
t = ("red", "blue", "green", "red", "yellow", "red")
x = "red"
print("Tuple:", t)
print("Occurrences of 'red':", [Link](x))
Output:
PS C:\Users\PracticalFile> python -u "tuple_06.py"
Tuple: ('red', 'blue', 'green', 'red', 'yellow', 'red')
Occurrences of 'red': 3
#7. Find the index/position of an element in a tuple using the index() method
Input:
t = ("Mercury", "Venus", "Earth", "Mars")
x = "Earth"
print("Tuple:", t)
print("Index of 'Earth':", [Link](x))
Output:
PS C:\Users\PracticalFile> python -u "tuple_07.py"
Tuple: ('Mercury', 'Venus', 'Earth', 'Mars')
Index of 'Earth': 2
#8. Perform tuple slicing to display selected elements, first/last elements, and the tuple
in reverse order
Input:
t = (10, 20, 30, 40, 50, 60, 70)
print("Original Tuple:", t)
print("First Element:", t[0])
print("Last Element:", t[-1])
print("Slice [2:5]:", t[2:5])
print("Reversed Tuple:", t[::-1])
Output:
PS C:\Users\PracticalFile> python -u "tuple_08.py"
Original Tuple: (10, 20, 30, 40, 50, 60, 70)
First Element: 10
Last Element: 70
Slice [2:5]: (30, 40, 50)
Reversed Tuple: (70, 60, 50, 40, 30, 20, 10)
#9. Convert a list into a tuple and a tuple into a list, and display both results
Input:
a = [1, 2, 3, 4]
t = tuple(a)
t2 = ('a', 'b', 'c')
b = list(t2)
print("Original List:", a)
print("Converted Tuple:", t)
print("Original Tuple:", t2)
print("Converted List:", b)
Output:
PS C:\Users\PracticalFile> python -u "tuple_09.py"
Original List: [1, 2, 3, 4]
Converted Tuple: (1, 2, 3, 4)
Original Tuple: ('a', 'b', 'c')
Converted List: ['a', 'b', 'c']
#10. Separate even and odd numbers from a tuple and store them in two different
tuples
Input:
t = (12, 7, 19, 24, 30, 5, 8, 11)
e = []
o = []
for i in t:
if i % 2 == 0:
[Link](i)
else:
[Link](i)
t1 = tuple(e)
t2 = tuple(o)
print("Original Tuple:", t)
print("Even Numbers Tuple:", t1)
print("Odd Numbers Tuple:", t2)
Output:
PS C:\Users\PracticalFile> python -u "tuple_10.py"
Original Tuple: (12, 7, 19, 24, 30, 5, 8, 11)
Even Numbers Tuple: (12, 24, 30, 8)
Odd Numbers Tuple: (7, 19, 5, 11)
SECTION C: 5 PROGRAMS ON DICTIONARIES
#1. Python Program to Check if a Key Exists in a Dictionary or Not
Input:
d = {"roll_no": 101, "name": "Rahul", "grade": "A"}
k = "grade"
print("Dictionary:", d)
if k in d:
print("Check Result:", f"Key '{k}' exists with value '{d[k]}'")
else:
print("Check Result:", f"Key '{k}' does not exist in dictionary.")
Output:
PS C:\Users\PracticalFile> python -u "dict_01.py"
Dictionary: {'roll_no': 101, 'name': 'Rahul', 'grade': 'A'}
Check Result: Key 'grade' exists with value 'A'
#2. Python Program to Add a Key-Value Pair to the Dictionary
Input:
d = {"apples": 10, "bananas": 5}
print("Original Dictionary:", d)
d["oranges"] = 15
print("Updated Dictionary:", d)
Output:
PS C:\Users\PracticalFile> python -u "dict_02.py"
Original Dictionary: {'apples': 10, 'bananas': 5}
Updated Dictionary: {'apples': 10, 'bananas': 5, 'oranges': 15}
#3. Python Program to Find the Sum of All the Items in a Dictionary
Input:
d = {"Keyboard": 800, "Mouse": 400, "Monitor": 7500}
s=0
for i in d:
s = s + d[i]
print("Dictionary Items:", d)
print("Sum of all values:", s)
Output:
PS C:\Users\PracticalFile> python -u "dict_03.py"
Dictionary Items: {'Keyboard': 800, 'Mouse': 400, 'Monitor': 7500}
Sum of all values: 8700
#4. Python Program to Multiply All the Items in a Dictionary
Input:
d = {'a': 2, 'b': 3, 'c': 4, 'd': 5}
p=1
for i in d:
p = p * d[i]
print("Dictionary:", d)
print("Product of all values:", p)
Output:
PS C:\Users\PracticalFile> python -u "dict_04.py"
Dictionary: {'a': 2, 'b': 3, 'c': 4, 'd': 5}
Product of all values: 120
#5. Python Program to Concatenate Two Dictionaries
Input:
d1 = {'a': 1, 'b': 2}
d2 = {'c': 3, 'd': 4}
d3 = {}
for i in d1:
d3[i] = d1[i]
for i in d2:
d3[i] = d2[i]
print("Dictionary 1:", d1)
print("Dictionary 2:", d2)
print("Concatenated Dictionary:", d3)
Output:
PS C:\Users\PracticalFile> python -u "dict_05.py"
Dictionary 1: {'a': 1, 'b': 2}
Dictionary 2: {'c': 3, 'd': 4}
Concatenated Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}