I1 · Python Programming for AI

Output, Comments and Program Structure

print() displays values on the screen. A comment, starting with #, is a note for people that Python ignores. A statement is one instruction. Python uses indentation, spaces at the start of a line, to group statements into blocks, so indentation is part of the syntax.

8 min Intermediate · Sublevel Beginner Jump to the activity

What you need: The earlier topics of this module.

Definition

Why can a single space at the start of a line break a Python program?

print() displays values on the screen. A comment, starting with #, is a note for people that Python ignores. A statement is one instruction. Python uses indentation, spaces at the start of a line, to group statements into blocks, so indentation is part of the syntax.

In simple words

print("Name:", "Riya") shows Name: Riya, with a space added between the items. # lines are like pencil notes in the margin: the reader sees them, Python skips them. Indentation is like the steps of a recipe that belong to 'while the milk boils': they are pushed in to show they are inside.

Overview

  • print() sends text and values to the screen; its sep and end options control spacing and line endings.
  • Comments are notes for humans that Python ignores; they start with #.
  • A Python program runs from top to bottom: imports first, then definitions, then the main steps.
  • Indentation (four spaces by convention) shows which lines belong to a block such as a loop or function.
  • PEP 8 is the official style guide: clear names, spaces around operators and lines up to 79 characters.
  • The line if __name__ == "__main__": lets a file run as a program and also be imported safely.

Why it matters

Clear output and good comments make programs easy to check and share. Indentation errors are among the most common problems for beginners, so understanding them early saves a lot of frustration.

Key terms

print()
A built-in function that displays values, separated by spaces and followed by a new line.
Comment
Text after # that Python ignores, written to explain the code to people.
Statement
One complete instruction, usually one line.
Block
A group of statements indented together under a line ending with a colon.
Docstring
A string at the top of a file or function that documents what it does.
PEP 8
The official style guide for writing readable Python.

Types

Programs talk to people in two directions: comments explain the code to readers, and output shows results to users. These are the kinds of each.

1. Single-line and block comments

Lines starting with # that explain the code below them; several such lines form a block comment.

  • How it works: Python skips everything after # on a line; there is no special multi-line comment symbol, so long notes use # on every line.
  • Example: # Convert marks out of 80 into a percentage
  • When to use it: Use them to explain why a step is done, not what each line obviously does.

2. Inline comments

A short # note at the end of a line of code.

  • How it works: Separate it from the code by at least two spaces; it explains something specific to that line.
  • Example: rate = 0.18 # GST rate for this item
  • When to use it: Use them sparingly for units, sources of constants or surprising values.

3. Docstrings

A string in triple quotes placed first inside a module, function or class to describe it.

  • How it works: Unlike a comment, Python keeps it: help(function) and editors show it, and tools build documentation from it.
  • Example: The first line inside def average(marks): is """Return the mean of a list of marks."""
  • When to use it: Use one for every function, class and module that someone else may call.

4. Plain print output

print() with one or more values separated by commas.

  • How it works: Values are converted to text and joined with a space (change it with sep=), and a new line is added at the end (change it with end=).
  • Example: print("Total:", 450, sep=" ") shows Total: 450; print("Loading", end="...") stays on one line.
  • When to use it: Use it for quick results and messages in small programs.

5. Formatted output (f-strings)

Strings prefixed with f in which expressions inside braces are filled in and formatted.

  • How it works: Write f"...{value:format}..."; format codes control decimals, width, alignment and thousands separators.
  • Example: f"Average: {avg:.1f}%" prints Average: 76.4%; f"{1234567:,}" prints 1,234,567.
  • When to use it: Use it for any output that people will read, such as reports, tables and messages with numbers.

Explained in detail

More about print()

print() accepts any number of values and prints them separated by a space. Two optional settings change this: sep sets what goes between values, and end sets what goes at the end instead of a new line. So print("2024", "08", "15", sep="-") prints 2024-08-15, and print("Loading", end="...") keeps the next output on the same line. print() can show numbers, text, lists and almost any other value, which makes it the simplest debugging tool: printing a variable's value at a key moment often reveals a problem immediately.

Writing useful comments

Comments are for people, including your future self. Good comments explain why something is done, not what, because the code already shows what. "# add 1 to count" above count = count + 1 adds nothing. "# count starts at 1 because the header row was already read" explains a decision someone might otherwise change by mistake. A docstring, a string in triple quotes at the start of a file or function, describes its purpose and is shown by help(), so tools and other programmers can read it.

Indentation is grammar

Many languages use curly braces to group statements and treat indentation as decoration. Python makes indentation part of the grammar: every line ending with a colon, such as if, for, while or def, must be followed by an indented block, and the block ends when the indentation returns to the previous level. The convention is four spaces per level. Mixing tabs and spaces can cause an IndentationError or, worse, code that looks right but groups statements differently from what you see.

A clear program layout

  • A docstring at the top saying what the program does.
  • Imports next, one per line.
  • Constants in capital letters, such as PASS_MARK = 40.
  • Functions, each doing one job.
  • The main steps at the bottom, calling those functions.

This layout follows PEP 8, the style guide written in 2001 by Guido van Rossum and others. Tools such as black and ruff can format code to PEP 8 automatically, so teams spend less time arguing about style.

Finally, remember that Python reads a script strictly from top to bottom, so a function must be defined before the line that calls it runs.

How it works

  • print() can take several items separated by commas; sep changes the separator and end changes what comes at the end.
  • Use four spaces for each level of indentation, and be consistent.
  • A line ending with a colon, such as if or for, must be followed by an indented block.

In code

Comments explain the program; sep and end shape the output.

profile_card.py
# A profile card, printed neatly
name = "Riya"
city = "Pune"
hobbies = 3

print("=" * 20)
print("Name:", name)
print("City:", city)
print("Hobbies", hobbies, sep=": ")
print("Loves AI", end="!")
print()  # an empty print moves to a new line
print("=" * 20)

Output

====================
Name: Riya
City: Pune
Hobbies: 3
Loves AI!
====================

Try it yourself

Will it run?

Decide whether each small program runs or gives an error.

  • print("Hello") # say hi

  • print("Missing quote)

  • print("A", "B", sep="-")

  • print("Hello") (spaces before print, nothing above)

Common mistakes

  • Mixing tabs and spaces for indentation.
  • Indenting a line that isn't inside a block.
  • Writing comments that repeat the code instead of explaining why.

Check yourself

Question 1

1. What does print("A", "B", sep="-") show?

Question 2

2. What does Python do with a line starting with #?

Question 3

3. How many spaces is the usual indentation?

Summary

  • print() shows output; sep and end control spacing.
  • Comments start with # and are ignored by Python.
  • Indentation defines blocks: use four spaces, consistently.

Finished reading?

Sources: PEP 8: Style Guide for Python Code