Course: Basic Data Transformation
Module: 5 of 8
Level: Beginner
Imagine you are a teacher in a secondary school in Ibadan. You have a spreadsheet containing students' examination scores.
Some scores are written as numbers, such as 85, while others are stored as text, such as "85".
Although they look similar, a computer may treat them differently. The number 85 can be used in calculations, but the text "85" may need to be converted before it can be used in mathematical operations.
This is why data conversion and formatting are important.
Data conversion means changing data from one type or format into another.
| Original Data | Converted Data |
|---|---|
| "25" (Text) | 25 (Number) |
| "2026-11-24" | 24 November 2026 |
| 0.75 | 75% (display format) |
| 5000 | "5000" (Text) |
Text contains letters, words, or characters.
"CRMNuggets"
"John"
"Computer Science"
In Python, text is called a string.
Integers are whole numbers without decimal points.
10
25
100
1500
These are numbers that contain decimal points.
12.5
45.75
99.9
Boolean values represent two possibilities: True or False.
True
False
For example:
Sometimes numbers are stored as text. Python provides functions that help us convert them.
The int() function converts suitable values into whole numbers.
age = "13"
new_age = int(age)
print(new_age)
print(type(new_age))
Output:
13
<class 'int'>
The value has changed from text into an integer.
The float() function converts suitable values into decimal numbers.
price = "2500.50"
new_price = float(price)
print(new_price)
Output:
2500.5
Sometimes you need to convert a number into text. Python uses the str() function.
score = 85
message = "Your score is " + str(score)
print(message)
Output:
Your score is 85
This is useful when you want to combine numbers with messages. For example, a learning platform can display:
You earned 500 XP points.
Python uses the bool() function to convert values into Boolean values.
payment = 1
result = bool(payment)
print(result)
Output:
True
Another example:
payment = 0
result = bool(payment)
print(result)
Output:
False
Generally, zero and empty values are treated as False, while most non-empty values are treated as True.
Data formatting means changing how information is displayed without necessarily changing the original meaning or underlying value.
For example, the number 2500 can be displayed in different ways:
2500
2,500
โฆ2,500.00
They can represent the same amount of money, but their presentation is different.
Instead of displaying:
5000
You can display:
โฆ5,000.00
This makes it clear that the value represents Nigerian Naira.
The same date can appear in different formats:
2026-11-24
24/11/2026
24 November 2026
A decimal value such as 0.75 can be displayed as 75%.
A value such as 45.67891 can be displayed with two decimal places:
45.68
You can convert and format data without writing Python code.
| Student | Amount Paid |
|---|---|
| Tunde | 5000 |
| Amina | 3500 |
| David | 7000 |
To format the amounts as Nigerian currency:
Your spreadsheet can now display:
| Student | Amount Paid |
|---|---|
| Tunde | โฆ5,000.00 |
| Amina | โฆ3,500.00 |
| David | โฆ7,000.00 |
If your spreadsheet contains 2026-11-24, you can change its display format to 24 November 2026.
A teacher has collected examination scores, but the scores are stored as text. Your task is to convert them into numbers and calculate the average.
scores = ["70", "85", "90", "65", "80"]
# Convert text scores into numbers
converted_scores = []
for score in scores:
converted_scores.append(int(score))
# Calculate the total
total = sum(converted_scores)
# Calculate the average
average = total / len(converted_scores)
print("Converted Scores:", converted_scores)
print("Total Score:", total)
print("Average Score:", average)
Expected Output:
Converted Scores: [70, 85, 90, 65, 80]
Total Score: 390
Average Score: 78.0
Imagine you are helping a school administrator organise payment records. The payment amounts are stored as text.
| Student | Payment |
|---|---|
| Tunde | "5000" |
| Amina | "3500" |
| David | "7000" |
| Grace | "4500" |
Suggested Python Code:
payments = ["5000", "3500", "7000", "4500"]
converted_payments = []
for payment in payments:
converted_payments.append(int(payment))
total = sum(converted_payments)
average = total / len(converted_payments)
print("Total School Payment:", total)
print("Average Payment:", average)
Expected Output:
Total School Payment: 20000
Average Payment: 5000.0
You cannot directly convert "Good" into an integer.
Displaying 5000 as โฆ5,000.00 does not necessarily change its underlying numeric value.
Using int() on a decimal number removes the decimal part. For example, int(12.8) becomes 12.
Always check whether your data contains unexpected values before converting it.
| Term | Meaning |
|---|---|
| Data Conversion | Changing data from one type or format to another. |
| Data Formatting | Changing how data is displayed. |
| String | Text data. |
| Integer | A whole number. |
| Float | A decimal number. |
| Boolean | A True or False value. |
| int() | Converts suitable values into integers. |
| float() | Converts suitable values into decimal numbers. |
| str() | Converts values into text. |
| bool() | Converts values into Boolean values. |
In this module, you learned that data conversion and formatting help computers and people work with information correctly.
You learned how to:
Answer all 10 questions below to check what you have learned.
Module 6: Combining and Merging Data
In the next module, you will learn how to bring information from different tables and datasets together to create more useful information.
CRMNuggets โ Learn. Practice. Get Certified.