Python Assignment 5 SUK BCA
Python Assignment 5 SUK BCA
List Operations
Question: Write a Python program to perform basic list
operations like adding, removing, and sorting elements.
numbers = [5, 3, 8, 6, 7]
numbers.append(10)
print("After appending 10:", numbers)
numbers.remove(3)
print("After removing 3:", numbers)
numbers.sort()
print("After sorting:", numbers)
numbers.reverse()
print("After reversing:", numbers)
Expected Output:
2. Tuple Operations
Question: Write a Python program to demonstrate tuple
creation, indexing, and slicing.
my_tuple = ('apple', 'banana', 'cherry', 'date')
print("Element at index 1:", my_tuple[1])
print("Elements from index 1 to 3:", my_tuple[1:3])
print("Length of tuple:", len(my_tuple))
new_tuple = my_tuple + ('elderberry', 'fig')
print("Concatenated tuple:", new_tuple)
Expected Output:
3. Dictionary Operations
Question: Write a Python program to create a dictionary
and demonstrate adding, updating, and removing
elements.
Expected Output:
Expected Output:
Expected Output:
6. Dictionary Methods
Question: Write a Python program to demonstrate various
dictionary methods.
person = {'name': 'Alice', 'age': 30, 'profession': 'Doctor'}
print("Name:", person.get('name'))
print("Keys:", person.keys())
print("Values:", person.values())
print("Items:", person.items())
Expected Output:
Name: Alice
Keys: dict_keys(['name', 'age', 'profession'])
Values: dict_values(['Alice', 30, 'Doctor'])
Items: dict_items([('name', 'Alice'), ('age', 30),
('profession', 'Doctor')])
subjects_scores = {
'Math': [85, 90, 80],
'Science': [78, 88, 92],
'English': [88, 84, 91]
}
9. Nested Dictionary
Question: Write a Python program to create a nested
dictionary and access its elements.
students = {
'John': {'age': 20, 'grade': 'A'},
'Emma': {'age': 22, 'grade': 'B'},
'Liam': {'age': 21, 'grade': 'A'}
}
print("John's age:", students['John']['age'])
print("Emma's grade:", students['Emma']['grade'])
students['Sophia'] = {'age': 23, 'grade': 'A'}
print("Updated dictionary:", students)
Expected Output:
John's age: 20
Emma's grade: B
Updated dictionary: {'John': {'age': 20, 'grade': 'A'},
'Emma': {'age': 22, 'grade': 'B'}, 'Liam': {'age': 21, 'grade':
'A'}, 'Sophia': {'age': 23, 'grade': 'A'}}