Skip to content
Exercises · Q1

Q.Which of the following identifier names are invalid and why?

a) Serial_no.
b) 1st_Room
c) Hundred$
d) Total Marks
e) Total_Marks
f) total-Marks
g) _Percentage
h) True
Assam AhsecTextbookSubjective· 3mImportance★★★★★est
56% · 14/25 Questions
✓ Free question

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:

  1. Start character: Must be a letter (a-z, A-Z) or an underscore (_). Cannot start with a digit.
  2. Body characters: After the first character, can contain letters, digits (0-9), and underscores only. No spaces, hyphens, dollar signs, or special symbols.
  3. Reserved keywords: Cannot use a Python keyword (like True, False, if, for, etc.) as an identifier.
  4. Case-sensitive: Total_Marks and total_marks are 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.

Watch out

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.

Tip

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

IdentifierValid?Reason
Serial_no.NoContains dot (.)
1st_RoomNoStarts with digit
Hundred$NoContains $
Total MarksNoContains space
Total_MarksYesFollows all rules
total-MarksNoContains hyphen (-)
_PercentageYesStarts with underscore, valid
TrueNoReserved keyword
✓Final answer

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.