Understanding Python Syntax
Python syntax refers to the set of rules that define how Python programs are written and interpreted. Python is designed to look clean and natural, reducing visual noise like braces and semicolons.
A Python statement usually represents a single instruction. Code is executed line by line, from top to bottom.
Example of Simple Python Syntax
print("Hello, Python")
x = 10
y = x + 5
There are no semicolons, no type declarations, and no block-ending symbols. This simplicity is intentional.
Indentation Rules in Python
Indentation is not optional in Python. It is how Python understands code structure.
Instead of using curly braces {}, Python uses indentation (whitespace) to define blocks of code.
Why Indentation Matters
- Defines code blocks
- Controls execution flow
- Improves readability
- Prevents ambiguous logic
Correct Indentation Example
if age >= 18:
print("Eligible to vote")
print("Access granted")
Incorrect Indentation Example
if age >= 18:
print("Eligible to vote")
The above code will raise an IndentationError because Python cannot determine the block structure.
Indentation Rules You Must Follow
- Use consistent indentation (spaces or tabs, not both)
- Recommended indentation is 4 spaces
- All statements in a block must align
Indentation and Control Structures
Indentation becomes critical in conditional statements, loops, and functions.
Indentation with Conditionals
if score >= 50:
print("Pass")
else:
print("Fail")
Indentation with Loops
for i in range(3):
print(i)
Indentation with Functions
def greet(name):
return "Hello " + name
What Are Python Keywords?
Python keywords are reserved words that have predefined meanings in the language. They cannot be used as variable names, function names, or identifiers.
Keywords define Python’s grammar and control logic.
Examples of Common Python Keywords
- if, else, elif
- for, while, break, continue
- def, return
- class
- try, except, finally
- True, False, None
Why Keywords Are Restricted
Keywords are reserved because Python needs them to understand program structure. Allowing them as identifiers would create ambiguity.
Invalid Keyword Usage Example
def = 10
This will cause a SyntaxError because def is a Python keyword.
What Are Identifiers in Python?
Identifiers are names used to identify variables, functions, classes, and objects in Python.
They allow humans to label and reuse data meaningfully.
Valid Identifier Examples
age user_name calculate_total StudentRecord
Rules for Naming Identifiers
- Must start with a letter or underscore (_)
- Cannot start with a number
- Can contain letters, digits, and underscores
- Cannot be a Python keyword
- Case-sensitive
Invalid Identifier Examples
2name total-amount class
Case Sensitivity in Identifiers
Python is case-sensitive. This means uppercase and lowercase names are different.
value = 10 Value = 20
These are two different identifiers.
Best Practices for Identifiers
- Use meaningful names
- Follow snake_case for variables and functions
- Use PascalCase for class names
- Avoid single-letter names except for counters
Keywords vs Identifiers Comparison
| Aspect Keywords Identifiers | ||
| Purpose | Define language structure | Name variables and objects |
| User-defined | No | Yes |
| Can be renamed | No | Yes |
Real-World Perspective
Poor indentation and unclear identifiers are among the most common causes of beginner frustration. Professionally written Python code relies heavily on clean indentation and meaningful naming to remain readable years later.
Summary
Python syntax is designed for clarity, and indentation is the backbone of its structure. Keywords define what Python understands, while identifiers define what humans control. Mastering these rules early ensures clean, readable, and error-free code. These fundamentals are not optional— they are the language itself.