Learn Python: The Fundamentals

Now that you’ve got variables down and understand how to name them properly, let’s talk about something just as important — comments and writing readable code.

 

Comments don’t change how your code runs, but they make a huge difference when it comes to understanding what your code is doing. Whether it’s you coming back to your code later, or someone else reading it for the first time — clear comments are gold.

 

In Python, a comment starts with the # symbol. Everything after that on the same line is ignored by Python.

 

# This is a comment. Python will ignore it.
name = "Alice"  # This sets the name variable
print(name)

 

In this example, the comments help explain what each line is doing — without affecting the code itself. That’s the goal: make your code easier to read and maintain, without getting in the way.

 

Why Comments Matter

Picture this: you open a file you wrote six months ago. No comments. You stare at the code like it’s written in alien symbols. What was this part supposed to do again?

 

Now imagine that same file, but with short notes explaining your logic. Boom — you understand it in seconds. That’s what good comments do.

 

They’re also a huge help when working on teams. Clear comments mean your teammates (and future you) don’t have to play detective to understand your code.

 

But here’s the catch — don’t comment every single line. Your code should mostly speak for itself. Comments are best used to explain the why, not just the what.

 

# BAD COMMENT: This adds 5 to x
x = x + 5

# GOOD COMMENT: Increase the score by 5 points for a correct answer
score = score + 5

 

Multi-line Comments

Sometimes, a quick note isn’t enough. If you want to write a longer explanation, you can use triple quotes (""" or ''') to write a multi-line comment or description at the top of your code.

 

"""
This program calculates the area of a rectangle.
It asks the user for the length and width,
then multiplies them together to get the area.
"""
length = 10
width = 5
area = length * width
print(area)

 

Interactive Exercise: Write Your Own Comments

Give it a go. Run the code below and add comments next to or above each line to explain what’s happening.

 


# Add comments to explain what’s happening
name = "Alice"
age = 25
print("My name is", name)
print("I am", age, "years old")

 
Write comments starting with # before or after each line of code.
 

Interactive Exercise: Multi-line Comments

Use triple quotes to describe what the following program does. Keep it simple and clear.

 


"""
Add your description here
"""
# Program: Calculate the total cost of items
item_price = 20
quantity = 3
total = item_price * quantity
print("Total cost:", total)

 
Use triple quotes to write a summary of the program at the top.
 

Practising good commenting habits now will pay off later. Clean, readable code with helpful notes makes your life easier — and shows you know how to think like a developer. It’s not just about writing code that works. It’s about writing code that’s easy to follow, easy to share, and easy to fix.