4.10 Literals
Numeric, string, boolean, None, and collection literals in Python — values written directly in source code, with type() examples for each.
A literal is a value written directly in source code, as opposed to one computed at runtime — 42, "hello", and True are all literals.
Numeric Literals
>>> type(10) # int
<class 'int'>
>>> type(10.5) # float
<class 'float'>
>>> type(1 + 2j) # complex
<class 'complex'>
String Literals
Covered in depth in Chapter 9: Strings — single, double, and triple quotes all produce str literals.
>>> type("hello")
<class 'str'>
Boolean Literals
>>> type(True)
<class 'bool'>
None
What Is It?
Python’s explicit “no value” placeholder — its own type, distinct from 0, False, or an empty string.
Why Is It Used?
To represent the deliberate absence of a value, e.g. a function that has nothing meaningful to return.
>>> type(None)
<class 'NoneType'>
Collection Literals
>>> type([1, 2]) # list
<class 'list'>
>>> type((1, 2)) # tuple
<class 'tuple'>
>>> type({1, 2}) # set
<class 'set'>
>>> type({"a": 1}) # dict
<class 'dict'>
Quick Interview Answer
“A literal is a value written directly into source code rather than computed — numeric (
int,float,complex), string, boolean (True/False),None, and the collection literals[],(),{}for list/tuple/set/dict.Noneis its own distinct type,NoneType— not the same as0,False, or"".”
Common Mistakes
- Treating
None,0,False, and""as interchangeable — they’re all “falsy” in a boolean context, butNonespecifically represents the absence of a value, not a zero or empty one. - Writing
{}expecting an empty set —{}is actually an emptydict; an empty set requiresset().
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form