Chapter 03

Seeing with print, explaining with comments

How print writes several things at once, how sep and end change that, and why comments exist — your first tool for seeing inside a running program.

20 minPython 3.12
  1. 1Encounter
  2. 2Understand
  3. 3Worked
  4. 4Predict
  5. 5Apply
  6. 6Stretch

The problem we are solving

You wrote a program. You ran it. The terminal said nothing.

Did it work? Did it stop halfway? What did the calculation come to inside? There is no way to know, because a computer volunteers nothing — whatever you want to see, you have to ask for.

print is how you ask. Early on it is your only window: where the program got to, what a variable holds, what the arithmetic produced. Experienced developers drop print calls into code many times a day even with a debugger at hand — that is not a beginner's crutch, that is the job.

By the end of this mission you can

  • Print several things in one call, and control the gap between them
  • Reshape output with sep and end
  • Spot the difference between text and numbers — why "2" + "3" is not 2 + 3
  • Write comments, and tell which comments are actually worth writing

Prerequisites: The terminal, folders, and your first project.


print takes more than one thing

Separate as many values as you like with commas, and print writes them all on one line:

python
print("Order", 7, "confirmed")
text
Order 7 confirmed

Notice you never typed a space, yet the words came out spaced. print puts a single space between each comma-separated value on its own.

And at the end of every print, it moves to a new line:

python
print("line one")
print("line two")
text
line one
line two

An empty print() produces just a blank line — the simplest way to break long output into readable groups.

sep — the gap, your way

If that space is not what you want, change it:

python
print("2026", "09", "10", sep="-")
print("home", "user", "notes.txt", sep="/")
print("a", "b", "c", sep="")
text
2026-09-10
home/user/notes.txt
abc

end — what happens at the end of the line

By default print ends the line and moves down. That is adjustable too:

python
print("Loading", end="")
print(".", end="")
print(".", end="")
print(". done")
text
Loading... done

Four separate print calls, one line of output — because the first three were told to put nothing at the end of the line.

Text and numbers are not the same thing

These two lines look nearly identical, and behave differently:

python
print(2 + 3)
print("2" + "3")
text
5
23

Inside quotes it is text, and + does not add — it joins. Outside quotes it is a number, and + really does add.

This distinction is the foundation of the missions that follow, and it is also the cause of about half the errors beginners hit. For now just hold onto the rule: quotes mean text.

Comments — the lines Python never reads

From a # to the end of that line, Python ignores everything:

python
# Friday delivery report
total = 12 + 8
print("Total:", total)  # the sum shown at the bottom of the report
text
Total: 20

Comments have no effect on how the program runs. They are for people — for your colleague, and for you in three months, by which time you will have forgotten all of this.

*A good comment says why, not what.* What the code does is visible in the code:

python
# Weak: repeats what the code already says
count = count + 1  # add 1 to count

# Useful: gives the reason, which the code cannot show
count = count + 1  # the header row was counted too, so shift by one

You learn nothing from the first one — the line plainly adds one. The second says why one is added, which no amount of reading the code would tell you.

While learning, comments have one more use — switching a line off temporarily to see how the program behaves without it:

python
print("first")
# print("second")
print("third")
text
first
third

A complete example

Create report.py and run it:

python
# A small sales report
print("=" * 24)
print("SALES REPORT")
print("=" * 24)

print("Items sold:", 3 + 5 + 2)
print("Revenue:", 3 * 120 + 5 * 90 + 2 * 250)

print()
print("Prepared by", "Rafi", sep=": ")
text
========================
SALES REPORT
========================
Items sold: 10
Revenue: 1310
Prepared by: Rafi

Look at "=" * 24. Multiplying text by a number makes Python repeat it that many times, so there is no need to type twenty-four equals signs by hand to draw a rule.


When it breaks

SyntaxError: unterminated string literal A quote was opened and never closed. print("Hello) does this. Count the opening and closing quotes.

You need a quote inside the text Use one kind outside and the other inside: print("It's working") or print('She said "yes"').

print "Hello" raises SyntaxError That is Python 2 syntax. In Python 3 print is a function, so it needs parentheses: print("Hello"). The mistake usually arrives by way of an old tutorial or answer.

Giving sep or end a value without quotes print("a", "b", sep=-) will not run. sep and end always take text: sep="-".

Which language should comments be in? Python accepts Bengali comments, but in this course all code — comments, variable names, filenames — is written in English. The reason is practical, not stylistic: not everyone on the team your code lands in will read Bengali, and the error messages, the documentation and the answers you will search for are all in English. Explanation in Bengali, code in English — that split holds across every mission.