Welcome to Module 3! In the previous module, you learned about Python variables, data types, and basic mathematical operations.
Now, you will learn about Python data structures. Data structures help you store and organise multiple pieces of information so that you can work with them easily.
Data structures are ways of organising and storing information in a computer program.
Imagine you are a teacher with 30 students. You need to keep their names, ages, and examination scores. Creating a separate variable for every student would take a lot of time.
Python provides data structures that allow you to store many values together and manage them easily.
Four important Python data structures are:
A list is a Python data structure used to store multiple items in one variable.
Think of a shopping list. Instead of writing each item separately, you can put all the items together in one list.
Example:
fruits = ["Mango", "Orange", "Banana", "Apple"]
print(fruits)
Output:
['Mango', 'Orange', 'Banana', 'Apple']
In this example, the variable fruits stores four different fruit names.
Important Features of Lists:
Python uses index numbers to identify the position of items in a list. The first item has an index of 0, not 1.
Example:
fruits = ["Mango", "Orange", "Banana", "Apple"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
Output:
Mango
Orange
Banana
Remember:
You can use the append() method to add a new item to the end of a list.
fruits = ["Mango", "Orange", "Banana"]
fruits.append("Pineapple")
print(fruits)
Output:
['Mango', 'Orange', 'Banana', 'Pineapple']
You can use the remove() method to remove an item from a list.
fruits = ["Mango", "Orange", "Banana"]
fruits.remove("Orange")
print(fruits)
Output:
['Mango', 'Banana']
You can change an item by using its index number.
fruits = ["Mango", "Orange", "Banana"]
fruits[1] = "Pineapple"
print(fruits)
Output:
['Mango', 'Pineapple', 'Banana']
A tuple is another Python data structure used to store multiple items in one variable.
A tuple looks similar to a list, but there is one important difference: you cannot directly change the items inside a tuple after creating it.
Example:
days = ("Monday", "Tuesday", "Wednesday", "Thursday")
print(days)
Output:
('Monday', 'Tuesday', 'Wednesday', 'Thursday')
Important Features of Tuples:
days = ("Monday", "Tuesday", "Wednesday")
print(days[0])
print(days[2])
Output:
Monday
Wednesday
Tuples are useful when you want to keep information together without accidentally changing it.
For example, you can use a tuple to store the months of the year or the days of the week.
A dictionary is a Python data structure that stores information in pairs called keys and values.
Think of a student's school record. The student has a name, age, class, and examination score. Each piece of information has a label and a value.
Example:
student = {
"name": "David",
"age": 13,
"class": "JSS 2",
"score": 85
}
print(student)
Output:
{'name': 'David', 'age': 13, 'class': 'JSS 2', 'score': 85}
In this example:
Important Features of Dictionaries:
You can use a key to access its value.
student = {
"name": "David",
"age": 13,
"score": 85
}
print(student["name"])
print(student["score"])
Output:
David
85
You can add a new key and value to an existing dictionary.
student = {
"name": "David",
"age": 13
}
student["school"] = "Bright Future School"
print(student)
Output:
{'name': 'David', 'age': 13, 'school': 'Bright Future School'}
You can update an existing value by referring to its key.
student = {
"name": "David",
"score": 70
}
student["score"] = 90
print(student["score"])
Output:
90
A set is a Python data structure used to store unique items. This means that a set does not keep duplicate values.
Imagine you have a list of students who registered for a computer class. Some students may accidentally appear more than once. A set can help you remove the repeated names.
Example:
students = {"David", "Grace", "Musa", "David"}
print(students)
Possible Output:
{'David', 'Grace', 'Musa'}
Notice that David appears only once. Sets automatically remove duplicate values.
Important Features of Sets:
subjects = {"Mathematics", "English", "Science"}
subjects.add("Computer Studies")
print(subjects)
The new subject is added to the set. Remember that sets do not guarantee the order in which items appear.
subjects = {"Mathematics", "English", "Science"}
subjects.remove("English")
print(subjects)
The subject English is removed from the set.
Python provides different data structures because different types of information require different ways of organising them.
| Data Structure | Symbols | Main Feature |
|---|---|---|
| List | [ ] | Can be changed and allows duplicates. |
| Tuple | ( ) | Cannot be directly changed and allows duplicates. |
| Dictionary | { } | Stores information as key-value pairs. |
| Set | { } | Stores unique items without duplicates. |
Data analysts work with different types of information every day. Python data structures help them organise and manage this information.
Here are some practical examples:
Understanding these data structures will make it easier to work with larger datasets using Python libraries such as Pandas.
In this module, you have learned:
Open Google Colab or another Python environment and complete the following activities:
Imagine you are helping a school organise its student information.
The school has the following information:
Your Task:
Remember: Lists, tuples, dictionaries, and sets are important tools for organising information in Python. Practise using them because you will need these skills when working with real datasets.