Python Syntax
1Python Syntax: overview
Python syntax is the set of rules for writing code Python can understand: one statement per line, run from top to bottom, with indentation showing which lines belong together.
2Python Syntax: diagram
3Python Syntax: explained step by step
Every Python program is a plain text file full of instructions. When you press Run, Python reads the file from the top and carries out one instruction — called a statement — at a time, in order. Syntax is the set of spelling and layout rules those statements must follow so Python can understand them.
Your first program
print() shows whatever you put between its brackets. Text goes inside quotes, so Python knows it is text and not code. Numbers and calculations don't need quotes.
print("Hello, world!")
print("This line runs second.")
print(7 + 3)Each print() starts a new line of output. The quotes themselves are not shown — they only mark where the text begins and ends.
Comments are notes for people
Anything after a # on a line is a comment. Python skips it completely, so you can use comments to explain why the code does something, or to switch a line off while you experiment.
One statement per line
You don't need a semicolon at the end of a line: the line break tells Python the statement is finished. If a statement gets long, you may continue it on the next line as long as you are still inside brackets ( ), [ ] or { }.
Indentation groups lines into blocks
A line that ends with a colon : opens a block. The lines that belong to the block are indented, normally by 4 spaces. Python uses this indentation to decide which lines go together, so it is part of the syntax, not decoration.
temperature = 31
if temperature > 30:
print("It is hot today.")
print("Remember to drink water.")
print("Weather report finished.")Both indented lines belong to the if block, so they run only because the condition is true. The last line is back at the left edge, so it runs either way. (You will learn if properly later — for now, just notice the indentation.)
Python is case-sensitive
print, Print and PRINT are three different names to Python, and only the first one exists. The same applies to your own names: score and Score would be two separate things.
Reading a SyntaxError
Before running anything, Python checks the whole file. If it finds something it cannot understand, it stops with a SyntaxError and no line runs — not even the ones above the mistake. The message tells you where to look:
File "main.py", line 2
print("Goodbye)
^
SyntaxError: unterminated string literal (detected at line 2)
Read it from the bottom up: the last line names the problem (a piece of text that was never closed), line 2 says where, and the ^ points at the spot. If the reported line looks fine, check the line just above it — a missing bracket often shows up one line late.
Quick reference: Python Syntax
- Python Cheat Sheet (Python 3.13 Quick Reference) — This Python cheat sheet puts the most-used syntax for Python 3.13 into tables of tested one-liners: numbers, …
- How to Run a Python Script from the Command Line — Save your code in a file such as `hello.py`, open a terminal in that folder and run `python hello.py` (or `py…
4Python Syntax: syntax
print("Some text") # show text; quotes mark where it starts and ends
print("Total:", 42) # several values, separated by a space
# a comment # ignored by Python
if condition: # a colon opens a block...
indented_statement # ...4 spaces of indentation put a line inside it
next_statement # back at the left edge = outside the block
5Python Syntax: run the example
Press Run (or Ctrl + Enter). The output (step 6) appears directly below the code. Change anything and run it again.
# Print a short recipe card
print("Pancake recipe")
print("--------------")
servings = 4 # how many people are eating
print("Serves:", servings)
print("Cooking time:", 15, "minutes")
# Steps run in order, from top to bottom
print("1. Mix flour, eggs and milk")
print("2. Cook on a hot pan")
if servings > 2:
print("Tip: use a bigger bowl")
print("Tip: keep cooked pancakes warm")
print("Enjoy!")Expected outputPancake recipe
--------------
Serves: 4
Cooking time: 15 minutes
1. Mix flour, eggs and milk
2. Cook on a hot pan
Tip: use a bigger bowl
Tip: keep cooked pancakes warm
Enjoy!
7Python Syntax: how the example works
Hover or tap a step to highlight its lines in the example above.
- Line 1A comment. Python ignores everything after
#, so this line prints nothing — it is a note for whoever reads the code. - Line 2-3Each
print()shows its text on a new line. The quotes mark where the text starts and ends; they are not printed. - Line 4A blank line. Python ignores it, but it splits the program into readable chunks.
- Line 5Stores the number 4 under the name
servings(variables are the next lesson). The comment after the code is ignored too. - Line 6-7
print()can take several values separated by commas. It puts one space between them, so15and"minutes"come out as15 minutes. - Line 9-11Another comment, then two prints that run in the order they are written — top to bottom.
- Line 13The line ends with a colon, so a block must follow.
servings > 2is true, so Python runs the block. - Line 14-15Both lines are indented by 4 spaces, so they belong to the
ifblock and run together. - Line 16Back at the left edge, so this line is outside the block. It would run even if the condition were false.
8Python Syntax: common mistakes
Forgetting the closing quote
✗ Wrong
print("Hello, world!)✓ Right
print("Hello, world!")Every opening quote needs a matching closing quote of the same kind. Here the " before ) is missing, so Python thinks the text never ends.
Writing Print with a capital P
✗ Wrong
Print("Good morning")✓ Right
print("Good morning")Python is case-sensitive, and built-in names such as print are all lowercase. The end of the message even suggests the right spelling.
Forgetting to indent a block
✗ Wrong
apples = 5
if apples > 3:
print("Plenty of apples")✓ Right
apples = 5
if apples > 3:
print("Plenty of apples")A line that ends with : must be followed by at least one indented line. Indent the body by 4 spaces.
9Python Syntax: exercises
Write your answer, then press Check. The checks run in your browser.
1. Your first program
Write code Easy ✓ CompletedWrite a program that prints exactly these two lines:
Hello, Python!
I am learning to code.
Use one print() call for each line. Capital letters and punctuation must match.
# Write your two print() calls belowprint("Hello, Python!")
print("I am learning to code.")
2. Fix the shopping list
Fix the bug Easy ✓ CompletedThis program should print the three lines below, but it has two bugs. Run it, read the error, fix that line, then run again until it works.
Shopping list:
- apples
- bread
print("Shopping list:")
print("- apples)
Print("- bread")print("Shopping list:")
print("- apples")
print("- bread")
2 more exercises → 4 more examples
Questions learners ask
Do I need a semicolon at the end of each line in Python?
No. In Python the end of the line ends the statement. A semicolon is allowed if you want two statements on one line, but that is rarely done because it makes code harder to read.
How many spaces should I use to indent Python code?
Use 4 spaces per level — almost all Python code follows this convention. Tabs also work as long as you never mix them with spaces in the same block; mixing them causes a TabError, which is why most editors insert spaces when you press Tab.
Why does my program print nothing when the mistake is on the last line?
Python checks the syntax of the whole file before running any of it, so a SyntaxError anywhere stops everything. Other errors, such as a NameError from a misspelled name, happen while the program runs — so the lines above them do run and print first.
Further reading
10Next lesson
Up nextPython CommentsA comment is text after a # that Python ignores, used to explain why code does something, leave reminders, or temporari…Related:
Python VariablesPython Input and OutputPython if, elif and elsePython Exceptions: try, except, else and finally
← What Is Python? · All Python Basics lessons · Practise in the IDE