Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.2

Comments, Readability & Program Structure

The habits every examiner rewards: # comments, meaningful names, one statement per line, and the case-sensitivity rules that trip students up.

01

Learning objectives

  • Write single-line comments with # and state that Python ignores them completely.
  • Explain why comments and clear names matter for maintenance and exams.
  • Describe the top-to-bottom, one-statement-per-line structure of a Python program.
  • Apply basic style rules: snake_case names, spaces around operators, four-space indents.
  • Predict the effect of Python's case-sensitivity on names like Name and name.
02

Lesson notes

A comment is text in your file that begins with # and is completely ignored by the interpreter. It exists for exactly one audience: humans. The you-of-six-months-from-now, a classmate reviewing your work, an examiner looking for evidence you understood the problem — all of them read comments to learn why the code does what it does. Code says what happens; comments explain the reasoning, the assumptions and the traps. Board marking schemes regularly award method marks for a sensible comment on each logical step.

Comments cost nothing at runtime — the interpreter skips them before executing — so the only limit is usefulness. Write them for the non-obvious: # subtract discount before tax teaches; # x = x is noise. Above a tricky block, a comment can state the plan in one sentence, so a reader understands the goal before reading the details. In this course every worked example carries comments for exactly that reason: read them as part of the lesson.

Readability is a discipline with a few simple rules. Give every variable a meaningful name: total_marks explains itself while t forces the reader to scroll and guess. Follow snake_case (lowercase words joined by underscores) for names. Put one statement per line, spaces around operators (a + b, not a+b), and a blank line between logical sections. These conventions come from Python's official style guide, PEP 8, and following them makes your code look professional for free.

Structure-wise, every Python program is a vertical list of statements executed strictly from top to bottom, and each statement is complete on its own line. Python is also case-sensitive: marks, Marks and MARKS are three different variables. Typing Print("hi") with a capital P raises NameError, because the built-in is lowercase print. This single fact explains a huge share of beginner errors, so train your eye to check the case of every name.

Put together, the first three lessons give you the full skeleton of every program you will write this year: a comment header saying what the program does, meaningful names assigned top to bottom, and print statements that report results. Every challenge in the Labs section is marked with the same skeleton, and the sooner the shape feels natural, the faster the later chapters — conditions, loops, functions — will click onto it.

03

Worked code examples

A well-commented marksheet program

Comments state each value's purpose; the names obtained, total and percentage explain the arithmetic without further comments. This is the standard every answer should match.

# Marksheet: read total marks and percentage grade band
obtained = 448          # marks the student scored
total = 550             # maximum possible marks
percentage = obtained / total * 100

print("Percentage:", percentage)
print("Grade band: A" if percentage >= 80 else "Grade band: below A")
Output
Percentage: 81.45454545454545
Grade band: A

Style comparison: same logic, opposite readability

Both versions compute the identical value, but the second survives review, debugging and the viva. Same program, radically different maintainability.

# Hard to read
a=412;b=550;c=a/b*100;print(c)

# Easy to read
obtained = 412
total = 550
percentage = obtained / total * 100
print(percentage)
Output
74.90909090909092
74.90909090909092

Case-sensitivity is not a style choice

marks was defined; Marks was not. Python treats the two spellings as unrelated variables, so the second print raises a NameError — modern interpreters even suggest the close match.

marks = 88
print(marks)     # fine
print(Marks)     # different name entirely
Output
88
NameError: name 'Marks' is not defined. Did you mean: 'marks'?
04

Key terminology

comment

Text starting with # that Python ignores, written to explain the code to humans.

readability

How easily another person (or future you) can understand the code — names, spacing and structure all contribute.

snake_case

The naming style of lowercase words joined by underscores, e.g. total_marks.

case-sensitive

Property of Python meaning Name, name and NAME are three different identifiers.

PEP 8

Python's official style guide describing recommended naming, spacing and layout conventions.

05

Common misconceptions

Common mistake

Comments make the program slower.

Correct understanding

The interpreter skips comments entirely; they have zero effect on execution speed.

Common mistake

Comments are only for weak programmers.

Correct understanding

Professional codebases comment heavily — the comment explains intent, and intent is invisible in code alone.

Common mistake

Python ignores capital letters in names.

Correct understanding

Python is case-sensitive: marks and Marks are different variables, and confusing them causes NameError.

06

Check your understanding

1. What does the interpreter do with a line like # TODO: fix later?

2. Which variable name follows Python's conventions AND is valid?

3. A program defines result = 91, then prints Result. What happens?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.