0% found this document useful (0 votes)
4 views1 page

Tripti Python Answer

This document provides an in-depth overview of lists and dictionaries in Python, highlighting their definitions, key characteristics, common operations, and typical use cases. Lists are ordered, mutable collections, while dictionaries are unordered mappings from immutable keys to values. Understanding these data structures is essential for writing maintainable software in Python.

Uploaded by

sidhantfi9hai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Tripti Python Answer

This document provides an in-depth overview of lists and dictionaries in Python, highlighting their definitions, key characteristics, common operations, and typical use cases. Lists are ordered, mutable collections, while dictionaries are unordered mappings from immutable keys to values. Understanding these data structures is essential for writing maintainable software in Python.

Uploaded by

sidhantfi9hai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

List, Dictionary, and Function Design in Python

===============================================

**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
```

*Typical use cases*


* • Iteration in order (e.g. processing log lines).
* • Maintaining a stack or queue (with `append`, `pop(0)` or `pop()`).
* • Bulk mathematical transforms (via list comprehensions or `map`).

--------------------------------------------------------------------
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

You might also like