Python supports integer literals written in decimal, binary, octal, and hexadecimal notation. The prefix tells the interpreter which number base to use.
Understanding the Concept
Ordinary decimal values use digits 0–9. Binary uses prefix 0b and digits 0–1; octal uses 0o and digits 0–7; hexadecimal uses 0x and digits 0–9 plus A–F. These are different textual representations of integer values, not different numeric types. Bases are useful for permissions, bit patterns, binary formats, and technical protocols. When printing an integer normally, Python usually displays its decimal representation.
When and Why You Would Use This
Compare four literals that all represent the same value. Note the leading zero and letter on the nondecimal forms: those are part of the literal syntax.
Practical Example
decimal_number = 26
binary_number = 0b11010
octal_number = 0o32
hex_number = 0x1A
print(decimal_number, binary_number, octal_number, hex_number)
Understanding the Example
The output is 26 26 26 26. Python interprets each literal according to its prefix and then stores an ordinary int object.
Example Output
26 26 26 26
Try It Yourself
Represent decimal 15 as binary, octal, and hexadecimal literals, then print all four.
Common Beginner Mistakes
- Typing digits outside the chosen base, such as 0b102 or 0o89.
- Thinking binary and hexadecimal literals create separate types from int.
- Confusing uppercase A–F hexadecimal digits with variable names.
Key Takeaways
- Base prefixes control how integer literals are interpreted.
- All these literals produce int values.
- Printing an int normally uses decimal notation.