Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
Python Keywords and Identifiers
Learn Python’s 33 reserved words and how to give clear, meaningful names to variables, functions, and classes.
Python Keywords
Python keywords are the building blocks of the language’s syntax. They are reserved words that cannot be used as identifiers such as variable or function names. Because they define the language’s structure, using a keyword incorrectly results in a syntax error.
Keywords are case‑sensitive and are listed exactly as they appear in the language. In Python 3.7 there are 33 of them, though newer releases may add a few more. For a definitive reference, see the official Python documentation.
All keywords are lowercase except True, False, and None. The table below lists each keyword with a brief example of its typical use.
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 |
While it may feel daunting to remember all 33 keywords, understanding their purpose is essential for writing clear, error‑free code.
Python Identifiers
An identifier is the name you give to classes, functions, variables, and other objects. It serves as a unique label that makes your code readable and maintainable.
Rules for Writing Identifiers
- Identifiers may contain letters (a–z, A–Z), digits (0–9), and the underscore
_. Valid examples includemyClass,var_1, andprint_this_to_screen. - They cannot begin with a digit;
1variableis invalid, whereasvariable1is acceptable. - Keywords cannot be used as identifiers.
Outputglobal = 1File "<interactive input>", line 1 global = 1 ^ SyntaxError: invalid syntax - Special symbols such as !, @, #, $, %, etc., are not allowed.
Outputa@ = 0File "<interactive input>", line 1 a@ = 0 ^ SyntaxError: invalid syntax - There is no practical limit to the length of an identifier.
Things to Remember
Python is case‑sensitive, so Variable and variable refer to different objects.
Choose descriptive names: c = 10 is syntactically correct, but count = 10 conveys intent more clearly and eases future maintenance.
For multi‑word identifiers, the underscore _ is the recommended separator, e.g., this_is_a_long_variable. Adhering to PEP 8 naming conventions further enhances code readability.
Python
- Understanding C# Keywords and Identifiers: Rules, Lists, and Best Practices
- C Keywords & Identifiers: A Practical Guide
- Python Statements, Indentation, and Comments: A Clear Guide
- Python Namespaces & Variable Scope: Understanding Names, Bindings, and Scopes
- Mastering Python Loop Control: break & continue
- Mastering Directory and File Operations in Python
- Python Regular Expressions (re Module) – A Practical Guide
- Mastering Python's strftime(): Convert Dates and Times to Readable Strings
- Master Python's strptime() for Accurate Date Parsing
- Mastering Python’s time Module: Functions, Structs, and Practical Examples