In Post #5, we successfully wrote and ran our first “Hello, World!” script. It was a simple, one-line program, but like any language, Python has rules of grammar—its syntax—that govern how we write more complex instructions.
In this post, we’ll dissect the anatomy of a Python script to understand these foundational rules. We’ll focus on the most defining feature of Python syntax: the use of whitespace and what it means for the structure of our code. Mastering these concepts is the key to writing clean, functional Python.
The Most Important Rule: Indentation Defines Structure
In many other programming languages, like JavaScript or Java, developers use curly braces {}
to group code into blocks. For example, a block of code might look like this in JavaScript:
// JavaScript Example
if (x > 5) {
console.log("x is greater than 5");
console.log("this is also in the block");
}
Python takes a different, much cleaner approach. It uses indentation—the empty space at the beginning of a line—to define code blocks. There are no curly braces. The structure of the code is determined entirely by its layout.
The rule is simple: Code that is at the same level of indentation belongs to the same block.
Let’s look at a quick example. Don’t worry about understanding the if
statement yet; just focus on the structure.
weather = "sunny"
if weather == "sunny":
print("It's a beautiful day!")
print("You should go outside.")
print("This line will run no matter the weather.")
In this script:
- The two
print()
statementsprint("It's a beautiful day!")
andprint("You should go outside.")
are indented. This means they belong together in a block that will only run if theweather
is “sunny”. - The final statement,
print("This line will run...")
, is not indented. This means it is not part of theif
block and will run after theif
block has finished, regardless of the outcome.
This use of “significant whitespace” forces programmers to write code that is visually organized and easy to read.
How much to indent? The official and universally accepted standard is to use four spaces for each new level of indentation. Most code editors can be configured to automatically insert four spaces when you press the Tab
key.
The Colon: Signaling a New Block
You may have noticed the colon (:
) at the end of the line if weather == "sunny":
.
The colon is a crucial part of Python’s syntax. It is a signal that tells the interpreter: “Get ready! A new, indented block of code is about to begin on the next line.”
You will see this colon used constantly in Python whenever you start a new logical block, such as with if
statements, loops (for
, while
), and when defining your own functions (def
) or classes (class
).
Statements: The Sentences of Your Program
A statement is a single instruction or command that Python can execute. Think of it as one complete sentence in your program.
In our first script from Post #5, print("Hello, World!")
was a single statement. In the example above, weather = "sunny"
is another statement, known as an assignment statement because it assigns a value to a name.
Generally, you write one statement per line. This keeps your code clean and easy to follow.
A Quick Note on Case Sensitivity
One final rule that often trips up beginners is that Python is case-sensitive. This means that uppercase and lowercase letters are treated as different characters.
For example, a variable named weather
is completely different from a variable named Weather
. The function print()
is not the same as Print()
.
If you get an error in your code, one of the first things you should check is whether you have used the correct capitalization for your function and variable names.
What’s Next?
You now understand the fundamental grammar of Python: indentation isn’t for style, it’s for structure; colons introduce these blocks; and every line is a statement. These rules are the bedrock upon which all Python programs are built.
As our programs get more complex, it will become important to leave notes inside our code—for our future selves and for other programmers who might read it. In Post #7, we’ll learn how to write comments: special lines of text that Python ignores but are crucial for making our code readable and understandable.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com