No history yet

Python Syntax

The Grammar of Code

Every language has rules. In English, we use punctuation and sentence structure to make our meaning clear. Programming languages have similar rules, which we call syntax. Python's syntax is famous for being clean and readable, making it one of the most beginner-friendly languages out there.

Think of it like a recipe. A well-written recipe has clear, step-by-step instructions. Python code aims for that same level of clarity. The two most important principles to understand are indentation and statement structure.

Indentation is Everything

In many other programming languages, programmers use curly braces {} to group lines of code into a block. Python takes a different approach. It uses indentation—the whitespace at the beginning of a line—to group statements together.

This isn't just a style suggestion; it's a strict rule. If you get the indentation wrong, your program won't work. This forces code to be written in a structured, readable way.

The standard convention in Python is to use four spaces for each level of indentation.

Imagine a to-do list. The main tasks are flush with the left margin. Any sub-tasks required to complete a main task are indented underneath it. Python code is organized in the exact same way. An indented block of code is always part of the statement that came before it.

This structure makes it visually obvious how different parts of a program relate to each other.

Statements and Readability

A statement is a single instruction for the computer to follow. In Python, the rule of thumb is simple: one statement per line. This keeps the code from becoming cluttered.

print("This is one instruction.")
print("This is a second instruction.")

Following this rule makes your code easy to read from top to bottom. This emphasis on readability is a core part of Python's philosophy. Clean, understandable code is easier to debug and maintain, both for the person who wrote it and for anyone else who needs to work on it later.

Python's syntax is meant to be simple and readable, reflecting the structure of natural language, making it a better option for novices.

Sometimes, you can add comments to your code. A comment is a note for human readers that the computer completely ignores. In Python, comments start with a hash symbol (#). They're useful for explaining why you wrote a piece of code a certain way.

# This is a comment. The computer will ignore it.
print("Hello, world!") # You can also put comments at the end of a line.

By combining mandatory indentation with a simple one-statement-per-line structure, Python ensures that code isn't just functional, but also organized and easy to understand.

Time to check your understanding of these core syntax rules.

Quiz Questions 1/4

What is the primary role of indentation in Python code?

Quiz Questions 2/4

In Python, it's a strict rule that you must have exactly one statement per line.