Assignment 3: Data Structures in Python
Topic: Perform CRUD Operations on Lists, Tuples, Sets,
and Dictionaries, and Use List Comprehensions
Objective:
To perform Create, Read, Update, and Delete (CRUD) operations on Python data
structures and use list comprehensions for data manipulation
1. Lists
# Create
fruits = ["apple", "banana", "cherry"]
# Read
print(fruits[1])
# Update
fruits[1] = "mango"
# Delete
[Link]("apple")
print(fruits)
Output: ['mango', 'cherry']
2. Tuples
# Create
colors = ("red", "green", "blue")
# Read
print(colors[0])
# Tuples are immutable; use conversion to modify
temp = list(colors)
[Link]("yellow")
colors = tuple(temp)
print(colors)
Output: ('red', 'green', 'blue', 'yellow')
3. Sets
# Create
numbers = {1, 2, 3}
# Read
print(2 in numbers)
# Update
[Link](4)
# Delete
[Link](1)
print(numbers)
Output: {2, 3, 4}
4. Dictionaries
# Create
student = {"name": "Aditya", "age": 21}
# Read
print(student["name"])
# Update
student["age"] = 22
# Delete
del student["name"]
print(student)
Output: {'age': 22}
5. List Comprehensions
nums = [1, 2, 3, 4, 5, 6]
squares = [n**2 for n in nums]
evens = [n for n in nums if n % 2 == 0]
print(squares, evens)
Output: [1, 4, 9, 16, 25, 36] [2, 4, 6]
Conclusion:
CRUD operations enable efficient data manipulation in Python structures.
List comprehensions provide a concise way to filter and transform data.