l✅ Python Assignment – Detailed Solution
Q1. Write a Python program to apply various slicing operations to the list and tuple
Program
Copy code
Python
# List slicing
L1 = ['P','Y','T','H','O','N','P','R','O','G','R','A','M']
print("Original List:", L1)
print("From index 2 to 6:", L1[2:7])
print("First 5 elements:", L1[:5])
print("Last 5 elements:", L1[-5:])
print("Alternate elements:", L1[::2])
print("Reverse list:", L1[::-1])
# Tuple slicing
T1 = tuple(L1)
print("\nOriginal Tuple:", T1)
print("From index 3 to 8:", T1[3:9])
print("Alternate elements:", T1[1::2])
print("Reverse tuple:", T1[::-1])
Output
Copy code
Original List: ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']
From index 2 to 6: ['T', 'H', 'O', 'N', 'P']
First 5 elements: ['P', 'Y', 'T', 'H', 'O']
Last 5 elements: ['G', 'R', 'A', 'M']
Alternate elements: ['P', 'T', 'O', 'P', 'O', 'R', 'M']
Reverse list: ['M', 'A', 'R', 'G', 'O', 'R', 'P', 'N', 'O', 'H', 'T', 'Y', 'P']
Explanation
Slicing is used to extract a part of list or tuple.
Syntax: sequence[start : stop : step]
Negative index slices from the end.
Q2. Write a program to find the length of elements of tuple and list
Program
Copy code
Python
my_list = [10, 20, 30, 40, 50]
my_tuple = ('A', 'B', 'C', 'D')
print("Length of List:", len(my_list))
print("Length of Tuple:", len(my_tuple))
Output
Copy code
Length of List: 5
Length of Tuple: 4
Explanation
len() function returns the number of elements.
Q3. WAP to perform built-in functions on tuple (count, index, add)
Program
Copy code
Python
T = ('A', 'B', 'C', 'D', 'E', 'F', 'A')
print("Tuple:", T)
print("Count of A:", [Link]('A'))
print("Index of D:", [Link]('D'))
# Adding tuples
T2 = ('G', 'H')
T3 = T + T2
print("After adding tuple:", T3)
Output
Copy code
Tuple: ('A', 'B', 'C', 'D', 'E', 'F', 'A')
Count of A: 2
Index of D: 3
After adding tuple: ('A', 'B', 'C', 'D', 'E', 'F', 'A', 'G', 'H')
Explanation
count() → counts element occurrences
index() → returns index position
+ operator is used to add tuples
Q4. Write a Python program to perform add and update function on set
Program
Copy code
Python
Set1 = {10, 20, 30}
print("Original Set:", Set1)
[Link](40)
print("After add():", Set1)
[Link]([50, 60])
print("After update():", Set1)
Output
Copy code
Original Set: {10, 20, 30}
After add(): {10, 20, 30, 40}
After update(): {10, 20, 30, 40, 50, 60}
Explanation
add() → adds single element
update() → adds multiple elements
Q5. Write a Python program to perform union operation on two sets
Program
Copy code
Python
A = {10, 20, 30, 40, 50}
B = {30, 60, 70}
C = [Link](B)
print("Set A:", A)
print("Set B:", B)
print("Union of sets:", C)
Output
Copy code
Union of sets: {10, 20, 30, 40, 50, 60, 70}
Explanation
Union combines elements from both sets
Duplicate values appear only once
Q6. Write a Python program to perform different operations on set
Program
Copy code
Python
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print("Union:", A | B)
print("Intersection:", A & B)
print("Difference:", A - B)
print("Symmetric Difference:", A ^ B)
Output
Copy code
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference: {1, 2}
Symmetric Difference: {1, 2, 5, 6}
Q7. Write a Python program to display elements available in dictionary
Program
Copy code
Python
student = {
"Roll No": 101,
"Name": "Himanshu",
"Course": "Computer Science"
}
for key, value in [Link]():
print(key, ":", value)
Output
Copy code
Roll No : 101
Name : Himanshu
Course : Computer Science
Q8. WAP to use 5 names along with their DOB and access them using DOB
Program
Copy code
Python
dob = {
"01-01-2003": "Rahul",
"05-05-2004": "Amit",
"10-10-2003": "Neha",
"15-08-2002": "Sneha",
"25-12-2004": "Ravi"
}
date = input("Enter DOB (dd-mm-yyyy): ")
print("Name:", [Link](date, "DOB not found"))
Output
Copy code
Enter DOB: 10-10-2003
Name: Neha
Q9. Write a Python program to perform different operations on dictionary using functions
Program
Copy code
Python
d = {"A": 10, "B": 20, "C": 30}
print("Keys:", [Link]())
print("Values:", [Link]())
print("Items:", [Link]())
[Link]({"D": 40})
print("After update:", d)
[Link]("B")
print("After pop:", d)
Output
Copy code
Keys: dict_keys(['A', 'B', 'C'])
Values: dict_values([10, 20, 30])
Items: dict_items([('A', 10), ('B', 20), ('C', 30)])
After update: {'A': 10, 'B': 20, 'C': 30, 'D': 40}
After pop: {'A': 10, 'C': 30, 'D': 40}