Valid Variable Names in Python
Which TWO of the following are valid Python variable names? (Choose two.)
⚠ Common exam trap
Python Institute often tests the rule that hyphens are invalid in variable names (tricking candidates who confuse them with underscores) and that keywords like 'class' cannot be used as identifiers, even though they look like valid names.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
my_var
Option B, my_var, is a valid Python identifier because it starts with a letter and contains only letters and underscores, which are permitted characters. Option D, _count, is also valid because a leading underscore is allowed in Python variable names and the rest of the name consists of legal characters. Option A, my-var, is invalid because hyphens are not allowed in identifiers and would be parsed as subtraction. Option C, 2nd_place, is invalid because identifiers cannot begin with a digit. Option E, class, is invalid because it is a reserved Python keyword and cannot be used as a variable name.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
my-var
Why it's wrong here
Hyphens are subtraction operators, so Python parses my-var as the expression my minus var, not one identifier. It tempts because hyphens are legal in identifiers elsewhere, such as CSS class names or Lisp symbols, but Python's grammar permits only letters, digits and underscores.
- ✓
my_var
Why this is correct
`my_var` is valid because Python identifiers may contain letters, digits and underscores, provided they do not begin with a digit. The underscore sits between letters, satisfying the stem's requirement for a legal variable name. It is also not a reserved keyword, so the interpreter accepts it without error.
- ✗
2nd_place
Why it's wrong here
Identifiers cannot begin with a digit, so 2nd_place fails tokenisation. It tempts because digits are permitted after the first character, making names like place2 or second_place valid, and the leading-digit rule is easy to overlook.
- ✓
_count
Why this is correct
The underscore prefix satisfies Python's identifier rules: a name may begin with a letter or underscore, followed by letters, digits or underscores. `_count` contains no spaces or hyphens and avoids reserved keywords, so it is a valid variable name.
- ✗
class
Why it's wrong here
class is a reserved keyword that begins class definitions, so it cannot name a variable. It tempts because it reads as an ordinary descriptive word, and Python's convention of lowercase names makes it look unremarkable beside valid choices.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.