Python course menu
Python Syntax Basics: Indentation, Case Sensitivity, Statements and Keywords
Learn the rules of Python syntax: indentation and code blocks, colons, case sensitivity, one statement per line, line continuation, keywords, and naming conventions, with examples of common errors.
On this page
Syntax is the set of grammar rules a language follows. Python's syntax is famously clean: there are no semicolons at the end of lines and no curly braces around blocks. Instead, Python relies on indentation and a few simple rules, which this lesson covers.
One statement per line#
A statement is one instruction. In Python, each statement usually sits on its own line, with no semicolon:
name = "Asha"
age = 21
print(name, age)You can put several statements on one line with semicolons (x = 5; y = 10; print(x + y)), but it is considered poor style.
Indentation defines blocks#
Many languages group lines with { }. Python uses indentation: the spaces at the start of a line. A line ending in a colon : starts a block, and every line of that block is indented by the same amount:
age = 20
if age >= 18:
print("Adult")
print("Can vote")
print("Done")Adult
Can vote
DoneThe two indented lines belong to the if. print("Done") is back at the left edge, so it runs regardless of the condition.
Forgetting to indent is an error:
if True:
print("no indent") File "example.py", line 2
print("no indent")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 1Blocks appear after if, else, for, while, def, class, with and try. You will meet each of them in this course.
Python is case-sensitive#
name, Name and NAME are three different names:
name = "Asha"
Name = "Ravi"
print(name, Name)Asha RaviKeywords and built-in functions are lowercase (print, if, for), except True, False and None, which start with a capital letter.
Long lines#
Lines longer than about 80 to 100 characters are hard to read. Inside parentheses, brackets or braces, you can break a line freely:
total = (10 +
20 +
30)
print(total)60Python knows the statement is not finished until the ( is closed. A backslash \ at the end of a line also continues it, but parentheses are clearer and preferred.
Keywords#
Python reserves some words for its own syntax. You cannot use them as variable names:
import keyword
print(keyword.kwlist[:10])
print(len(keyword.kwlist))['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class']
35The full list includes def, for, if, import, in, return, while, and more. Writing class = "A" causes a SyntaxError.
Naming conventions (PEP 8)#
Python's official style guide, PEP 8, recommends:
These are conventions, not rules, but following them makes your code look familiar to every Python developer.
Whitespace inside lines#
Spaces inside a line are mostly up to you: x=1+2 and x = 1 + 2 work the same. PEP 8 recommends spaces around = and operators, and after commas, for readability.
Common mistakes#
Practice#
Practice exercise
This program has three syntax mistakes. Fix them so it prints Positive and then Finished:
number = 7
If number > 0
print("Positive")
print("Finished")Show hint
Check the keyword's capitalisation, the end of the if line, and which line belongs inside the block.
Show solution
number = 7
if number > 0:
print("Positive")
print("Finished")Positive
FinishedIfmust be lowercaseif.- The
ifline needs a colon. print("Positive")must be indented to belong to theif.
Key takeaway#
Finished this lesson?