Welcome to Lesson 02! In Lesson 01 you learned how Python works and how to print things on the screen. Now we are going to learn two of the most important building blocks of every Python program:
- Comments · notes you write inside your code that Python completely ignores. They are for you (the programmer), not for the computer.
- Variables · labelled boxes that store information so your program can remember and reuse it later.
By the end of this lesson you will be able to:
- Write single-line and multi-line comments in Python.
- Create variables and store different kinds of data in them.
- Name variables correctly following Python's rules.
- Assign one value to many variables at the same time.
- Print variables to the screen in different ways.
- Understand the difference between local and global variables.
Take your time, try every example, and read every explanation. There are no silly questions here · we start from zero.
Before we dive in, make sure you are comfortable with one thing from Lesson 01:
The print() function · this is how we display output on the screen.
print("Hello, World!")Expected output:
Hello, World!print(...) simply shows whatever is inside the parentheses on your screen. We will use it constantly in this lesson to check our work.
What Is a Comment?
Think of a comment like a sticky note on your code. Imagine you are writing a recipe. Sometimes you jot little notes to yourself: "Use ripe tomatoes here!" Those notes are for you · not part of the cooking instructions. Comments in Python work exactly the same way.
A comment is a line (or part of a line) in your Python file that Python completely skips when running your program. It does not execute, it does not produce output, and it does not affect anything. It is purely a human-readable note.
Why Do Comments Exist?
Comments solve several real problems:
- Explaining what code does · When you or a teammate reads the code three months later, comments remind everyone why a decision was made.
- Making code readable · Long programs become confusing. Comments break them into understandable sections.
- Temporarily disabling code · During testing, you can "turn off" a line of code by making it a comment instead of deleting it.
Professional programmers in software companies, data science labs, research institutions, and engineering firms all use comments daily. Well-commented code is considered a sign of a good programmer.
How to Create a Single-Line Comment
In Python, a comment starts with the hash symbol #. Everything on the line after # is ignored by Python.
Syntax:
# This is a commentExample 1 · Comment on its own line:
# This line is a comment. Python ignores it completely.
print("Hello, World!")Expected output:
Hello, World!Notice that only the print line produced output. The comment did nothing · Python skipped it entirely.
Breaking it down line by line:
# This line is a comment.→ Python sees the#and skips the whole line.print("Hello, World!")→ Python runs this and displaysHello, World!
Example 2 · Comment at the end of a line (inline comment):
You can also place a comment at the end of a working line of code. Python will run the code part and ignore everything after the #.
print("Hello, World!") # This is an inline commentExpected output:
Hello, World!The comment here is just a label explaining what this line does. Python ran print("Hello, World!") and completely ignored # This is an inline comment.
Tip: Leave two spaces before an inline comment. It keeps your code tidy and readable.
Example 3 · Using a comment to disable code:
This is very useful when you are testing. Instead of deleting a line, you turn it into a comment:
# print("Hello, World!")
print("Cheers, Mate!")Expected output:
Cheers, Mate!The first print is commented out · Python ignores it. Only the second print runs.
Why is this useful? Imagine you wrote a line of code that is causing an error. Instead of deleting it (and losing your work), you comment it out temporarily. Then you uncomment it later when you are ready.
How to Create a Multi-Line Comment
Python does not have a built-in multi-line comment symbol (unlike some other languages). But there are two easy ways to write comments that span multiple lines.
Method 1 · Use # on every line:
# This is a comment
# written across
# more than one line
print("Hello, World!")Expected output:
Hello, World!Each line starts with #, so Python skips each one individually.
Method 2 · Use a triple-quoted string (not assigned to any variable):
Python lets you write text inside triple quotes """...""". If you never assign this text to a variable, Python reads it and immediately throws it away. Programmers take advantage of this trick to write multi-line comments.
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")Expected output:
Hello, World!The triple-quoted block is technically a string, but since it is not stored anywhere or used for anything, Python discards it. The print still runs normally.
Important Note: This method is widely used but is technically a string that gets ignored · not a true comment. The
#method is the "official" way to write comments in Python.
A Quick Comparison
| Situation | What to use |
|---|---|
| Short explanation on one line | # Your note here |
| Note at the end of a code line | code # Your note |
| Long explanation across many lines | # on each line OR """...""" |
| Temporarily disabling a line | # print("disabled line") |
Thinking Prompt
What happens if you put a
#in the middle of a word, likepri#nt("Hello")? Try it! What do you notice?
What Is a Variable?
Imagine you are working at a supermarket. You need to remember the price of tomatoes: ₦500. You write it on a small sticky label and stick it on a jar. Now whenever you look at the jar, you know the price.
A variable in Python is exactly like that sticky label and jar. The label is the variable name. The jar holds the value. Whenever your program needs that value, it looks up the label.
In simple terms: A variable is a named location in your computer's memory where you can store a piece of data.
Why Do Variables Exist?
Without variables, every program would have to hardcode every number and every piece of text · and you could never reuse or change data. Variables allow programs to:
- Remember information (a user's name, a test score, a temperature reading)
- Reuse data without repeating it
- Update data as the program runs (e.g., a score increasing in a game)
- Make calculations flexible (calculate tax for any salary, not just one fixed amount)
In data science, a variable might store a dataset. In a game, it stores the player's health. In an app, it stores a user's login name. Variables are everywhere.
Creating a Variable
Python makes creating variables extremely simple. You do not need to declare what type of data goes in it first. You just write the name, an equals sign =, and the value.
Syntax:
variable_name = valueThe = sign here is called the assignment operator. It means: "store the value on the right into the name on the left."
Example 1 · Your first variable:
x = 5
print(x)Expected output:
5x = 5→ Create a variable namedxand store the number5in it.print(x)→ Show the value stored inx. Python looks upx, finds5, and prints it.
Example 2 · A text variable:
name = "John"
print(name)Expected output:
Johnname = "John"→ Create a variable callednameand store the text (called a string)"John"in it.print(name)→ Python looks upname, finds"John", and prints it.
Example 3 · Multiple variables:
x = 5
y = "John"
print(x)
print(y)Expected output:
5
JohnYou can create as many variables as you need. Each one stores its own value independently.
Variables Can Change Type
In many programming languages, once you say a variable holds a number, it can only hold numbers. Python is more flexible · a variable can hold different types of data at different times. This is called dynamic typing.
x = 4 # x is an integer (a whole number)
x = "Sally" # Now x is a string (text)
print(x)Expected output:
SallyThe variable x first held the number 4. Then we reassigned it to hold the text "Sally". Python updated it without complaint. The old value 4 is gone.
Thinking prompt: What would happen if you added a third line:
x = 3.14? What wouldprint(x)show then?
Casting · Forcing a Specific Data Type
Sometimes you want to make absolutely sure a variable stores a specific type of data. You do this with casting · using built-in functions to convert values.
| Function | What it does | Example |
|---|---|---|
str(...) | Converts to text (string) | str(3) → '3' |
int(...) | Converts to a whole number (integer) | int(3) → 3 |
float(...) | Converts to a decimal number | float(3) → 3.0 |
Example:
x = str(3) # x will be '3' (text)
y = int(3) # y will be 3 (whole number)
z = float(3) # z will be 3.0 (decimal number)
print(x)
print(y)
print(z)Expected output:
3
3
3.0Notice how x printed 3 (looks the same) but it is actually stored as text, not a number. And z printed 3.0 · the .0 shows it is stored as a decimal (float).
Checking the Type of a Variable
Curious what type of data a variable holds? Use the built-in type() function. It tells you exactly what kind of value is stored.
x = 5
y = "John"
print(type(x))
print(type(y))Expected output:
<class 'int'>
<class 'str'>intmeans integer · a whole number (no decimal point).strmeans string · text.
You will learn much more about data types in the next lesson. For now, just know that type() is a useful tool to check your work.
Single Quotes or Double Quotes?
When storing text in a variable, you can use either single quotes '...' or double quotes "...". They work exactly the same way.
x = "John"
# is the same as
x = 'John'
print(x)Expected output:
JohnUse whichever style you prefer · but try to stay consistent throughout your code.
Case-Sensitivity: Capital Letters Matter!
Python is case-sensitive. This means age, Age, and AGE are three completely different variables. A capital letter makes it a different name.
a = 4
A = "Sally"
print(a)
print(A)Expected output:
4
Sallya and A are two separate variables. A = "Sally" does not overwrite a = 4. They live independently.
Common mistake: Writing
Print(x)instead ofprint(x). Python is case-sensitive about built-in functions too!
Rules for Naming Variables
Variable names are not completely free · Python has rules. If you break these rules, Python will give you an error and your program will not run.
The rules are:
- A variable name must start with a letter (a · z, A · Z) or an underscore
_. - A variable name cannot start with a number.
- A variable name can only contain letters, numbers, and underscores · no spaces, no hyphens, no symbols.
- Variable names are case-sensitive (
age≠Age≠AGE). - Variable names cannot be Python reserved keywords (special words Python already uses, like
print,if,for,while, etc.).
Legal (valid) variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"All of the above are perfectly fine variable names.
Illegal (invalid) variable names · these cause errors:
2myvar = "John" # ERROR: cannot start with a number
my-var = "John" # ERROR: hyphens are not allowed
my var = "John" # ERROR: spaces are not allowedIf you try running any of these, Python will give you a SyntaxError.
Naming Conventions · How Professionals Name Variables
Beyond the strict rules, professional programmers follow certain styles to make their code readable. For Python, the three most common styles for multi-word variable names are:
1. Camel Case · Start each word (except the first) with a capital letter:
myVariableName = "John"2. Pascal Case · Start every word with a capital letter:
MyVariableName = "John"3. Snake Case · Use underscores between words (this is the recommended Python style):
my_variable_name = "John"Best practice for Python: The official Python style guide (called PEP 8) recommends snake_case for variable names. This is what you will see in professional Python code and most tutorials. Examples:
student_name,total_score,average_temperature.
Descriptive Names Are Better Than Short Names
A variable named x tells you nothing. A variable named student_age tells you exactly what it stores. Use descriptive names.
# Hard to understand:
x = 18
y = "Amara"
# Easy to understand:
student_age = 18
student_name = "Amara"Both versions work the same way in Python. But the second version is much easier to read and maintain.
Assigning Different Values to Multiple Variables at Once
Python has a shortcut: you can assign values to multiple variables in a single line, separating everything with commas.
Syntax:
variable1, variable2, variable3 = value1, value2, value3Example:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)Expected output:
Orange
Banana
CherryPython matched them up in order: x gets "Orange", y gets "Banana", z gets "Cherry".
Important: The number of variable names on the left must match the number of values on the right. If they don't match, Python will give you a
ValueError.
Example of the error to avoid:
# This causes an error:
x, y = "Orange", "Banana", "Cherry" # 2 names, 3 values — mismatch!Assigning the Same Value to Multiple Variables at Once
If you want several variables to hold the same value, you can chain assignments together.
x = y = z = "Orange"
print(x)
print(y)
print(z)Expected output:
Orange
Orange
OrangeAll three variables (x, y, z) now store the string "Orange". This is very useful when setting up default values.
Unpacking a Collection
If you have a list (a collection of values), you can "unpack" it · assign each item in the list to its own variable, all in one line.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)Expected output:
apple
banana
cherryfruitsis a list containing three items.x, y, z = fruitsunpacks those three items into three separate variables.xgets"apple",ygets"banana",zgets"cherry".
This technique is used constantly in real Python programs · for example, when reading rows from a spreadsheet or receiving data from a web API.
Printing a Single Variable
The simplest way to display a variable's value is to pass it to print():
x = "Python is awesome"
print(x)Expected output:
Python is awesomePrinting Multiple Variables in One print() Statement
You can print several variables together by separating them with commas inside print(). Python automatically adds a space between them.
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)Expected output:
Python is awesomePython put a space between each variable for you · that is the default behaviour of print() with commas.
Combining Variables with the + Operator
You can also join (concatenate) variables using the + operator. This works for text (strings):
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)Expected output:
Python is awesomeNotice there is a space inside "Python " and "is " · because + does not add spaces automatically (unlike commas).
Watch out: You can only use
+to join variables of the same type. Adding a number to a text string causes an error.
This causes an error:
x = 5
y = "John"
print(x + y) # TypeError: cannot mix int and strThe fix: Convert the number to a string first using str():
x = 5
y = "John"
print(str(x) + " " + y)Expected output:
5 JohnThe Best Way to Print Variables: f-strings
The cleanest, most professional way to print variables mixed with text is using f-strings (formatted strings). An f-string starts with the letter f before the quote, and you put variable names inside curly braces {}.
name = "Amara"
age = 18
print(f"My name is {name} and I am {age} years old.")Expected output:
My name is Amara and I am 18 years old.This approach works with all data types (text, numbers, decimals) without any conversion needed. You will use f-strings constantly in real Python programming.
What Is Scope?
Before understanding global variables, you need to understand scope · the idea of where a variable can be seen and used in your program.
Think of it like this: imagine you write a note and put it inside a locked box. Only people who have the key to that box can read the note. In Python, functions (blocks of reusable code you will learn later) work like locked boxes. Variables created inside a function are only visible inside that function.
- A local variable is created inside a function. It only exists inside that function.
- A global variable is created outside all functions. It can be seen and used anywhere in the program.
Global Variables
A variable created at the top level of your script (outside any function) is called a global variable. It is accessible from everywhere in the program.
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()Expected output:
Python is awesomex = "awesome"is created outsidemyfunc()· it is a global variable.- Inside
myfunc(), Python can still seexbecause global variables are visible everywhere. myfunc()calls the function to run it.
Local Variables
If you create a variable inside a function, it is local · it only exists inside that function.
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()Expected output:
Python is fantasticHere, x = "fantastic" is created inside myfunc(). It is a local variable. It exists only while the function is running and disappears after.
When a Local Variable Has the Same Name as a Global Variable
This is where beginners often get confused. When a function creates a local variable with the same name as a global variable, the local one takes over inside the function · but the global one stays unchanged outside.
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)Expected output:
Python is fantastic
Python is awesome- Inside
myfunc(), the localx = "fantastic"takes priority. - Outside
myfunc(), the globalx = "awesome"is untouched.
The global Keyword · Modifying a Global Variable from Inside a Function
Normally, when you create a variable inside a function, Python treats it as local. But what if you want to create or modify a global variable from inside a function? You use the global keyword to tell Python to use the global version.
Example 1 · Creating a global variable inside a function:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)Expected output:
Python is fantasticglobal xtells Python: "ThexI am about to create/modify is the globalx, not a local one."- After
myfunc()runs, the globalxnow holds"fantastic". print("Python is " + x)runs outside the function and uses the globalx.
Example 2 · Modifying an existing global variable inside a function:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)Expected output:
Python is fantasticWithout the global x keyword, this function would create a local copy instead of modifying the real global variable. The global keyword ensures we modify the actual global one.
Real-world use case: In a game program, you might have a global variable called
score = 0. Every function that gives the player points needs to useglobal scoreto actually update the shared score · otherwise each function would just create its own local copy.
Exercise 1 · Writing Useful Comments
Objective: Practice adding meaningful comments to code.
Scenario: You are writing a program that calculates a student's final grade.
Task: Copy this code and add a comment above each line explaining what it does:
student_name = "Chidera"
score = 85
passing_score = 50
print(student_name)
print(score)
print(score >= passing_score)One possible solution:
# Store the student's full name
student_name = "Chidera"
# Store the student's exam score out of 100
score = 85
# Define the minimum score needed to pass
passing_score = 50
# Print the student's name
print(student_name)
# Print the student's score
print(score)
# Print True if the student passed, False if they failed
print(score >= passing_score)Expected output:
Chidera
85
TrueSelf-check questions:
- Did your comments explain the purpose of each line, not just what Python does mechanically?
- Are your comments concise and easy to read?
Exercise 2 · Creating and Printing Variables
Objective: Create variables to store information about a person and print them.
Scenario: You are building a simple student record system.
Steps:
- Create a variable
student_nameand store your own name in it. - Create a variable
student_ageand store your age in it. - Create a variable
student_classand store your class name (e.g.,"SS2") in it. - Print all three variables using an f-string in one
print()statement.
Expected code:
student_name = "Amara"
student_age = 17
student_class = "SS2"
print(f"Name: {student_name}, Age: {student_age}, Class: {student_class}")Expected output:
Name: Amara, Age: 17, Class: SS2What-if challenge: What happens if you change student_age to a string like "seventeen" instead of the number 17? Try it and observe.
Exercise 3 · Multiple Assignment
Objective: Use both forms of multiple assignment.
Scenario: Track the three top scores in a class quiz.
Task: Assign 95, 87, and 76 to variables first_place, second_place, and third_place in a single line, then print all three.
Solution:
first_place, second_place, third_place = 95, 87, 76
print(f"1st Place: {first_place}")
print(f"2nd Place: {second_place}")
print(f"3rd Place: {third_place}")Expected output:
1st Place: 95
2nd Place: 87
3rd Place: 76Bonus: Now assign the same default score 0 to three variables player1, player2, player3 in one line.
player1 = player2 = player3 = 0
print(player1, player2, player3)Expected output:
0 0 0Exercise 4 · Global vs Local Variables
Objective: Understand the difference between global and local scope.
Scenario: A simple game program needs a shared score.
# Global score starts at zero
score = 0
def award_bonus():
global score
score = score + 50 # Add 50 bonus points
def award_points():
global score
score = score + 10 # Add 10 regular points
award_points()
award_points()
award_bonus()
print(f"Final score: {score}")Expected output:
Final score: 70Trace through the logic:
scorestarts at0.award_points()adds 10 →score = 10award_points()adds 10 again →score = 20award_bonus()adds 50 →score = 70printshows70
Self-check: What would happen if you removed both global score lines? Try it and read the error message.
Mistake 1: Starting a Variable Name with a Number
# WRONG
2name = "Amara" # SyntaxError
# CORRECT
name2 = "Amara" # Number at the end is fineMistake 2: Using Spaces in Variable Names
# WRONG
student name = "Amara" # SyntaxError
# CORRECT
student_name = "Amara" # Use underscore insteadMistake 3: Forgetting That Python is Case-Sensitive
name = "Amara"
print(Name) # NameError: 'Name' is not defined
print(name) # This works — lowercase 'n'Mistake 4: Adding a Number and a String with +
age = 18
message = "I am " + age + " years old." # TypeError
# CORRECT — convert the number to a string first:
message = "I am " + str(age) + " years old."
print(message)Expected output:
I am 18 years old.Mistake 5: Mismatched Variables and Values in Multiple Assignment
# WRONG — 3 values, only 2 variable names
x, y = 1, 2, 3 # ValueError: too many values to unpack
# CORRECT — equal numbers on both sides
x, y, z = 1, 2, 3Mistake 6: Forgetting the global Keyword
score = 0
def add_points():
score = score + 10 # UnboundLocalError!
add_points()Python sees that you're trying to use score before assigning it locally. Fix:
score = 0
def add_points():
global score
score = score + 10 # Now it works
add_points()
print(score)Expected output:
10Mistake 7: Commenting with // (That's JavaScript, Not Python!)
// This is NOT a Python comment # SyntaxError!
# This IS a Python comment # Correct!Python uses # for comments. // is used in JavaScript and other languages.
Project Overview
You will build a simple Python program that creates a complete profile card for a student and prints it in a formatted, readable way. This project combines comments, variables, multiple assignment, output formatting, and global variables.
Stage 1 · Setup the Student Data
# =============================================
# STUDENT PROFILE CARD — Mini Project
# =============================================
# Student personal information
student_name = "Fatima Bello"
student_age = 16
student_gender = "Female"
student_class = "JSS3"
student_school = "Unity Secondary School"Lesson 02 complete! 🎉
You covered:
- ✅ Lesson Introduction
- ✅ Prerequisite Concepts
- ✅ PART 1 · Python Comments
- ✅ PART 2 · Python Variables
- ✅ PART 3 · Variable Names
- ✅ PART 4 · Assigning Multiple Values
- ✅ PART 5 · Output Variables
- ✅ PART 6 · Global Variables