Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

How Python code is structured


Programming works only if the computer understands the structure of your code.
This structure is called syntax.

Syntax defines:

Even small syntax mistakes can prevent code from running. Clear syntax is not only about avoiding errors. It also makes your code easier for others to read and reuse.


1. Identifiers and case

An identifier is a name used for:

Example:

x = 5
X = "Harry"

print(x)
print(X)

Python is case sensitive.

x and X are two different identifiers.


2. Comments

Comments explain your code to humans.
They are ignored by Python.

Comments start with #.

# This is a comment
number = 5  # This comment is after code

print(number)

Good comments:


3. Strings and quotes

Strings represent text.

Python allows:

The same quote type must start and end the string.

a = 'Eier'
b = "Kuchen"
c = """This is
a multi-line
string"""

print(a, b)
print(c)

Triple quotes are useful for multi line text.


4. Statements and lines

Each line in Python normally represents one statement.

x = 5
y = 12

You can write multiple statements on one line using ;, but this reduces readability.

x = 5; y = 12
print(x + y)

In this course, prefer one statement per line.


Continuing long lines

Sometimes a statement is too long to fit comfortably on one line.

Inside brackets (), [], or {}, Python automatically allows the statement to continue across multiple lines.

week_days = (
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday"
)

print(week_days)

5. Indentation and blocks

Indentation is not just formatting in Python.
It defines structure.

Blocks such as loops, conditions, and functions:

Example:

for i in range(0, 10, 2):
    print(i)

If indentation is missing or inconsistent, Python raises an error.


Nested indentation

Blocks can contain other blocks.

def calculate_fee(x):
    if x < 100:
        fee = x * 0.05
    else:
        fee = x * 0.10
    return fee

print(calculate_fee(80))

6. Common beginner errors

If your code does not run, first check:


7. Summary

In this section, you learned:

Syntax may feel strict, but it enables clarity.
Structure is what makes code readable, maintainable, and reproducible.
Clear structure is essential for reproducible workflows.


Looking ahead

In the next section, you will apply your syntax knowledge in a real notebook environment. You will:

Syntax tells Python what your code means. Running notebooks shows you what it does.