Python
  • Intro.
  • Catalogue
  • Chapter 1: Introduction to Python
  • Chapter 2: Python Syntax and Fundamentals
    • Chapter: Variables and Data Types in Python
  • Chapter 3: Control Flow
  • Chapter 4: Functions
  • Chapter 5: Data Structures
  • Chapter 6: Object-Oriented Programming (OOP)
  • Chapter 7: Modules and Packages
  • Chapter 8: File Handling
  • Chapter 9: Error and Exception Handling
  • Chapter 10: Working with Databases
  • Chapter 11: Iterators and Generators
  • Chapter 12: Decorators and Context Managers
  • Chapter 13: Concurrency and Parallelism
  • Chapter 14: Testing and Debugging
  • Chapter 15: Web Development with Python
  • Chapter 16: Data Science and Machine Learning with Python
  • Chapter 17: Working with APIs
  • Chapter 18: Automation with Python
  • Chapter 19: Python and Cloud/DevOps
  • Chapter 20: Python and IoT
  • Appendices
Powered by GitBook
On this page

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:

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

fruits.remove("banana")
print(fruits)  # Output: ['apple', 'cherry', 'orange']

fruits.sort()
print(fruits)  # Output: ['apple', 'cherry', 'orange']

Tuples

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

Creating a Tuple:

# An empty tuple
empty_tuple = ()

# A tuple with elements
numbers = (1, 2, 3, 4)

# A single-element tuple (note the comma)
single = (5,)

Accessing Elements:

print(numbers[1])  # Output: 2
print(numbers[-1])  # Output: 4

Immutability:

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

numbers[1] = 10  # TypeError: 'tuple' object does not support item assignment

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:

# An empty dictionary
data = {}

# A dictionary with key-value pairs
person = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

Accessing Values:

print(person["name"])  # Output: Alice

Modifying a Dictionary:

person["age"] = 26
print(person)  # Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}

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:

person = {"name": "Bob", "age": 30}
person["city"] = "London"
print(person)  # Output: {'name': 'Bob', 'age': 30, 'city': 'London'}

print(person.keys())  # Output: dict_keys(['name', 'age', 'city'])

Sets

A set is an unordered collection of unique elements.

Creating a Set:

# An empty set
empty_set = set()

# A set with elements
numbers = {1, 2, 3, 4}

Adding and Removing Elements:

numbers.add(5)
print(numbers)  # Output: {1, 2, 3, 4, 5}

numbers.remove(3)
print(numbers)  # Output: {1, 2, 4, 5}

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:

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)  # Union: {1, 2, 3, 4, 5}
print(a & b)  # Intersection: {3}
print(a - b)  # Difference: {1, 2}
print(a ^ b)  # Symmetric Difference: {1, 2, 4, 5}

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:

numbers = [5, 2, 9, 1]
numbers.append(7)
numbers.remove(2)
numbers.sort()
print(numbers)  # Output: [1, 5, 7, 9]

Exercise 2:

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

Solution:

student = {"name": "John", "age": 20, "grade": "B"}
student["grade"] = "A"
print(student)  # Output: {'name': 'John', 'age': 20, 'grade': 'A'}

Exercise 3:

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

Solution:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1 | set2)  # Union: {1, 2, 3, 4, 5}
print(set1 & set2)  # Intersection: {3}
print(set1 - set2)  # Difference: {1, 2}

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

PreviousChapter 4: FunctionsNextChapter 6: Object-Oriented Programming (OOP)

Last updated 5 months ago