Skip to content
Laviyx Learn

Try

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.

Beginner6 min readUpdated
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:

Python
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:

Python
age = 20
if age >= 18:
    print("Adult")
    print("Can vote")
print("Done")
Output
Adult
Can vote
Done

The 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:

Python
if True:
print("no indent")
Output
  File "example.py", line 2
    print("no indent")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 1

Blocks 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:

Python
name = "Asha"
Name = "Ravi"
print(name, Name)
Output
Asha Ravi

Keywords 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:

Python
total = (10 +
         20 +
         30)
print(total)
Output
60

Python 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:

Python
import keyword
print(keyword.kwlist[:10])
print(len(keyword.kwlist))
Output
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class']
35

The 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:

Kind of nameStyleExample
variables and functionssnake_casetotal_price, get_user()
classesPascalCaseBankAccount
constantsUPPER_CASEMAX_SIZE

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:

Python
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
Python
number = 7
if number > 0:
    print("Positive")
print("Finished")
Output
Positive
Finished
  1. If must be lowercase if.
  2. The if line needs a colon.
  3. print("Positive") must be indented to belong to the if.

Key takeaway#

Finished this lesson?