1.2 Identifiers, Keywords, Comments, and Basic Program Structure
Key Takeaways
- Python identifiers contain letters, digits, and underscores, must not start with a digit, and are case-sensitive.
- There are 35 reserved keywords in Python 3.x (such as `if`, `while`, `def`, `return`, `True`, `False`, `None`) that cannot be ordinary names.
- A `#` starts a comment outside strings; a docstring is a string literal used as documentation, not a comment.
- Python scripts run top to bottom, so a name must be bound before executing code reads it or a `NameError` is raised.
- PCEP items check whether a name is valid, whether a word is reserved, and whether a variable is used before assignment.
Identifiers name objects
An identifier is a name for a variable, function, parameter, class, or module. Legal Python identifiers contain only letters, digits, and underscores; they may not begin with a digit; and they are case-sensitive, so total, Total, and TOTAL are three different names. There is no length limit, and Unicode letters are technically allowed, but PCEP snippets stick to ASCII.
student_count = 18
Student_count = 19
_private_note = 'draft'
The first two are distinct names because s and S differ to Python. This matters on the exam because a snippet may assign score and later print Score, a different, unbound name that raises NameError.
| Candidate name | Valid? | Reason |
|---|---|---|
total_2 | Yes | Letter start, allowed characters |
_ready | Yes | Leading underscore is allowed |
2_total | No | Starts with a digit |
unit-price | No | Hyphen is parsed as minus |
class | No | Reserved keyword |
my var | No | Space is not allowed in a name |
Keywords are reserved
A keyword has a fixed grammatical role and cannot be used as an ordinary name. Python 3.x defines 35 keywords. You can list them at runtime with import keyword; print(keyword.kwlist).
| Group | Keywords |
|---|---|
| Values | True, False, None |
| Boolean logic | and, or, not, in, is |
| Control flow | if, elif, else, for, while, break, continue, pass |
| Functions/scope | def, return, lambda, global, nonlocal, yield |
| Exceptions | try, except, finally, raise, assert |
| Other | import, from, as, class, with, del, async, await |
When Python sees if, it expects a conditional, not a custom variable. If a PCEP option suggests assigning to a keyword, for example True = 5, eliminate it immediately as a SyntaxError. Note that built-in names such as print, int, and list are not keywords; you can (unwisely) reassign them.
Comments and docstrings
A comment begins with # and runs to the end of that physical line, unless the # is inside a string. Comments are ignored at execution. Python has no dedicated multi-line comment syntax; programmers stack # lines.
# Convert minutes into full hours.
hours = total_minutes // 60
label = 'room #4'
The first line is a comment; the # in 'room #4' is part of the string literal. A docstring is different: it is a string literal placed as the first statement of a module, function, class, or method. It is not a comment because Python keeps it as the object's __doc__ value.
Basic program structure and order of execution
Small PCEP snippets run as scripts, top to bottom. Assignment creates or rebinds a name. A def statement creates a function object, but the body does not run until the function is called.
def double(n):
return n * 2
value = double(5)
print(value) # 10
Python defines double, then calls it, then prints 10. If the call appeared before the def executed, the name would be unbound and Python would raise NameError. Larger programs usually order imports first, then constants, helper functions, and main actions, but the exam only requires that you respect execution order.
Exam reading checklist
On name-focused questions, run this list:
- Does the identifier start with a letter or underscore (never a digit)?
- Does it avoid the 35 reserved keywords?
- Is the capitalization identical everywhere the name appears?
- Is the name bound before execution reaches the line that reads it?
- Is a
#starting a real comment, or is it inside quotes and part of a string?
These rules are small but underpin every later topic, so missing one here cascades into wrong answers on loops and functions.
Conventions versus rules
PCEP distinguishes what Python requires from what programmers merely prefer. A name like MAX_SPEED in all capitals is a convention signaling a constant, but Python does not enforce immutability; you can still reassign it. A leading single underscore such as _helper signals "internal use" by convention only. A name with two leading underscores inside a class triggers name mangling, which is beyond PCEP scope but is why the exam keeps to ordinary names. When a question asks whether code runs, judge it by the hard rules (legal characters, no keyword clash, defined before use); when it asks about style, apply the conventions.
Confusing the two leads candidates to mark legal code as an error.
Finally, note that comments and docstrings never change execution: removing every # comment from a snippet produces identical output. The exam sometimes inserts a misleading comment that contradicts the code; trust the executable statements, not the comment, when predicting results.
Variables are references, not boxes
A Python variable is a name bound to an object, not a fixed storage box. Assignment x = 5 creates an integer object and points the name x at it; a later x = 'hi' simply rebinds x to a new string object, and the type of x changes accordingly. This is why Python is dynamically typed: a single name may refer to an int on one line and a str on the next without any declaration. The exam may show n = 3 then n = 'three' and ask the final type, which is str.
Because names are references, two names can point at the same object, but for the immutable scalars in this chapter (int, float, str, bool, None) rebinding one name never affects another.
Deleting and rebinding names
The del statement removes a binding, so after del x the name x is unbound and reading it raises NameError again. PCEP rarely tests del deeply, but recognizing that a name can cease to exist reinforces the central idea: a variable exists only from the point it is assigned until the program ends or the name is deleted. Reading a name in any window where it is not bound is an error, and tracing that window precisely is the core skill this section builds toward the rest of the exam.
Which of the following is a valid Python identifier?
Code assigns score = 9 and then a later line evaluates print(Score). What happens?
In the line label = 'room #4', what is the role of the # character?