Tripti Python Answer
Tripti Python Answer
===============================================
**Introduction**
Modern programming hinges on two complementary ideas: *data structures* that store information
efficiently and *functions* that package behaviour into reusable blocks. In Python, the
most■used composite data structures are **lists** and **dictionaries**. Understanding them,
and knowing how to design clear, well■factored functions around them, is foundational for
writing maintainable software. The following long■answer (suitable for a 15■mark question)
explains each concept in depth and illustrates good...
--------------------------------------------------------------------
1. Lists (≈ 5 marks)
--------------------------------------------------------------------
*Definition*: A **list** is an *ordered, mutable* collection of items written with square
brackets, e.g. `grades = [85, 92, 78]`.
*Key characteristics*
* • **Ordered** – each element has a stable position (index starts at 0).
* • **Mutable** – elements can be added, removed, or replaced in■place.
* • **Heterogeneous** – any mix of types is allowed (`[1, "two", 3.0]`).
* • Backed by a dynamic array → O(1) average■time indexing.
*Common operations*
```python
nums = [10, 20, 30, 40]
nums.append(50) # add at end
nums.insert(2, 25) # add at index
del nums[0] # remove first item
slice = nums[1:4] # [20, 25, 30]
squares = [x**2 for x in nums] # list comprehension
```
--------------------------------------------------------------------
2. Dictionaries (≈ 5 marks)
--------------------------------------------------------------------
*Definition*: A **dictionary** (`dict`) is an *unordered* mapping from **immutable keys** to
**values**, created with curly braces, e.g.
```python
student = {"id": 101, "name": "Aditi", "marks": 87}
```
*Key characteristics*
* • **Association** – look■ups are by *key*, not position.
* • **Hash table implementation** – average O(1) insertion & lookup.
* • Since Python 3.7, the *insertion order is preserved* (helpful when serialising).
* • Keys must be hashable (`str`, `int`, `tuple`, `enum` …), values any type.
*Core operations*
```python
email_book = {"alice":"[email protected]", "bob":"[email protected]"}
email_book["carol"] = "[email protected]" # add / update
addr = email_book.get("dave", "N/A") # safe query
for name, email in email_book.items(): # iteration