Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† PYTHON FUNDAMENTALS LEVEL 1 PROFESSIONAL ยท Lesson 5 of 9

Module Four

๐Ÿ“– 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.
5
PYTHON FUNDAMENTALS LEVEL 1 PROFESSIONAL Module 4: Python Variables and Data Types

Module 4: Python Variables and Data Types

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.

Learning Objectives

By the end of this module, you should be able to:

  • Explain what a variable is in Python.
  • Create and assign values to variables.
  • Identify the rules for naming variables.
  • Understand the different Python data types.
  • Work with strings, integers and floating-point numbers.
  • Understand Boolean values.
  • Use the type() function to identify data types.
  • Convert one data type into another.
  • Create simple programs using variables and data types.

1. What Is a Variable?

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.

Example

student_name = "Amina"
student_age = 13
student_score = 85

In this example:

  • student_name stores the text "Amina".
  • student_age stores the number 13.
  • student_score stores the number 85.

The equal sign (=) is used to assign a value to a variable.

2. How to Create a Variable in Python

Creating a variable in Python is simple. You choose a name and assign a value to it using the equal sign.

Basic Structure

variable_name = value

Example 1: Storing a Name

name = "Tunde"

Example 2: Storing an Age

age = 15

Example 3: Storing a School Name

school = "Bright Future School"

Example 4: Storing a Price

product_price = 2500

Once you create a variable, you can use its name to access the value stored inside it.

3. Displaying Variable Values

You can use the print() function to display the information stored in a variable.

Example

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.

Displaying Labels and Variable Values Together

student_name = "Amina"
student_age = 13

print("Student Name:", student_name)
print("Student Age:", student_age)

Output:

Student Name: Amina
Student Age: 13

4. Rules for Naming Variables

Python allows you to choose names for your variables, but there are rules you must follow.

Important Rules

  1. A variable name must begin with a letter or an underscore.
  2. A variable name must not begin with a number.
  3. A variable name cannot contain spaces.
  4. A variable name can contain letters, numbers and underscores.
  5. A variable name must not be a Python reserved keyword.
  6. Variable names are case-sensitive.

Examples of Valid Variable Names

student_name = "Tunde"
age = 13
total_score = 90
school2026 = "Greenfield School"
_first_name = "Amina"

Examples of Invalid Variable Names

2student = "Tunde"
student name = "Amina"
class = "JSS 2"

Why are these invalid?

  • 2student: Starts with a number.
  • student name: Contains a space.
  • class: Uses a reserved Python keyword.

5. Understanding Python Data Types

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:

  • String (str)
  • Integer (int)
  • Float (float)
  • Boolean (bool)

6. String Data Type (str)

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.

Examples

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

Important: Numbers Can Also Be Strings

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.

7. Integer Data Type (int)

An integer is a whole number without a decimal point. Integers can be positive, negative or zero.

Examples of Integers

age = 13
number_of_students = 45
temperature_change = -5
score = 100

These values are integers because they do not contain decimal points.

Practical Example

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

8. Float Data Type (float)

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.

Examples of Floats

product_price = 2500.50
student_height = 1.65
average_score = 78.5
temperature = 36.6

These values are floats because they contain decimal points.

Practical Example

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.

9. Boolean Data Type (bool)

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.

Examples

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.

10. Comparing Python Data Types

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

11. How to Check the Data Type of a Variable

Python provides a built-in function called type(). It helps you identify the data type stored in a variable.

Example

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:

  • name is a string.
  • age is an integer.
  • height is a float.
  • is_student is a Boolean.

12. Changing the Value of a Variable

A variable can be updated by assigning a new value to it. This allows programs to work with information that changes.

Example

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.

13. Assigning Multiple Variables

Python allows you to assign values to multiple variables in one instruction.

Example 1: Separate Assignment

first_name = "Amina"
age = 13
country = "Nigeria"

Example 2: Multiple Assignment

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.

14. Type Conversion in Python

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.

14.1 Converting to an Integer

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

14.2 Converting to a Float

The float() function converts a suitable number into a floating-point number.

price = "2500"

new_price = float(price)

print(new_price)

Output:

2500.0

14.3 Converting to a String

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

14.4 Converting to a Boolean

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.

15. Combining Variables with Text

You can use variables to create messages that include stored information.

Example Using Commas in print()

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

Example Using an f-string

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.

16. Practical Activity: Create a Student Information Program

Imagine you are developing a student information system for a secondary school in Ibadan.

Your Task

Create a Python program that stores:

  • Student name.
  • Student age.
  • School name.
  • Examination score.
  • Whether the student is registered.

Use the correct data type for each piece of information.

Sample Solution

# 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

17. Mini Project: Create a Simple Shopping Information Program

Imagine you are building a simple shopping program for a customer buying items from a supermarket.

Project Requirements

  1. Create a variable for the product name.
  2. Create a variable for the product price.
  3. Create a variable for the quantity purchased.
  4. Create a variable to indicate whether the product is available.
  5. Display all the information using print().

Sample Project Code

# 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

18. Important Terms to Remember

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.

19. Module Summary

In this module, you have learned that:

  • Variables are used to store information in Python programs.
  • The equal sign (=) assigns values to variables.
  • Variable names must follow Python naming rules.
  • Strings store text, while integers store whole numbers.
  • Floats store decimal numbers.
  • Booleans store True or False.
  • The type() function identifies a value's data type.
  • Variables can be updated with new values.
  • Type conversion changes a value from one data type to another.
  • f-strings make it easy to combine text and variable values.

20. Knowledge Check

Answer the following questions to test your understanding.

  1. What is a variable in Python?
    • A. A computer monitor
    • B. A named place used to store information
    • C. A keyboard shortcut
    • D. A web browser
  2. Which symbol is used to assign a value to a variable?
    • A. +
    • B. -
    • C. =
    • D. /
  3. Which of these is a valid variable name?
    • A. 2name
    • B. student name
    • C. student_name
    • D. class
  4. Which data type is used to store text?
    • A. Integer
    • B. String
    • C. Float
    • D. Boolean
  5. Which data type stores whole numbers?
    • A. String
    • B. Float
    • C. Integer
    • D. Boolean
  6. Which of these is a floating-point number?
    • A. 25
    • B. "Amina"
    • C. 25.75
    • D. True
  7. Which two values can a Boolean variable store?
    • A. Yes and No as text
    • B. True and False
    • C. 1 and 2 only
    • D. Red and Blue
  8. Which Python function checks the data type of a value?
    • A. print()
    • B. input()
    • C. type()
    • D. display()
  9. Which function converts a suitable value into an integer?
    • A. str()
    • B. float()
    • C. int()
    • D. bool()
  10. What is the purpose of an f-string?
    • A. To delete variables
    • B. To insert variable values into formatted text
    • C. To install Python
    • D. To create a computer folder

Knowledge Check Answers

Click here to reveal the correct answers
  1. B โ€” A named place used to store information.
  2. C โ€” =.
  3. C โ€” student_name.
  4. B โ€” String.
  5. C โ€” Integer.
  6. C โ€” 25.75.
  7. B โ€” True and False.
  8. C โ€” type().
  9. C โ€” int().
  10. B โ€” To insert variable values into formatted text.

21. What Is Next?

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.

๐Ÿ† 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.
โ†’