Welcome to Module 5! In the previous modules, you learned Python basics, variables, data types, lists, dictionaries, conditions, loops, and functions.
Now, you are ready to explore something exciting: Python libraries.
Python libraries provide ready-made instructions that help programmers perform different tasks without writing every instruction from the beginning.
A Python library is a collection of ready-made code that helps programmers perform specific tasks.
Imagine you want to build a house. Instead of making every building material yourself, you can buy ready-made bricks, doors, windows, and roofing materials.
Python libraries work in a similar way. They provide tools that make programming easier and faster.
For example, instead of writing hundreds of instructions to calculate the average of thousands of numbers, you can use a Python library that already has tools for calculations.
Why Are Python Libraries Important?
NumPy stands for Numerical Python. It is a Python library used mainly for working with numbers and performing mathematical calculations.
Imagine a teacher has examination scores for 1,000 students. Instead of calculating everything manually, NumPy can help process the numbers quickly.
What Can NumPy Do?
If you are using Google Colab, NumPy is usually already available. You can import it directly.
If you are using a local Python environment, you can install it with this command:
pip install numpy
Before using NumPy, you need to import it into your Python program.
import numpy as np
The letters np are a short name commonly used for NumPy. This makes it easier to write NumPy instructions.
An array is a collection of values stored together. NumPy arrays are useful for performing calculations on groups of numbers.
import numpy as np
scores = np.array([70, 80, 90, 60, 85])
print(scores)
Output:
[70 80 90 60 85]
NumPy can calculate the average of several numbers using the mean() function.
import numpy as np
scores = np.array([70, 80, 90, 60, 85])
average = np.mean(scores)
print(average)
Output:
77.0
The average examination score is 77.
You can use NumPy to find the highest and lowest numbers in a collection.
import numpy as np
scores = np.array([70, 80, 90, 60, 85])
print(np.max(scores))
print(np.min(scores))
Output:
90
60
NumPy has identified 90 as the highest score and 60 as the lowest score.
Pandas is a popular Python library used to organise, clean, and analyse data.
Think about an Excel spreadsheet containing students' names, ages, classes, and examination scores. Pandas allows you to work with this type of information using Python.
Pandas is especially useful when working with information stored in rows and columns.
What Can Pandas Do?
If you are using Google Colab, Pandas is usually already installed.
For a local Python installation, use this command:
pip install pandas
To use Pandas, import it into your program.
import pandas as pd
The letters pd are the commonly used short name for Pandas.
A DataFrame is a table-like structure with rows and columns. It is similar to a spreadsheet in Microsoft Excel.
Example:
import pandas as pd
data = {
"Student": ["Ada", "Tunde", "Musa", "Grace"],
"Score": [85, 70, 90, 65]
}
table = pd.DataFrame(data)
print(table)
Output:
Student Score
0 Ada 85
1 Tunde 70
2 Musa 90
3 Grace 65
Python has created a table containing students' names and examination scores.
You can select one column from a DataFrame by using its column name.
print(table["Student"])
This displays the names of the students.
average_score = table["Score"].mean()
print(average_score)
Output:
77.5
Pandas has calculated the average examination score of the four students.
Matplotlib is a Python library used to create charts and graphs.
Imagine a supermarket owner wants to understand how much money the business makes every day. Looking at a long list of numbers may be difficult.
A chart can make the information easier to understand.
Matplotlib helps turn numbers into visual information.
What Can Matplotlib Do?
Matplotlib is usually available in Google Colab. If you need to install it locally, use:
pip install matplotlib
Use the following instruction to import the plotting tools:
import matplotlib.pyplot as plt
Imagine a shop records the number of products sold during a week.
import matplotlib.pyplot as plt
days = ["Monday", "Tuesday", "Wednesday", "Thursday"]
sales = [10, 15, 8, 20]
plt.bar(days, sales)
plt.title("Weekly Product Sales")
plt.xlabel("Days")
plt.ylabel("Products Sold")
plt.show()
This code creates a bar chart showing how many products were sold on each day.
Understanding the Code:
Before using a Python library, you must make sure it is installed and imported correctly.
Let's look at the basic steps.
Open Google Colab, Jupyter Notebook, or Visual Studio Code.
For example, you can install the libraries using:
pip install numpy pandas matplotlib
In a Google Colab notebook, you can run the installation command with an exclamation mark at the beginning:
!pip install numpy pandas matplotlib
You usually do not need to install these libraries again if they are already available in your environment.
Write the following instructions:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Now, you can use NumPy, Pandas, and Matplotlib in your Python program.
These three libraries can work together to help you analyse information.
Imagine a school wants to analyse the examination scores of five students.
Step 1: Create the Data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
students = ["Ada", "Tunde", "Musa", "Grace", "John"]
scores = np.array([85, 70, 90, 65, 80])
Step 2: Organise the Data with Pandas
data = {
"Student": students,
"Score": scores
}
table = pd.DataFrame(data)
print(table)
Output:
Student Score
0 Ada 85
1 Tunde 70
2 Musa 90
3 Grace 65
4 John 80
Step 3: Calculate the Average with NumPy
average = np.mean(scores)
print("Average Score:", average)
Output:
Average Score: 78.0
Step 4: Create a Chart with Matplotlib
plt.bar(students, scores)
plt.title("Students' Examination Scores")
plt.xlabel("Students")
plt.ylabel("Scores")
plt.show()
The chart displays each student's examination score, making it easier to compare their performance.
In this example:
| Library | Main Purpose | Example Use |
|---|---|---|
| NumPy | Numerical calculations | Calculating average scores. |
| Pandas | Organising and analysing data | Creating student records in a table. |
| Matplotlib | Data visualisation | Creating charts of examination scores. |
In this module, you have learned:
Open Google Colab or another Python environment and complete these activities:
Imagine you are helping a shop owner in Nigeria analyse the number of products sold during four days.
| Day | Products Sold |
|---|---|
| Monday | 15 |
| Tuesday | 20 |
| Wednesday | 12 |
| Thursday | 25 |
Your Task:
Python libraries make data analysis easier. NumPy helps you work with numbers, Pandas helps you organise and analyse information, and Matplotlib helps you present information using charts.
As you continue learning Python, you will use these libraries to work with larger datasets and solve real-world problems.