Python syntax defines how Python code should be written and formatted. If the syntax is incorrect, the Python interpreter will raise errors.
| Rule | Explanation | Example |
|---|---|---|
| Indentation is required | Python uses spaces/tabs instead of braces {} |
|
| Case-sensitive | Name and name are different |
|
| Statements end with newline | No semicolon (;) required |
print("Hello") |
| Comments | Start with # |
# This is a comment |
| Variable declaration | No var keyword, just assignment |
x = 10 |
Keywords are special reserved words that cannot be used as variable or function names. They are part of the Python language syntax.
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
To list all keywords using code:
import keyword
print(keyword.kwlist)
Trying to use a keyword as a variable name results in an error:
def = 10 # ❌ Invalid: 'def' is a reserved keyword
✅ Correct way:
my_def = 10