Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to Python for Data Analysis ยท Lesson 4 of 9

Module Three

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
4
Module 3: Python Data Structures Introduction to Python for Data Analysis | Beginner-friendly lesson for 13-year-olds In this module, you will learn how Python stores and organises different types of information. These skills are important because data analysts often work with lists of names, examination scores, customer records, product prices, and other information.

Module 3: Python Data Structures

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.

3.1 What Are Python Data Structures?

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:

  • Lists
  • Tuples
  • Dictionaries
  • Sets

3.2 Understanding Python Lists

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:

  • Lists can store many items.
  • Lists use square brackets [ ].
  • Lists can contain numbers, text, or different data types.
  • Lists allow duplicate values.
  • You can add, remove, or change items in a list.

Accessing Items in a List

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:

  • Index 0 means the first item.
  • Index 1 means the second item.
  • Index 2 means the third item.

Adding Items to a List

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

Removing Items from a List

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

Changing Items in a List

You can change an item by using its index number.

fruits = ["Mango", "Orange", "Banana"]

fruits[1] = "Pineapple"

print(fruits)

Output:

['Mango', 'Pineapple', 'Banana']

3.3 Understanding Python Tuples

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:

  • Tuples use round brackets ( ).
  • They can store multiple items.
  • They allow duplicate values.
  • Items can be accessed using index numbers.
  • They cannot be directly modified after creation.

Accessing Items in a Tuple

days = ("Monday", "Tuesday", "Wednesday")

print(days[0])
print(days[2])

Output:

Monday
Wednesday

When Should You Use a Tuple?

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.

3.4 Understanding Python Dictionaries

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:

  • name is a key, while David is its value.
  • age is a key, while 13 is its value.
  • class is a key, while JSS 2 is its value.
  • score is a key, while 85 is its value.

Important Features of Dictionaries:

  • Dictionaries use curly brackets { }.
  • Information is stored as key-value pairs.
  • Each key should be unique.
  • Values can be numbers, text, lists, or other data types.
  • You can add, update, and remove information.

Accessing Information in a Dictionary

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

Adding Information to a Dictionary

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

Updating Information in a Dictionary

You can update an existing value by referring to its key.

student = {
    "name": "David",
    "score": 70
}

student["score"] = 90

print(student["score"])

Output:

90

3.5 Understanding Python Sets

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:

  • Sets use curly brackets { }.
  • They do not keep duplicate values.
  • They are unordered collections.
  • You cannot access set items using index numbers.
  • You can add and remove items.

Adding Items to a Set

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.

Removing Items from a Set

subjects = {"Mathematics", "English", "Science"}

subjects.remove("English")

print(subjects)

The subject English is removed from the set.

3.6 Comparing Lists, Tuples, Dictionaries, and Sets

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.

3.7 Why Data Structures Are Important in Data Analysis

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:

  • Lists: A data analyst can use a list to store examination scores or daily sales figures.
  • Tuples: A tuple can store fixed information such as the days of the week.
  • Dictionaries: A dictionary can store customer details such as names, ages, and locations.
  • Sets: A set can help identify unique customers or remove repeated values from a collection.

Understanding these data structures will make it easier to work with larger datasets using Python libraries such as Pandas.

3.8 Summary of the Module

In this module, you have learned:

  • Data structures are used to organise and store information.
  • Lists store multiple items and can be changed.
  • Tuples store multiple items but cannot be directly modified.
  • Dictionaries organise information using keys and values.
  • Sets store unique items and remove duplicates.
  • Python uses different symbols to create different data structures.
  • Data structures help data analysts organise information before analysing it.

3.9 Practical Exercise

Open Google Colab or another Python environment and complete the following activities:

  1. Create a list containing five Nigerian states.
  2. Display the second and fourth items in your list.
  3. Add another state to your list using append().
  4. Create a tuple containing five school subjects.
  5. Create a dictionary containing your name, age, school, and class.
  6. Create a set containing the numbers 10, 20, 30, 20, and 10. Display the result.
  7. Explain why some numbers disappeared from the set.

Practical Challenge: Organising a School's Student Records

Imagine you are helping a school organise its student information.

The school has the following information:

  • Student name: Grace
  • Age: 13
  • Class: JSS 2
  • Examination score: 88
  • Subjects: Mathematics, English, Computer Studies, and Science

Your Task:

  1. Create a dictionary to store Grace's name, age, class, and examination score.
  2. Create a list containing her four subjects.
  3. Display her name and examination score.
  4. Add another subject to the list.

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.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’