Python Syntax Basics Explained

Python syntax is the set of rules for writing Python code correctly. If your code follows these rules, Python can read and run it.

This page explains the basic writing rules you need before learning variables, if statements, loops, and functions. The goal is simple: help you read and write small pieces of valid Python code.

Quick example

Use this small example to see the main syntax rules in one place:

name = "Sam"
age = 12

if age > 10:
    print(name)

What this shows:

  • = assigns a value to a variable
  • "Sam" is a string, so it uses quotes
  • if age > 10: ends with a colon
  • print(name) is indented because it belongs inside the if block

Output:

Sam

What Python syntax means

Syntax means the rules for writing Python code in a form Python understands.

Think of it like grammar in a language:

  • If the rules are correct, the meaning is clear
  • If the rules are broken, Python cannot run the code

This page focuses on the basic writing rules beginners need most. It does not go deep into error fixing, but if your code will not run, see how to fix SyntaxError: invalid syntax.

A simple Python statement

A statement is one instruction Python can run.

Examples:

print("Hello")
x = 5

What these statements do:

  • print("Hello") displays text
  • x = 5 stores the number 5 in a variable named x

Many beginner programs are just small statements written one after another. If you want to learn the difference between statements and expressions later, see understanding Python statements and expressions.

How Python uses new lines

In Python, a statement usually ends at the end of a line.

For example:

print("One")
print("Two")
print("Three")

Python reads this as three separate statements.

Because of this, beginner code is usually written as:

  • one statement per line
  • in a clear top-to-bottom order

Some long statements can continue across lines, but that is not something you need right away. For now, the safest habit is to write one complete instruction on each line.

Indentation is part of Python syntax

In many languages, spaces at the start of a line are just for style. In Python, they are part of the syntax.

Indentation tells Python that a line belongs inside a block.

Example:

age = 12

if age > 10:
    print("Older than ten")

Here, print("Older than ten") is indented, so Python knows it belongs inside the if statement.

Without indentation, the code is not valid:

age = 12

if age > 10:
print("Older than ten")

That causes an error because Python expected an indented block.

Good habits:

  • use 4 spaces for each level of indentation
  • keep indentation consistent
  • do not mix tabs and spaces

To learn this rule in more detail, read Python indentation rules and why they matter. If indentation causes an error, see IndentationError: expected an indented block.

Colons start a code block

In Python, lines that start a block often end with a colon :.

Common examples include:

  • if
  • for
  • while
  • def
  • class

Example with if:

score = 90

if score > 50:
    print("Pass")

The pattern is:

  1. Write the block-starting line
  2. End it with a colon
  3. Put the block on the next line
  4. Indent the lines inside the block

Another short example:

for i in range(3):
    print(i)

If you forget the colon, Python will raise an error. For that specific problem, see SyntaxError: missing colon.

Comments are ignored by Python

A comment starts with #.

Example:

# This is a comment
print("Hello")

Comments are for humans reading the code. Python ignores them when the program runs.

You can use comments to:

  • explain what code does
  • leave notes for yourself
  • make code easier to understand later

Example:

name = "Sam"  # store a name
print(name)

Python runs the code, but ignores the comment.

If you want to learn more, see Python comments explained: single-line and multi-line.

Case sensitivity in Python

Python is case-sensitive. That means uppercase and lowercase letters are treated as different.

These are three different names:

  • name
  • Name
  • NAME

Example:

name = "Sam"
print(name)

This works.

But this does not:

name = "Sam"
print(Name)

Why? Because Name is not the same as name.

Wrong capitalization can lead to bugs or errors such as NameError. This also matters for Python keywords and built-in names. For example, print is correct, but Print is not.

To learn more about special words in Python, see Python keywords explained for beginners.

Strings, numbers, and names

In Python, text usually goes in quotes.

Example:

print("hello")

Here, "hello" is a string.

Variable names do not use quotes:

name = "hello"
print(name)

In this example:

  • "hello" is the text value
  • name is the variable name

Compare these two lines:

print("name")
print(name)

They are different:

  • print("name") prints the text name
  • print(name) prints the value stored in the variable name

Numbers also do not use quotes if you want them treated as numbers:

age = 12
print(age)

If you write "12" instead, that is text, not a number.

A small valid program

Here is a short program that combines several basic syntax rules:

name = "Lina"
age = 15

print("Checking age...")

if age >= 13:
    print(name)
    print("Teenager")

What this program uses:

  • variables: name and age
  • strings in quotes: "Lina" and "Checking age..."
  • a normal statement: print(...)
  • an if statement ending with a colon
  • indentation inside the if block

Expected output:

Checking age...
Lina
Teenager

You can use this as a quick syntax checklist:

  • one statement per line
  • quotes around text
  • no quotes around variable names
  • colon after if
  • indentation inside the block
  • correct capitalization

Common syntax problems beginners make

These are some of the most common beginner mistakes:

  • Missing the colon after if, for, while, or def
  • Forgetting to indent code after a colon
  • Mixing tabs and spaces
  • Leaving a quote, bracket, or parenthesis unclosed
  • Using the wrong capitalization in names like print, name, or True
  • Writing text without quotes when a string is intended

Examples of common problems:

Missing colon

age = 12

if age > 10
    print("OK")

Problem:

  • if age > 10 should end with :

Fixed version:

age = 12

if age > 10:
    print("OK")

Missing indentation

age = 12

if age > 10:
print("OK")

Problem:

  • the print line must be indented

Fixed version:

age = 12

if age > 10:
    print("OK")

Unclosed quote

print("Hello)

Problem:

  • the string starts with " but does not close with "

Fixed version:

print("Hello")

Wrong capitalization

Print("Hello")

Problem:

  • Print is not the same as print

Fixed version:

print("Hello")

If you are not sure what is wrong, these simple commands can help while learning:

python your_file.py

Runs your Python file so you can see the error message.

print("debug")

A quick way to check whether a line or block runs.

help(print)

Shows help for the print function.

type("hello")

Shows the type of a value. In this case, it returns str.

FAQ

What is Python syntax in simple words?

Python syntax is the set of writing rules Python uses to understand your code.

Why does indentation matter in Python?

Indentation shows which lines belong inside a block, such as an if statement or loop.

Do I always need a colon in Python?

No. You usually need a colon after statements that start a block, such as if, for, while, and def.

Is Python case-sensitive?

Yes. Lowercase and uppercase letters are treated as different.

Are comments part of Python syntax?

Yes, but Python ignores comments when running the code.

See also

Once these basic writing rules make sense, the next good topics are indentation, comments, and statements. After that, move on to variables and simple if statements.