Q.Which of the following identifier names are invalid and why?
Identifier validity in Python follows strict rules: must start with a letter or underscore, contain only letters/digits/underscores, and cannot be a reserved keyword. We check each name against these rules.
The Core Idea: Identifier Validity Rules
In Python (and most programming languages), an identifier is a name given to a variable, function, class, or other entity. The rules are not arbitrary — they exist so the compiler/interpreter can unambiguously parse your code. Python's rules are:
- Start character: Must be a letter (
a-z,A-Z) or an underscore (_). Cannot start with a digit. - Body characters: After the first character, can contain letters, digits (
0-9), and underscores only. No spaces, hyphens, dollar signs, or special symbols. - Reserved keywords: Cannot use a Python keyword (like
True,False,if,for, etc.) as an identifier. - Case-sensitive:
Total_Marksandtotal_marksare different identifiers.
These rules are universal across Python versions. Let's apply them to each candidate.
Checking Each Identifier
a) Serial_no. — Invalid. The dot (.) is not allowed in an identifier. Dots are used for attribute access (e.g., obj.attribute), not inside names.
b) 1st_Room — Invalid. Starts with a digit (1). Python cannot parse this — it would see 1 as a number literal, then st_Room as something else, causing a syntax error.
c) Hundred$ — Invalid. The dollar sign ($) is not a valid character in Python identifiers. (Some other languages like JavaScript allow it, but Python does not.)
d) Total Marks — Invalid. Contains a space. Spaces are delimiters in Python — Total Marks would be parsed as two separate tokens.
e) Total_Marks — Valid. Starts with a capital letter, contains only letters and an underscore. Perfectly fine.
f) total-Marks — Invalid. The hyphen (-) is the subtraction operator in Python. Writing total-Marks would be interpreted as total minus Marks, not a single name.
g) _Percentage — Valid. Starts with an underscore (allowed), followed by letters. Underscore-prefixed names are conventionally used for "private" or internal-use identifiers, but they are syntactically valid.
h) True — Invalid. True is a reserved keyword in Python (a boolean literal). You cannot use it as a variable name.
A common mistake is thinking True or False can be used as variable names because they look like regular words. They are keywords — the language reserves them for boolean values. Attempting True = 5 raises a SyntaxError.
To quickly check if a name is a Python keyword, use import keyword; print(keyword.iskeyword("True")) in the Python shell. It returns True for reserved words.
Summary Table
| Identifier | Valid? | Reason |
|---|---|---|
Serial_no. | No | Contains dot (.) |
1st_Room | No | Starts with digit |
Hundred$ | No | Contains $ |
Total Marks | No | Contains space |
Total_Marks | Yes | Follows all rules |
total-Marks | No | Contains hyphen (-) |
_Percentage | Yes | Starts with underscore, valid |
True | No | Reserved keyword |
The invalid identifiers are: a) Serial_no. (contains dot), b) 1st_Room (starts with digit), c) Hundred$ (contains $), d) Total Marks (contains space), f) total-Marks (contains hyphen), and h) True (reserved keyword). The valid ones are e) Total_Marks and g) _Percentage.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.