Chapter 5: Data Structures

Python provides several built-in data structures that make it easy to organize, manipulate, and store data. These include lists, tuples, dictionaries, and sets. Understanding these structures is essential for writing efficient and readable code.

Lists

A list is a mutable, ordered collection of items. Lists can store elements of different data types.

Creating a List:

# An empty list
my_list = []

# A list with integers
numbers = [1, 2, 3, 4, 5]

# A mixed-type list
mixed = [1, "hello", 3.14, True]

Accessing Elements:

Elements in a list can be accessed using indices (starting from 0).

numbers = [10, 20, 30, 40]
print(numbers[0])  # Output: 10
print(numbers[-1])  # Output: 40 (last element)

Modifying Lists:

numbers[1] = 25
print(numbers)  # Output: [10, 25, 30, 40]

List Methods:

Method

Description

append(x)

Adds x to the end of the list.

insert(i, x)

Inserts x at index i.

remove(x)

Removes the first occurrence of x.

pop(i)

Removes and returns the element at index i (default last).

sort()

Sorts the list in ascending order.

reverse()

Reverses the list.

Example:

Tuples

A tuple is an immutable, ordered collection of items. Once created, its elements cannot be changed.

Creating a Tuple:

Accessing Elements:

Immutability:

Tuples cannot be modified after creation. Attempting to do so will raise an error.

Use Cases:

  • Representing fixed collections of items (e.g., coordinates).

  • Using as keys in dictionaries (immutable requirement).

Dictionaries

A dictionary is an unordered collection of key-value pairs. Keys must be unique and immutable.

Creating a Dictionary:

Accessing Values:

Modifying a Dictionary:

Dictionary Methods:

Method

Description

keys()

Returns a view of all keys.

values()

Returns a view of all values.

items()

Returns a view of all key-value pairs.

get(key, default)

Returns the value for key or default.

pop(key)

Removes and returns the value for key.

Example:

Sets

A set is an unordered collection of unique elements.

Creating a Set:

Adding and Removing Elements:

Set Operations:

Operation

Description

Example

Union

Combines elements from both sets.

`a

Intersection

Returns common elements.

a & b

Difference

Returns elements in a but not in b.

a - b

Symmetric Diff

Returns elements in either a or b but not both.

a ^ b

Example:

Summary of Differences

Data Structure

Mutable

Ordered

Allows Duplicates

List

Yes

Yes

Yes

Tuple

No

Yes

Yes

Dictionary

Yes

No

No (keys)

Set

Yes

No

No

Exercises

Exercise 1:

Create a list of numbers and perform the following operations: add a number, remove a number, and sort the list.

Solution:

Exercise 2:

Create a dictionary representing a student (name, age, and grade) and update their grade.

Solution:

Exercise 3:

Create two sets and find their union, intersection, and difference.

Solution:

In the next chapter, we will explore object-oriented programming (OOP) in Python, including classes, objects, and principles like inheritance and encapsulation.

Last updated