Python Syntax Basics

In the last lesson you wrote and ran Your First Python Program. So you have seen Python work. Now let us slow down and look at the rules of how Python code is written. These rules are the grammar of the language. Once you know them, the rest of Python feels easy.

πŸ€” Why Syntax Rules Matter

Here is the pain. You copy some code. You change one little thing. Suddenly Python shows an error you do not understand. Most of the time it is not a deep bug. It is a small syntax slip. A space sits in the wrong place.

Syntax is just the set of rules about where things go on the line. Python is strict about a few of them. Here is the good news. There are only a few rules. Once they click, your errors mostly go away.

One thing surprises people who come from other languages. In Python, spaces at the start of a line have meaning. They are not just for looks. Let us see why.

🧩 One Statement Per Line

A statement is one instruction you give to Python. The simplest rule is that each line holds one statement.

Here is a tiny program with three statements, one per line:

name = "Riya"
age = 25
print(name)

Python reads top to bottom. It takes one line at a time. First it stores the name. Then it stores the age. Then it prints the name. Simple and clean.

You do not end a line with a semicolon like some other languages do. The end of the line is the end of the statement. That is one less thing to remember.

πŸ”‘ The Colon Starts a Block

Sometimes you want a group of lines to belong together. For example, β€œif this is true, do all of these things.” That group is called a block.

In Python you start a block with a colon : at the end of the line. Then you indent the lines that go inside it.

You will see the colon with words like if, for, and def (which makes a function). Here is the shape of it:

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

Read it like a sentence. The line if age >= 18: ends with a colon. The colon says β€œa block is coming.” The two indented print lines below it are inside the if. They only run when age is 18 or more.

The colon is the signal. Whenever a line opens a block, it ends with a colon. Forget the colon and Python stops and asks for it.

πŸ“ Indentation Is the Block

This is the big idea, so let us take it slowly. Many languages use curly braces { and } to mark where a block begins and ends. Python does not. Python uses the indentation itself. That is the empty space at the start of a line.

Look at these two versions side by side. First, a line that is inside the if:

age = 20
if age >= 18:
print("Inside the if")
print("Always runs")

The first print is indented, so it lives inside the if. The second print is not indented, so it sits outside the if and runs no matter what. The indentation alone decides what belongs where.

When you run that with age = 20, both lines print:

Output

Inside the if Always runs

Now change age to a smaller number. The indented line is skipped, because it belongs to the if. The line that is not indented still runs.

So the rule is plain. Indented lines belong to the block above them. The same level of indent means the same block. This is why people say that in Python, indentation is not optional. It is how Python understands your code.

🚫 Wrong Indentation Breaks It

Because spacing carries meaning, getting it wrong is a real error. It is not just messy style. Watch this.

Here the indentation is uneven inside the same block:

if age >= 18:
print("Line one")
print("Line two")

The first print has four spaces. The second has six. Python expected them to line up, and they do not, so it stops and shows you this:

Output

IndentationError: unexpected indent

You will also get an error if you forget to indent at all after a colon:

# ❌ Avoid: nothing is indented after the colon
if age >= 18:
print("This should be inside")

Python opens the block with the colon, then finds no indented line, and complains:

Output

IndentationError: expected an indented block

The fix is always the same. Make every line in the same block start at the same number of spaces:

# βœ… Good: both lines indented the same amount
if age >= 18:
print("This is inside")
print("So is this")

Note

IndentationError is one of the most common errors for people new to Python. When you see it, do not panic. Just check that the lines in each block start at the same column.

πŸ”  Use 4 Spaces, and Stay Consistent

Python lets you pick how much to indent, as long as you stay consistent inside a block. But the whole Python world has settled on one habit. Use four spaces for each level of indentation.

Most code editors do this for you. When you press the Tab key, the editor inserts spaces automatically. So you usually just press Tab and keep writing.

There is one trap to avoid. Do not mix tabs and spaces in the same file. They can look identical on screen, but Python counts them as different indentation. That leads to confusing errors. Pick spaces, let your editor handle Tab, and you are fine.

Here is two levels of indentation, four spaces each:

for letter in "hi":
if letter == "h":
print("Found h")

The if is one level in (four spaces). The print is two levels in (eight spaces), because it sits inside both the for and the if. Each step inward is another four spaces.

πŸ”‘ Python Is Case Sensitive

Python treats uppercase and lowercase letters as completely different. So name, Name, and NAME are three separate things. They are not the same name written three ways.

This code creates one variable and then tries to print a different one:

name = "Arjun"
print(Name)

You typed name (lowercase) but asked for Name (capital N). Python never saw a Name, so it errors:

Output

NameError: name β€˜Name’ is not defined

This matters for built-in words too. The print function is print, all lowercase. Writing Print or PRINT will not work. The same is true for True and False. They must start with a capital letter in Python. Get the case right and a lot of mystery errors go away.

πŸ“‹ The Rules at a Glance

Here is a quick table you can come back to.

Rule What it means
One statement per line Each line is one instruction. No semicolon needed.
Colon starts a block if, for, and def lines end with :.
Indentation makes the block Indented lines belong to the block above them.
Four spaces per level The standard indent. Stay consistent, do not mix tabs and spaces.
Case sensitive name and Name are different. So is print vs Print.
Blank lines are fine Empty lines do nothing. Use them to space out your code.

That last one is worth a word. Blank lines are allowed anywhere and Python simply ignores them. So feel free to leave an empty line between two ideas to make your code easier to read. It changes nothing about how the program runs.

⚠️ Common Mistakes

A few slips catch almost everyone at the start:

  • Forgetting the colon at the end of an if, for, or def line.
  • Mixing different amounts of indentation in the same block, which gives an IndentationError.
  • Mixing tabs and spaces in one file. They look the same but Python counts them differently.
  • Getting the case wrong, like writing Print instead of print, which gives a NameError.
  • Indenting a line for no reason. A stray indent on a normal line is an error too.

βœ… Best Practices

Small habits that keep your code clean and error-free:

  • Let your editor insert four spaces when you press Tab, and never look back.
  • Keep every line in the same block lined up at the same column.
  • Write one clear statement per line. It is easier to read and easier to fix.
  • Use blank lines to separate steps in your program, like paragraphs in writing.
  • When you hit an IndentationError, check the spacing first. It is almost always the cause.

🧩 What You’ve Learned

A quick recap of the rules you now know:

  • βœ… Python runs one statement per line, and you do not need a semicolon to end it.
  • βœ… A colon : at the end of a line starts a block, used with if, for, and def.
  • βœ… Indentation, the space at the start of a line, is what defines a block in Python.
  • βœ… Four spaces per level is the standard, and mixing indentation causes an IndentationError.
  • βœ… Python is case sensitive, so name and Name are different names.
  • βœ… Blank lines are ignored and are great for making your code readable.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    In Python, how do you mark which lines belong inside an if-block?

    Why: Python uses indentation, the space at the start of a line, to decide which lines are inside a block.

  2. 2

    What does a colon ':' at the end of a line tell Python?

    Why: Lines like `if`, `for`, and `def` end with a colon to signal that an indented block follows.

  3. 3

    Why does writing `Print("hi")` instead of `print("hi")` cause an error?

    Why: Python treats uppercase and lowercase as different, so `Print` is not the same as the built-in `print`.

  4. 4

    What is the standard amount of indentation for one level in Python?

    Why: Four spaces per level is the agreed standard across the Python world, and most editors insert it for you.

πŸš€ What’s Next?

Now that you can write Python that Python understands, the next step is learning how to leave notes in your code for yourself and others. Up next:

Python Comments

Share & Connect