0% found this document useful (0 votes)
9 views22 pages

Data Structures: Lists, Tuples, Dicts, Sets

The document covers key concepts of data structures in Python, focusing on Lists, Tuples, Dictionaries, and Sets, including their mutability and practical applications. It outlines the characteristics of each structure, such as how Lists are ordered and mutable, while Tuples are ordered and immutable. The document also includes hands-on challenges and methods for manipulating these data structures effectively.

Uploaded by

Tezendra Thapa
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)
9 views22 pages

Data Structures: Lists, Tuples, Dicts, Sets

The document covers key concepts of data structures in Python, focusing on Lists, Tuples, Dictionaries, and Sets, including their mutability and practical applications. It outlines the characteristics of each structure, such as how Lists are ordered and mutable, while Tuples are ordered and immutable. The document also includes hands-on challenges and methods for manipulating these data structures effectively.

Uploaded by

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

Data Structures - Collections,

Mutability, and Methods


Topics: Lists, Tuples, Dictionaries, Sets, Mutability, and Practical Methods.
Day 3 Learning Goals

By the end of this session, you will be able to:

1. Differentiate between the four primary collection types: List, Tuple, Dictionary, and Set.
2. Explain the practical difference between Mutable (List, Dict, Set) and Immutable (Tuple).
3. Apply the structures to common data tasks (columns, rows, unique values).
4. Execute essential methods for each structure (e.g., indexing, adding elements).
Mutability

Immutable (Unchangeable)
The object's value cannot be altered in memory. Provides data integrity. - str, int, float, bool
Mutable (Changeable)
The object can be modified (added to, removed from) after creation. - list, dict
Review: The Two Types of Data
1. Scalar Data (Single Values) - The Cells

● What it is: A single piece of data (int, float, str, bool).


● Property: Immutable (Unchangeable).

2. Collection Data (Multiple Values) - The Rows & Columns

● What it is: A structure designed to hold multiple items.


● Property: Most are Mutable (Changeable) - this is necessary for data processing!

🎯 Warm-up Challenge (Hands-On): Mutability Concept

1. Define a variable customer_name = "Amy".


2. Can you directly change the first letter of "Amy" to "S" without using a special method? (Hint: No, strings are Immutable!)
Data Structure 1: Lists ([])

Concept: The Ordered Column of Data

● Definition: An ordered and mutable sequence of items.


● Notation: Defined using square brackets [].
● Use Case: Storing a single column of data, a list of filter values, or a sequence of steps.
List Features
Ordered -
The items have a fixed position.

Mutable -
You can add, remove, or change elements after creation.

Indexing -
Items are accessed by their position, starting at 0.
Demo

monthly_revenue = [45000, 52000, 38000, 61000]

# Access the first month (Index 0)

print(monthly_revenue[0])

# Access the last month using negative indexing

print(monthly_revenue[-1])
List Methods (The Actions of a Column)

List Methods allow us to modify the mutable List object.

.append(item)
Adds a single item to the end of the list.
.remove(item)
Removes the first occurrence of a specified item.
.sort()
Sorts the list items in place (alphabetical or numerical).
HANDS-ON: Data Appending

Scenario: We just got the sales figure for April and need to add it.

monthly_revenue = [45000, 52000, 38000]

# Task: Add the April revenue (55000)

monthly_revenue.append(55000)

print(monthly_revenue) # Output should now have 4 items.


Data Structure 2: Tuples (()) - The Fixed Record

● Definition: An ordered and immutable sequence of items.


● Notation: Defined using parentheses ().
● Use Case: Storing data that should NEVER change (e.g., geographic coordinates, configuration settings).
Tuple Feature

Immutable
Cannot be changed (no .append() or .remove()).
Ordered
Items are accessed by index (0, 1, 2...).
DEMO & HANDS-ON: Tuple Immutability

coordinates = (40.71, -74.00) # (Latitude, Longitude)

# Accessing the first item (Index 0)

print(coordinates[0])

# Task: Try to change a value (this will cause an ERROR)

# [Link](5) # Uncommenting this line causes an error!

# Why Tuple? It guarantees the original coordinates won't be messed up later.


Data Structure 3: Dictionaries ({})

Concept: The Single Row/Record

● Definition: An unordered and mutable collection of Key:Value pairs.


● Notation: Defined using curly braces {}.
● Use Case: Representing a single observation, row, or record in a dataset.
Dictionary Feature

Key:Value
Every entry is made of a unique Key (the column name) and a Value (the cell data).

Access by Key
Values are retrieved using their unique Key (name), not an index number.
DEMO & HANDS-ON: Accessing Data
employee_record = {

'ID': 1001,

'Name': 'Jane Doe',

'Department': 'Marketing',

'Salary': 65000

# Access the Department (like SELECT Department)

print(employee_record['Department'])

# Task: Print the employee's Salary


Dictionary Methods (The Actions of a Row)

Methods allow easy inspection and modification of the record.

['New_Key'] = Value
The simple way to add a new Key:Value pair.
.keys()
Returns a list-like view of all the Keys (column names) in the dictionary.
.values()
Returns a list-like view of all the Values in the dictionary.
HANDS-ON: Adding a Calculated Field

Scenario: We need to add a calculated Bonus field to the employee record.

employee_record = {

'ID': 1001,

'Salary' : 65000

# Task 1: Add a 'Bonus' key (10% of salary)

employee_record[ 'Bonus' ] = employee_record[ 'Salary' ] * 0.10

# Task 2: Print all the keys to see the new field

print(employee_record.keys())
Data Structure 4: Sets ({})

Concept: The Unique List

● Definition: An unordered, mutable collection of unique items.


● Notation: Defined using curly braces {} (but only containing values, not Key:Value pairs).
● Use Case: Finding the number of distinct values in a column.
Set Feature

Unique
Cannot contain duplicate values.

Fast Membership
Very fast at checking if an item exists within the collection.
DEMO & HANDS-ON: Finding Unique Categories

categories = [ "Laptop", "Monitor" , "Monitor" , "Keyboard" , "Laptop", "Mouse"]

# 1. Convert the List to a Set to enforce uniqueness

unique_categories = set(categories)

print(unique_categories)

# Task: Find the total number of unique categories

# Use the len() function (gives the size of the collection)

print(len(unique_categories))
The Full Data Structure Map
Structure Notation Mutability Ordered? Data Role

List [] Mutable Yes (Index 0, 1, 2...) Dynamic Column


(Changeable) (Data that changes)

Tuple () Immutable Yes (Index 0, 1, 2...) Fixed Record


(Unchangeable) (Headers,
Coordinates)

Dictionary { Key: Value } Mutable No (Accessed by Single Row/Record


(Changeable) Key Name) (Access by Column
Name)

Set { Value, Value } Mutable No (Only Unique Distinct Values


(Changeable) Items) (Finding unique
categories)
Day 3 Recap

Today We Mastered:

● The three core Python data structures for collections: Lists, Dictionaries, and Sets.
● Mapping them to data concepts: Lists (Columns), Dictionaries (Rows), Sets (Unique Values).
● Essential Methods for each (e.g., .append(), ['Key'] = Value, set()).
● The difference between Ordered (List) and Unordered (Dict, Set).

You might also like