Courseiva

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

Which of the following is an invalid variable name in Python?

⚠ Common exam trap

The PCEP exam often tests the misconception that hyphens are acceptable in variable names because they appear in other languages (like JavaScript object keys) or in file names, but Python strictly prohibits them in identifiers.

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

'my-var' contains a hyphen, which is not allowed in Python variable names. Python identifiers can only include letters, digits, and underscores, and cannot start with a digit. The hyphen is interpreted as a minus operator, making 'my-var' a syntax error.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    _myVar

    Why it's wrong here

    A leading underscore is legal in Python identifiers, so _myVar is valid, not invalid. It is tempting because underscore-prefixed names conventionally signal private or internal use, and a single underscore alone is reserved in interactive sessions, but neither rule makes this name invalid.

  • ✗

    myVar

    Why it's wrong here

    myVar is a syntactically valid identifier: it starts with a letter and contains only letters and digits. It is tempting to reject it because camelCase is discouraged by PEP 8, but style guidance governs naming conventions, not validity, so the interpreter accepts it.

  • ✓

    my-var

    Why this is correct

    Hyphens are parsed as the subtraction operator, so `my-var` reads as `my` minus `var` rather than a single identifier. Python identifiers permit letters, digits and underscores only, and cannot begin with a digit. The stem asks for an invalid name, which this satisfies.

  • ✗

    my_var

    Why it's wrong here

    my_var is a valid Python identifier: it starts with a letter, contains only letters, digits and underscores, and is not a reserved keyword. It is tempting to reject it because of the underscore, but underscores are permitted within identifiers; names beginning with a digit, such as 2var, are the invalid ones.

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 →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which TWO of the following are valid variable names in Python?

easy
  • ✓ A._myVar
  • B.my Var
  • C.my-var
  • ✓ D.my_var
  • E.2ndPlace

Why A: Option A (_myVar) is correct because Python identifiers may begin with an underscore, and underscores are legal characters anywhere in a variable name. Option D (my_var) is correct because underscores are valid within identifiers, making my_var a standard snake_case variable name. Option B (my Var) is invalid because it contains a space, which is not permitted in a Python identifier. Option C (my-var) is invalid because the hyphen is not a legal character in identifiers (it is interpreted as the subtraction operator). Option E (2ndPlace) is invalid because a Python identifier cannot begin with a digit.

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.