Skip to content

Module 2 · Python Syntax and Fundamentals — topic 1 of 14

Python syntax

Syntax is the set of grammar rules a Python program must follow. Learn the core rules: one statement per line, colons and indentation for blocks, case sensitivity and matching brackets.

Beginner 12 min read·Lesson 15 of 306 in Python Programming

Learning objectives

  • Explain what syntax means in a programming language
  • Apply Python’s core grammar rules
  • Recognise code that breaks those rules

Definition

Syntax is the set of grammar rules a Python program must follow. Learn the core rules: one statement per line, colons and indentation for blocks, case sensitivity and matching brackets.

Introduction

Every language has grammar. In English, “cat the sat mat on” uses real words but makes no sense. Programming languages are far stricter: if the grammar is wrong, the computer refuses to run the program at all. Python’s grammar rules are called its syntax.

The good news is that Python’s syntax is small and consistent. A handful of rules cover almost everything you will write in this course.

Why it matters

Most errors beginners meet in their first weeks are syntax errors: a missing colon, an extra space at the start of a line, or a bracket that is never closed. Knowing the rules up front turns these from mysteries into quick fixes.

Detailed explanation

Rule 1: normally one statement per line. Python reads the end of a line as the end of the statement, so you don’t need semicolons.

Rule 2: a line that introduces a block, such as if, for, while or def, ends with a colon. The lines that belong to the block are indented underneath it.

Rule 3: Python is case-sensitive. total, Total and TOTAL are three different names, and keywords such as if must be lower-case.

Rule 4: brackets (), [] and {} and quotes must be balanced. Inside brackets a statement may continue onto the next line, which is useful for long lists.

Rule 5: spaces inside a line are mostly optional (x=1 and x = 1 are the same), but spaces at the start of a line are meaningful because they set the indentation level.

Syntax

Python
statement
if condition:
indented_statement
next_statement
  • Each statement sits on its own line.
  • The colon after the condition opens a block.
  • Indented lines belong to the block; the next unindented line ends it.

Code examples

Three statements, three lines

Python
city = "Pune"
year = 2026
print(city, year)
Output
Pune 2026

Case matters

Python
score = 10
Score = 99
print(score)
print(Score)
Output
10
99

score and Score are separate variables.

More examples

A long statement split inside brackets

Python
subjects = [
"Maths",
"Physics",
"Chemistry",
]
for subject in subjects:
print("Studying", subject)
print("Total subjects:", len(subjects))
Output
Studying Maths
Studying Physics
Studying Chemistry
Total subjects: 3

Step-by-step explanation

  • The opening [ lets the list continue across lines until the closing ].
  • The trailing comma after "Chemistry" is allowed and makes adding items easier.
  • for ... : opens a block; the indented print runs once per subject.
  • The final print is not indented, so it runs once after the loop.

Expected output

Running “A long statement split inside brackets” prints exactly:

TextOutput
Studying Maths
Studying Physics
Studying Chemistry
Total subjects: 3

Practical applications

  • Code formatters such as Black rely on these rules to lay out code consistently across teams.
  • Configuration lists in real projects are written one item per line inside brackets so changes are easy to review.
  • Editors highlight broken syntax instantly because the rules are simple and strict.

Best practices

  • Write one statement per line.
  • Close every bracket and quote as soon as you open it, then fill in the middle.
  • Use lower-case names consistently to avoid case mix-ups.

Common mistakes

Missing colon

Avoid

if score > 50
print("pass")

Prefer

if score > 50:
print("pass")

Lines that start a block must end with a colon; without it Python reports “expected ':'”.

Unclosed bracket

Avoid

print("total", 5

Prefer

print("total", 5)

Python keeps reading the next lines looking for the closing bracket and reports an error, often on a later line.

Key takeaways

  • Syntax is Python’s grammar; breaking it stops the program from running.
  • Blocks start with a colon and are indented.
  • Python is case-sensitive and brackets must balance.

Try it yourself

  1. 1
    Write a list of four fruits split across several lines, then print how many there are.
  2. 2
    Find and fix the error: for n in [1, 2, 3] print(n)
    Show hint

    Look at the end of the first line.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

10 questions cover the “Python Syntax and Fundamentals” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat happens when this code runs?
  • BeginnerWhich line is a comment in Python?
  • BeginnerWhich of these is a Python keyword?
Practise this module