Welcome to Module 4 of Python Fundamentals Level 1 Professional! In the previous module, you learned about Python syntax, comments, indentation and how to write clean Python code.
In this module, you will learn how Python stores information using variables. You will also discover the different types of data Python can work with, including text, numbers and true-or-false values.
By the end of this module, you should be able to:
A variable is a named place used by a program to store information. You can think of it as a labelled container that holds a value.
Imagine you have a school bag with different compartments. One compartment holds your books, another holds your pens, and another holds your lunch.
In Python, variables work in a similar way. Each variable has a name and stores a value that your program can use.
student_name = "Amina"
student_age = 13
student_score = 85
In this example:
The equal sign (=) is used to assign a value to a variable.
Creating a variable in Python is simple. You choose a name and assign a value to it using the equal sign.
variable_name = value
name = "Tunde"
age = 15
school = "Bright Future School"
product_price = 2500
Once you create a variable, you can use its name to access the value stored inside it.
You can use the print() function to display the information stored in a variable.
student_name = "Amina"
student_age = 13
print(student_name)
print(student_age)
Output:
Amina
13
Python displays the values stored in the variables instead of displaying their names.
student_name = "Amina"
student_age = 13
print("Student Name:", student_name)
print("Student Age:", student_age)
Output:
Student Name: Amina
Student Age: 13
Python allows you to choose names for your variables, but there are rules you must follow.
student_name = "Tunde"
age = 13
total_score = 90
school2026 = "Greenfield School"
_first_name = "Amina"
2student = "Tunde"
student name = "Amina"
class = "JSS 2"
Why are these invalid?
A data type describes the kind of information stored in a variable. Python supports different data types because programs work with different kinds of information.
For example, a student's name is text, their age is a whole number, and their examination score may contain a decimal number.
Python has several built-in data types. Some important beginner data types are:
A string is a data type used to store text. It can contain letters, words, numbers written as text, spaces and special characters.
Strings are usually written inside single or double quotation marks.
student_name = "Amina"
country = "Nigeria"
school = "Bright Future School"
message = "Welcome to Python"
You can display these values using print():
print(student_name)
print(country)
print(school)
print(message)
Output:
Amina
Nigeria
Bright Future School
Welcome to Python
A number placed inside quotation marks is treated as text, not as a numerical value.
phone_number = "08012345678"
age = 13
The phone number is stored as a string because it is written inside quotation marks. The age is stored as an integer.
An integer is a whole number without a decimal point. Integers can be positive, negative or zero.
age = 13
number_of_students = 45
temperature_change = -5
score = 100
These values are integers because they do not contain decimal points.
Imagine a school has 120 students in a computer training programme. You can store the number of students in a variable.
number_of_students = 120
print(number_of_students)
Output:
120
A float is a number that contains a decimal point. Floats are useful when working with measurements, prices, percentages and other values that require decimal numbers.
product_price = 2500.50
student_height = 1.65
average_score = 78.5
temperature = 36.6
These values are floats because they contain decimal points.
Imagine you are buying a school notebook for โฆ1,250.50. You can store the price as a float.
notebook_price = 1250.50
print(notebook_price)
Output:
1250.5
Python may display a number without unnecessary trailing zeros. The value is still represented as a floating-point number.
A Boolean is a data type that stores only two possible values: True or False.
Boolean values are useful when a program needs to make decisions or check whether something is correct.
is_student = True
is_registered = False
has_paid = True
exam_completed = False
For example, a school system may use a Boolean value to check whether a student has completed registration.
registration_completed = True
print(registration_completed)
Output:
True
Remember that Python uses capital letters for the first letters of True and False. Writing true or false in lowercase is not the same thing.
The following table shows the differences between the four important data types introduced in this module.
| Data Type | Meaning | Example |
|---|---|---|
| String (str) | Stores text. | "Amina" |
| Integer (int) | Stores whole numbers. | 25 |
| Float (float) | Stores decimal numbers. | 25.5 |
| Boolean (bool) | Stores True or False. | True |
Python provides a built-in function called type(). It helps you identify the data type stored in a variable.
name = "Tunde"
age = 13
height = 1.65
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
The results show that:
A variable can be updated by assigning a new value to it. This allows programs to work with information that changes.
student_age = 12
print(student_age)
student_age = 13
print(student_age)
Output:
12
13
The first print() displays 12. After the variable is updated, the second print() displays 13.
Python allows you to assign values to multiple variables in one instruction.
first_name = "Amina"
age = 13
country = "Nigeria"
first_name, age, country = "Amina", 13, "Nigeria"
print(first_name)
print(age)
print(country)
Output:
Amina
13
Nigeria
Python assigns each value to the corresponding variable in the order they appear.
Type conversion means changing a value from one data type to another.
For example, you may receive a number as text but need to use it in a mathematical calculation. You can convert the text into a numerical value.
The int() function converts a suitable value into an integer.
age = "15"
new_age = int(age)
print(new_age)
print(type(new_age))
Output:
15
<class 'int'>
The float() function converts a suitable number into a floating-point number.
price = "2500"
new_price = float(price)
print(new_price)
Output:
2500.0
The str() function converts a value into text.
age = 13
age_text = str(age)
print(age_text)
print(type(age_text))
Output:
13
<class 'str'>
The bool() function converts a value into True or False based on Python's rules for truth values.
number = 1
result = bool(number)
print(result)
Output:
True
In Python, zero, an empty string and other empty values are generally considered False. Non-zero numbers and non-empty strings are generally considered True.
You can use variables to create messages that include stored information.
student_name = "Tunde"
student_age = 13
print("My name is", student_name)
print("I am", student_age, "years old")
Output:
My name is Tunde
I am 13 years old
An f-string allows you to place variable values directly inside a text message.
student_name = "Tunde"
student_age = 13
print(f"My name is {student_name}")
print(f"I am {student_age} years old")
Output:
My name is Tunde
I am 13 years old
The letter f before the quotation mark tells Python that the text is an f-string. The curly brackets show where variable values should be inserted.
Imagine you are developing a student information system for a secondary school in Ibadan.
Create a Python program that stores:
Use the correct data type for each piece of information.
# Student information program
student_name = "Amina"
student_age = 13
school_name = "Future Leaders School"
examination_score = 85.5
is_registered = True
print("STUDENT INFORMATION")
print(f"Name: {student_name}")
print(f"Age: {student_age}")
print(f"School: {school_name}")
print(f"Score: {examination_score}")
print(f"Registered: {is_registered}")
Expected output:
STUDENT INFORMATION
Name: Amina
Age: 13
School: Future Leaders School
Score: 85.5
Registered: True
Imagine you are building a simple shopping program for a customer buying items from a supermarket.
# Shopping information program
product_name = "School Bag"
product_price = 8500.50
quantity = 2
product_available = True
print("SHOPPING INFORMATION")
print(f"Product: {product_name}")
print(f"Price: N{product_price}")
print(f"Quantity: {quantity}")
print(f"Available: {product_available}")
Expected output:
SHOPPING INFORMATION
Product: School Bag
Price: N8500.5
Quantity: 2
Available: True
| Term | Meaning |
|---|---|
| Variable | A named place used to store information in a program. |
| Value | The information stored in a variable. |
| Data Type | Describes the kind of information a value represents. |
| String (str) | A data type used to store text. |
| Integer (int) | A data type used to store whole numbers. |
| Float (float) | A data type used to store decimal numbers. |
| Boolean (bool) | A data type that stores True or False. |
| Assignment | Giving a value to a variable. |
| type() | A function used to identify the data type of a value. |
| Type Conversion | Changing a value from one data type to another. |
| f-string | A way to insert variable values into formatted text. |
In this module, you have learned that:
Answer the following questions to test your understanding.
Congratulations! You have completed Module 4 of Python Fundamentals Level 1 Professional.
In Module 5: Python Operators and Expressions, you will learn how to perform calculations, compare values, combine conditions and use Python operators to solve everyday problems.
Keep practising! Variables and data types are important building blocks for almost every Python program.