Lesson 2: Understanding Python Syntax and Running Scripts

Indentation, structure, and how to execute Python code

Python’s Clean and Minimal Syntax

Python is known for its readable, minimal syntax. Unlike many languages, Python uses indentation—not braces—to define code blocks. This makes code visually clean and consistent.

Indentation Rules

Indentation is not optional in Python. A typical block looks like this:

if True:
    print("This is inside the block")
print("This is outside the block")

Most developers use 4 spaces per indentation level.

Comments in Python

Use the # symbol for single-line comments:

# This is a comment
print("Hello")  # Inline comment

Variables and Dynamic Typing

Python does not require type declarations. Variables are created when assigned:

name = "Alice"
age = 30
is_active = True

Running Python Interactively

You can run Python in interactive mode by typing:

python

Then try:

>> print("Hello from interactive mode!")

Creating and Running Python Scripts

To run a Python file, create a file named script.py:

print("This is a Python script!")

Then run it from the terminal:

python script.py

Using the Python REPL vs. Scripts

Common Syntax Errors

Beginners often encounter:

Next Steps

Now that you understand Python’s syntax and how to run code, you’re ready to explore variables and data types in Lesson 3: Variables, Data Types, and Type Conversion.

← Back to Lesson Index