1.3 Literals, Types, and Numeral Systems
Key Takeaways
- A literal is a value written directly in source code, such as `42`, `3.5`, `'Python'`, `True`, or `None`.
- Quoted digits are strings, not numbers, so `'42'` and `42` have different types and behavior.
- Integer literals can be written in decimal, binary with `0b`, octal with `0o`, or hexadecimal with `0x`.
- String literals may use single quotes, double quotes, triple quotes, and escape sequences such as `\n`.
- The `bool` values `True` and `False` and the null value `None` must be written with exact capitalization.
Literal values
A literal is a value written directly in the program. When Python reads 25, it creates an integer value. When it reads '25', it creates a string containing two characters. The characters look similar to a person, but Python treats the values differently.
age = 25
text_age = '25'
active = True
missing = None
PCEP snippets often depend on this distinction. age + 1 is valid if age is an integer. text_age + 1 is not valid because it tries to add a string and an integer.
| Literal | Type created | Notes |
|---|---|---|
12 | int | Whole number |
12.0 | float | Decimal-point number |
'12' | str | Text, even though it contains digits |
True | bool | Boolean true value |
None | NoneType | Absence of a value |
Numeric literal forms
Python integer literals can use several numeral systems. The stored value is still an int; the prefix only tells Python how to interpret the written digits.
| Written form | Base | Decimal value |
|---|---|---|
10 | 10 | 10 |
0b1010 | 2 | 10 |
0o12 | 8 | 10 |
0xA | 16 | 10 |
Binary uses only digits 0 and 1. Octal uses 0 through 7. Hexadecimal uses 0 through 9 plus letters A through F or a through f. Invalid digits for the base produce a syntax error, such as trying to write 0b102.
Python also allows underscores inside numeric literals for readability: 1_000_000 is the same integer as 1000000. Underscores cannot be placed anywhere; they must separate digits in a legal numeric form.
For the exam, remember that the notation does not stay attached to the value. After assignment, 0b1010, 0o12, and 10 are just integers with the same value. If a later print() displays the variable normally, Python shows the decimal representation unless formatting code says otherwise.
Strings and escape sequences
String literals can use single or double quotes. Triple-quoted strings can span multiple lines. Escape sequences let a string contain characters that are difficult to type literally.
name = 'Ada'
message = "Line one\nLine two"
path_hint = r'C:\temp\notes'
In message, \n means newline. In path_hint, the leading r creates a raw string literal, which reduces escape processing and is useful for paths or patterns. PCEP questions usually emphasize common escapes rather than advanced string rules.
Booleans and None
True, False, and None are keywords with exact capitalization. true, false, and none are ordinary identifiers, not the built-in values. This is a common beginner mistake.
None means no value or no object has been provided. It is not the same as 0, False, or an empty string, even though all of those can behave as false in conditions.
Exam approach
When a question includes a literal, ask two questions before calculating anything:
- What type does this exact spelling create?
- Is Python being asked to combine compatible types?
That habit catches most literal traps before they become arithmetic, comparison, or function-call mistakes.
What is the decimal value of the Python literal 0b1101?
Which pair contains two values with different Python types?
Which literal represents the absence of a value in Python?