Python Syntax
Python syntax refers to the set of rules that defines how a Python program is written and interpreted. Unlike many other programming languages, Python emphasizes readability and uses indentation instead of curly braces ({}) to group blocks of code.
Python Indentation
Indentation (leading whitespace such as spaces or a tab) tells Python which statements belong to a block, such as inside an if statement, loop, or function.
if 10 > 5:
print("Ten is greater than five!")
print("This line is also inside the if block.")
If you skip indentation or mix inconsistent spacing in the same block, Python raises an IndentationError. Using 4 spaces per indentation level is the standard convention in Python.
Statements and Line Breaks
In Python, each instruction is typically written on its own line. Semicolons (;) at the end of a line are not required.
message = "Learning Python syntax"
count = 3
print(message)
print(count)
Key Syntax Rules to Remember
- Case Sensitivity: Variable and identifier names are case-sensitive—
name,Name, andNAMEare three different names. - Colons for Blocks: Headers like
if,for,while, anddefend with a colon (:) before the indented body. - Strings and Quotes: Text literals can use single quotes (
'...') or double quotes ("...").