CertGrid CertGrid
Concepts·Certified Entry-Level Python Programmer

Python Literals, Variables and Number Systems

PCEP objective 1.3 covers literals, variables and numeral systems, and it is the first objective with real traps in it. Binary, octal and hexadecimal literals all have prefixes and one of them changed in Python 3; `bool` is a subclass of `int`, so `True + True` is 2; and the rules for a legal variable name are asked as a spot-the-error question. Everything here is one line of code to check.

Language Fundamentals Guide 6 of 26 Beginner

Written against the versions above. Written against **Python 3.14**. Two things in this objective are Python 3 changes that older material gets wrong: octal is `0o17` and a bare leading zero is now an error, and underscores in numeric literals (`1_000_000`) have been legal since 3.6.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

Before you start

  1. Every literal type, and what each one produces

    A literal is a value written directly in the source. A variable is a name bound to a value. The distinction matters in questions phrased as "which of these is a literal?".

    Below is one literal of each type the syllabus can draw on, printed with the class it produces. repr() is used rather than str so quotes stay visible.

    bash Example session
    cat > ~/py/littypes.py <<'PY'for value in (1, 1.0, "1", True, None, 1j, (1,), [1], {1}, {"a": 1}):    print(repr(value).ljust(8), "->", type(value).__name__)PYpython3 ~/py/littypes.py1        -> int1.0      -> float'1'      -> strTrue     -> boolNone     -> NoneType1j       -> complex(1,)     -> tuple[1]      -> list{1}      -> set{'a': 1} -> dict

    Expected resultTen literals, ten type names - and True reports bool, not int.

    Success conditionYou can name the type any literal in the syllabus produces.

  2. Four numeral systems, three prefixes

    Integers can be written in binary, octal, hexadecimal or decimal. The prefixes are case-insensitive (0X1F works) and the letters in a hex literal are too.

    Three separate things are going on and questions mix them deliberately: literals in the source, converting a number to text in a given base, and parsing text from a given base.

    bash Example session
    cat > ~/py/numerals.py <<'PY'print("literals :", 0b1010, 0o17, 0x1f, 10)print("to text  :", bin(10), oct(15), hex(31))print("from text:", int("1010", 2), int("17", 8), int("ff", 16))PYpython3 ~/py/numerals.pyliterals : 10 15 31 10to text  : 0b1010 0o17 0x1ffrom text: 10 15 255

    Expected result10 15 31 10, then the prefixed strings, then 10 15 255.

    Success conditionYou can move between all four bases in both directions.

  3. The octal literal that used to work

    In Python 2, a leading zero meant octal: 017 was 15. Python 3 requires the 0o prefix and rejects the bare leading zero outright, because 017 looked far too much like the number seventeen.

    bash Example session
    python3 -c 'print(017)'  File "<string>", line 1    print(017)          ^SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers[exit 1]

    Expected resultSyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers.

    Success conditionYou can spot a Python 2 octal literal in a question.

  4. Float literals, and making long numbers readable

    A float literal needs either a decimal point or an exponent, and it can be surprisingly sparse: .5 and 5. are both valid.

    The exponent form uses e or E and always produces a float, even when the value is a whole number - 3e2 is 300.0, not 300.

    Underscores may be placed between digits purely for readability, in any base.

    bash Example session
    cat > ~/py/floatlit.py <<'PY'print(.5, 5., 1.0)print(3e2, 1E-3, 6.02e23)print(1_000_000, 0b1010_1010, type(1_000_000).__name__)PYpython3 ~/py/floatlit.py0.5 5.0 1.0300.0 0.001 6.02e+231000000 170 int

    Expected result0.5 5.0 1.0, then 300.0 0.001 6.02e+23, then 1000000 170 int.

    Success conditionYou can recognise every float literal form the syllabus uses.

  5. bool is a kind of int, and it shows

    bool is a subclass of int. True is 1 and False is 0, not merely convertible to them, and every arithmetic operator therefore accepts them.

    This is one of the highest-yield facts in objective 1.3, because it makes True + True a legal expression with a numeric answer.

    bash Example session
    cat > ~/py/boolint.py <<'PY'print(True + True)print(isinstance(True, int))print(int(True), int(False))print(True == 1, True is 1)PYpython3 ~/py/boolint.py/home/sysadmin/py/boolint.py:4: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?  print(True == 1, True is 1)2True1 0True False

    Expected result2, True, 1 0, then True False.

    Success conditionYou can predict the result of arithmetic on booleans.

  6. What makes a legal variable name

    The rules are short: letters, digits and underscores; not starting with a digit; not a keyword; case-sensitive. All four names below are legal, including the two that look questionable.

    bash Example session
    cat > ~/py/names.py <<'PY'_leading = 1trailing_ = 2with2digits = 3UPPER = 4print(_leading, trailing_, with2digits, UPPER)PYpython3 ~/py/names.py1 2 3 4

    Expected result1 2 3 4 - every one of those names is valid.

    Success conditionYou can tell a legal name from an illegal one without guessing.

  7. The two ways a name goes wrong

    A name starting with a digit fails at compile time, with an error message that is less obvious than you would expect.

    A name that is legal but never assigned fails at runtime, with a NameError - the distinction from guide 4, in the smallest possible example.

    bash Example session
    python3 -c '2var = 1'  File "<string>", line 1    2var = 1    ^SyntaxError: invalid decimal literal[exit 1]python3 -c 'print(counter)'Traceback (most recent call last):  File "<string>", line 1, in <module>    print(counter)          ^^^^^^^NameError: name 'counter' is not defined[exit 1]

    Expected resultSyntaxError: invalid decimal literal, then NameError: name 'counter' is not defined.

    Success conditionYou can name both failures and say which is found before the program runs.

  8. What the exam does with this objective

    Objective 1.3 is part of the 18% fundamentals block and produces a recognisable set of questions:

    "What is the value of 0x1f?" - convert. 31.

    "What does 017 print?" - a SyntaxError.

    "What is True + True?" - 2.

    "Which of these is not a valid variable name?" - look for a leading digit, a keyword, or a hyphen.

    "What type is 3e2?" - float.

    "What does bin(10) return?" - the string '0b1010', not the number.

    None of these needs anything memorised beyond the three prefixes, and all of them can be checked in one line at the prompt.

    guide 7 is next, and it is where the fundamentals block gets genuinely dense.

    bash
    rm -f ~/py/littypes.py ~/py/numerals.py ~/py/floatlit.py ~/py/boolint.py ~/py/names.py && ls -A ~/py

    Expected resultAn empty scratch directory.

    Success conditionYou can answer every question shape this objective produces.

Troubleshooting

Official sources