Login Sign Up
Python Comments
Chapter 4 🟡 Intermediate

Python Comments

Master the concept step by step with clear explanations, examples, and code you can run.

Python Comments for Beginners: The Ultimate Guide to Leaving Sticky Notes in Your Code

Hello there! Welcome back to our Python journey; I am incredibly happy for see you here again.

In our last lesson, we discovered how to properly structure our code using invisible blank spaces and we learned that if we break the grammar rules, computer will give us SyntaxError. We also learned that we should probably use exactly 4 spaces to group our code blocks.

Today as promised we are actually going for learn how to leave helpful little sticky notes inside our programs for ourselves and other humans to read. We call these Python Comments. Let’s dive in from absolute zero!


Why Do We Need Comments? (The "Why" Before the "How")

Imagine you're pretty much reading really difficult math textbook. Sometimes, you might write tiny note into the margin with a pencil right; you might write "This formula calculates the area of a circle."

That pencil note isn't meant for the teacher, and it isn't part of the math problem itself. It is actually note out of you, for you, helping you remember what's going on.

In programming the comment is basically exactly like that pencil note. It is actually text written inside your code that a computer completely ignores. When Python reads your file it executes the code but turns a blind eye to your comments.

Here is a simple visual map of how Python's "brain" processes your file when it sees comments:

graph TD
    A[Python Reads Your Code File] --> B{Is the line a comment?}
    B -- Yes --> C[Ignore it completely!]
    B -- No --> D[Execute the code as an instruction]
    C --> E[Move to next line]
    D --> E

We write comments to explain a "why" behind our code to warn other programmers about tricky parts, or to temporarily disable a line of code while we're basically testing.


1. The Single-Line Comment

The most common way to write a note in Python is the single-line comment. To create one, you simply use a hash symbol (#).

The moment Python sees a # it completely ignores everything that comes after it on that specific line.

Here is probably what it looks like:

# This is a single-line comment. Python will not read this.
print("Hello, World!") # This prints a message to the screen.

Notice how you can put comment on its very own line, or you can place it right next towards some real code (this is called an inline comment). It's basically incredibly flexible!


2. Multi-Line Comments: Fascinating Python Secret

Sometimes you need to write the very long explanation, and maybe you're actually explaining the complex algorithm or leaving module-level documentation.

Here is a fascinating piece about information that many standard textbooks get wrong. As highlighted in a reliable April 2025 complete guide on comments and docstrings, Python actually doesn't have just true multi-line comment syntax built into the language!

So, how do we write a comment that spans multiple lines; you have two highly recommended options.

Option A: Stacking Hash Symbols

You can just use consecutive single-line comments for longer explanations. This is the safest and most standard way to do it.

# We need to calculate the temperature for the Smart Weather Assistant.
# First, we check if it is raining.
# Then, we adjust the temperature based on the weather conditions.
weather_assistant(True, 20)

Option B: Triple-Quoted Strings

If you're pretty much writing massive block of text stacking # symbols can actually get tiring, while instead programmers often use triple quotes (""" or ''').

"""
This is a multi-line explanation.
We often use this for very long descriptions.
Python technically sees this as a string of text, 
but if we don't assign it to a variable, Python just ignores it!
"""

Expert Note: When you use these triple quotes right at the very top of a function or a file to explain what it does, they're called Docstrings. We'll just explore docstrings much deeper when we learn of building our own functions later on!


3, and formatting Rules: Keeping Your Code Beautiful

When you add comments and format your code it is actually vital towards keep everything clean and readable; python developers follow a famous style guide called PEP 8.

Operator Spacing Rule

While we're basically adding comments to explain our math, PEP 8 warns us to always use our own judgment but to never use more than one space around binary operators (like +, - *). You must always have the exact same amount of whitespace on both sides of a binary operator.

# Correct PEP 8 Formatting:
c = (a + b) * (a - b)

# Wrong (do not do this!):
c = (a+b) * (a-b)

Function Arrow Rule

If you ever see a tiny arrow (->) into advanced Python code (used on function annotations), PEP 8 states that you really have to use normal rules towards colons and always have spaces around the -> arrow.


4, while a Real-World Trap: Comments and Indentation

Here is a key tip for absolute beginners, while we know that Python uses indentation (invisible spaces at the start for a line) to figure out which block with code belongs together.

If you put a comment inside an indented block of code comment must be indented with the exact same number of spaces!

if is_raining:
    # This comment is indented by 4 spaces, just like the code below it!
    print("Take an umbrella")

As we discussed in our last chapter, you should always use 4 spaces and never mix tabs and spaces otherwise you'll just get a nasty IndentationError.

If you are basically ever modifying code in very basic text editor (like a program called nano), it can be hard to tell if previous programmer used tabs or spaces. A brilliant technique out of a June 25 2025 tutorial on proper Python indentation is to use your keyboard's Left Arrow key.

If you press the Left key to move your cursor backward across the blank space and the cursor jumps completely over the whitespace in one massive leap the editor is inserting tab characters, and if it moves slowly space by space you're safely using spaces. Always match whatever spacing an original file used!


What's Next?

You're basically doing absolutely fantastic. Today, you learned how to leave helpful notes inside your code using a # symbol, and you learned a secret that Python doesn't actually have a true multi-line comment built in, while

now that we know how to properly space out our code and leave sticky notes towards ourselves it is actually time to start storing actual data! In our next chapter, we will cover Python Variables. I will show you exactly how to create tiny digital boxes inside your computer's memory for hold onto numbers, words, and information; we will cover it next!

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.