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, so `'42'` and `42` have different types and cannot be combined arithmetically.
- Integer literals can be written in decimal, binary (`0b`), octal (`0o`), or hexadecimal (`0x`); the value is still an `int`.
- String literals use single, double, or triple quotes and support escapes such as `\n`, `\t`, and raw `r'...'` strings.
- `True`, `False`, and `None` are keywords requiring exact capitalization; `None` is type `NoneType`.
Literal values
A literal is a value written directly in the program. When Python reads 25, it creates an int; when it reads '25', it creates a str of two characters. The two look similar to a person but behave very differently.
age = 25
text_age = '25'
active = True
missing = None
age + 1 is valid because age is an int. text_age + 1 raises TypeError because you cannot add a str and an int. You can check any value's type with type(x), which the exam expects you to predict.
| Literal | Type created | Notes |
|---|---|---|
12 | int | Whole number, unlimited precision |
12.0 | float | Has a decimal point |
'12' | str | Text, even though it holds digits |
True | bool | bool is a subclass of int |
None | NoneType | Absence of a value |
3 + 4j | complex | Imaginary part uses j |
Numeric literal forms
Python integer literals can use four numeral systems. The stored value is always an int; the prefix only tells Python how to read the written digits.
| Written form | Base | Decimal value |
|---|---|---|
10 | 10 | 10 |
0b1010 | 2 | 10 |
0o12 | 8 | 10 |
0xA | 16 | 10 |
Binary uses digits 0-1, octal uses 0-7, and hexadecimal uses 0-9 plus A-F (case-insensitive). An out-of-range digit is a SyntaxError, so 0b102 and 0o18 are illegal. The prefix letter is also case-insensitive: 0B, 0O, and 0X work, though lowercase is conventional.
Python allows underscores as digit separators for readability: 1_000_000 equals 1000000. They must sit between digits, so leading, trailing, or doubled underscores (_100, 100_, 1__0) are errors. A float literal can use scientific notation: 2.5e3 is 2500.0, and 1e-2 is 0.01.
The critical exam point: notation does not persist. After assignment, 0b1010, 0o12, 0xA, and 10 are identical integers, and a plain print() shows the decimal form 10 unless you format it (for example bin(10) gives '0b1010').
Strings and escape sequences
String literals use single or double quotes interchangeably; triple quotes (''' or """) span multiple lines. Escape sequences embed hard-to-type characters.
| Escape | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | A single backslash |
\' | A literal single quote |
\" | A literal double quote |
name = 'Ada'
message = "Line one\nLine two"
path_hint = r'C:\temp\notes'
In message, \n becomes a newline. The leading r in path_hint creates a raw string, which disables escape processing, useful for Windows paths and patterns. Note that len('\n') is 1, not 2, because the escape is a single character.
Booleans and None
True, False, and None are keywords with exact capitalization. true, false, and none are ordinary, unbound identifiers that raise NameError, a frequent beginner trap. Because bool subclasses int, True equals 1 and False equals 0 arithmetically, so True + True is 2.
None means no value or no object was provided. It is not the same as 0, False, or '', even though all four are "falsy" in conditions. A function with no return statement returns None.
Exam approach
Before calculating anything in a literal-heavy question, ask two things: what type does this exact spelling create, and is Python being asked to combine compatible types? That habit catches most literal traps, such as a quoted number sneaking into arithmetic, before they turn into a wrong answer.
Predicting type with type()
The exam frequently asks for the result of type(x) or relies on you knowing it implicitly. Memorize these: type(7) is int, type(7.0) is float, type('7') is str, type(True) is bool, and type(None) is NoneType. Mixed operations promote: type(2 + 3.0) is float, and type(4 / 2) is float because true division always yields a float, while type(4 // 2) is int. A common distractor offers int where the correct answer is float after a / operation.
Float precision and limits
Unlike integers, which have unlimited precision in Python, floats are binary approximations, so 0.1 + 0.2 displays as 0.30000000000000004, not 0.3. The exam rarely demands exact float arithmetic, but it may show such a result and ask which value is printed, testing whether you know floats are inexact. Integers, by contrast, never overflow in Python 3, so 2 ** 100 is computed exactly. Knowing that integer literals can grow arbitrarily large while float results may carry rounding artifacts is enough for entry-level questions.
Quick reference: falsy literals
For later condition questions, fix this list now. The values 0, 0.0, '', None, False, and any empty collection are falsy; every other value is truthy. Because None, 0, and '' are three distinct objects that merely behave as false, an equality test like None == 0 returns False even though both are falsy. The exam tests this distinction directly, so never treat "falsy" as "equal."
String literal details that trip candidates
A string delimited by single quotes can contain double quotes without escaping, and vice versa: 'She said "hi"' and "it's fine" are both legal one-line strings. To put the same quote character inside, escape it ('it\'s fine') or switch delimiters. Triple-quoted strings preserve embedded newlines literally, so a multi-line triple-quoted literal counts every line break in its length. Adjacent string literals are concatenated at compile time, so 'ab' 'cd' becomes 'abcd', an occasional distractor. None of these change the underlying type: the result is always str.
Float literals also have edge forms worth recognizing. A leading or trailing zero may be omitted around the decimal point in some notations, but Python requires a digit on at least one side, so .5 and 5. are both valid floats equal to 0.5 and 5.0. Scientific notation always produces a float, even when the value is whole: 1e3 is 1000.0, not 1000.
Choosing the right literal on the exam
When an answer option must produce a specific value and type, match both. If a question needs the integer ten written in hexadecimal, only 0xA qualifies; 0b10 is two and 0o10 is eight. If it needs the boolean true, only True with a capital T works. If it needs an empty value distinct from zero, only None fits. Reading the exact spelling of each literal, including prefix letters and capitalization, is the discipline that converts this section's rules into correct answers.
What is the decimal value of the Python literal 0b1101?
Which pair contains two values of DIFFERENT Python types?
Which literal represents the deliberate absence of a value in Python?