Python Identifiers and Reserved Words¶
Identifiers¶
In Python, a name used in a program is called an identifier. It can be the name of a:
- Class
- Function
- Module
- Variable
Example:
Rules for Defining Identifiers in Python¶
- Allowed Characters Identifiers can only contain:
- Alphabet symbols (a–z, A–Z)
- Digits (0–9)
- Underscore symbol
_
❌ Using any other symbol like $ will result in a syntax error.
- Should Not Start with a Digit
- Case Sensitivity Identifiers in Python are case-sensitive.
- Cannot Use Reserved Words Reserved words in Python cannot be used as identifiers.
-
No Length Limit There is no maximum length for an identifier, but it's best to avoid excessively long names.
-
Dollar Symbol Not Allowed
$is not permitted in Python identifiers. -
Identifiers Starting with Underscore
_identifier: Indicates private.__identifier: Indicates strongly private.-
__identifier__: Indicates a language-defined special name (also known as magic methods).Example:
Valid and Invalid Identifiers¶
| Identifier | Validity |
|---|---|
123total |
❌ Invalid |
total123 |
✅ Valid |
java2share |
✅ Valid |
ca$h |
❌ Invalid |
_abc_abc_ |
✅ Valid |
def |
❌ Invalid (reserved word) |
if |
❌ Invalid (reserved word) |
Reserved Words¶
In Python, certain words are reserved to represent specific meanings or functionalities. These are known as reserved words or keywords.
There are 33 reserved words in Python (as of version 3.x):
True, False, None,
and, or, not, is,
if, elif, else,
while, for, break, continue, return, in, yield,
try, except, finally, raise, assert,
import, from, as, class, def, pass, global, nonlocal, lambda, del, with
Notes:¶
- All reserved words contain only alphabet characters.
- All are in lowercase, except:
TrueFalseNone
Checking Reserved Words in Python¶
You can view the list of reserved words using the keyword module:
Output: