Chapter 03 of 11

Python Syntax, Indentation & Comments

Before writing bigger Python programs, there are three basic things you should understand: syntax, indentation, and comments.

Don't worry—Python keeps these concepts quite simple.

What is Python Syntax?

Syntax means the rules that define how Python code should be written.

Just like English has grammar rules, Python has syntax rules. If we don't follow them, Python will show an error.

For example:

print("Hello, World!")

This follows valid Python syntax.

But if we write something incorrectly:

print("Hello, World!

Python will report a SyntaxError because the string is missing its closing quotation mark.

So, simply remember:

Syntax is the set of rules you need to follow when writing Python code.

Python is Case-Sensitive

Python treats uppercase and lowercase letters as different.

For example:

name = "John"
Name = "Jason"

name and Name are two different variables.

Similarly:

print("Hello")

is correct, while:

Print("Hello")

will cause an error because Print and print are not the same.

What is Indentation?

Indentation means the spaces at the beginning of a line of code.

In many programming languages, indentation is mainly used to make code look organized. But in Python, indentation is part of the syntax and is used to define blocks of code.

For example:

age = 20

if age >= 18:
    print("You are an adult")

Notice the spaces before print().

The indented line belongs to the if statement.

Why is Indentation Important?

Without proper indentation, Python doesn't know which statements belong to a particular block.

For example:

age = 20

if age >= 18:
print("You are an adult")

This will produce an IndentationError.

Python commonly uses 4 spaces for indentation.

if age >= 18:
    print("You are an adult")
    print("You can vote")

Both print() statements are indented by four spaces, so both belong to the if block.

Nested Indentation

Indentation also allows us to create blocks inside other blocks.

age = 20

if age >= 18:
    if age >= 21:
        print("Age is 21 or above")

Each level is indented further.

Don't worry if nested indentation looks confusing right now. You'll become comfortable with it as we learn if, loops, functions, and other concepts.

What are Comments?

Comments are notes written inside the code that Python doesn't execute.

Comments are mainly used to explain code and make programs easier to understand.

A single-line comment starts with #:

# This is a comment
print("Hello")

Python ignores the comment and executes only:

print("Hello")

Comments Can Explain Code

For example:

# Store the student's name
name = "John"

# Display the name
print(name)

Comments can be especially useful when you're working on a large project or sharing your code with other developers.

Multi-Line Comments

Python doesn't have a special multi-line comment syntax like some other languages.

However, you can use multiple # comments:

# This program
# displays a
# welcome message

print("Welcome to Python")

You may also see triple-quoted strings used for documentation:

"""
This is a multi-line string.
It can span multiple lines.
"""

These are technically strings, not ordinary comments. When used at the beginning of a module, function, or class, they can serve as docstrings.

A Simple Example

Let's put everything together:

# Check the student's age
age = 20

if age >= 18:
    # Display the result
    print("John is an adult")

Here:

  • # Check the student's age is a comment.

  • age = 20 follows Python syntax.

  • The print() statement is indented because it belongs to the if block.

Once you understand syntax, indentation, and comments, Python code becomes much easier to read and write.