PCEP-30-02 · Question #146
Which of the following variable names are illegal and will cause the SyntaxError exception? (Choose two.)
The correct answer is C. for D. in. In Python, for and in are reserved keywords - words that are part of the language's syntax and cannot be reassigned as variable names. Attempting to do so raises a SyntaxError immediately, because the parser interprets them as language constructs, not identifiers. print (A) is…
Question
Options
- Aprint
- BIn
- Cfor
- Din
How the community answered
(48 responses)- A10% (5)
- B4% (2)
- C85% (41)
Explanation
In Python, for and in are reserved keywords - words that are part of the language's syntax and cannot be reassigned as variable names. Attempting to do so raises a SyntaxError immediately, because the parser interprets them as language constructs, not identifiers.
print (A) is a built-in function, not a keyword, so Python allows you to rebind it as a variable (e.g., print = 5 is legal, just a bad idea). In (B) is safe because Python is case-sensitive - only the lowercase in is reserved; In is treated as a regular identifier.
Memory tip: Run import keyword; print(keyword.kwlist) in a Python shell to see every reserved word. Keywords are all lowercase (except True, False, None) and cannot be variable names - built-ins like print, len, or list are fair game syntactically, even if overwriting them is poor practice.
Community Discussion
No community discussion yet for this question.