← Home

016. Literals

Literal values encode constants directly in source

016. Literals

RAM Manager — Step 1: Reading a Config File

Aryan’s RAM manager tool needs a config file so users can tune it without touching code. The config looks like this:

max_retries = 3
timeout = 30.0
app_name = RamManager
debug = True
empty =

Aryan writes a quick parse_setting() function to return each setting — but he stores every value as a string. That means parse_setting("max_retries") returns "3" (a string) instead of 3 (an integer). When the tool later does range(max_retries), it crashes with TypeError: 'str' object cannot be interpreted as an integer.

The lesson: Python literals encode type in their syntax. 3 is an int; "3" is a str. A config parser must return the right Python type, not a string copy of every value.


💡 Fun fact: Python’s True, False, and None are capitalized as a deliberate design choice from Python 2.3 (2003), when they were promoted from built-in names to language keywords. JSON uses lowercase true, false, null — a common source of confusion when parsing JSON data into Python.

⚠️ Watch out: The most common literals mistake is wrapping a value in quotes by accident — "3" and "True" look plausible but are strings, not an int and a bool. When building config dicts or data structures, always check whether your values have quotes around them.

🤔 Think about it: "None" (a string) and None (the null value) are completely different objects. What would happen if parse_setting("empty") returned "None" instead of None, and the caller did if result is None?

Learning objectives

  • Recognize and write integer, float, string, bool, and None literals
  • Understand that syntax determines the type of a literal
  • Avoid string literals where other types are intended

Key concepts

  • literals
  • int literal
  • float literal
  • bool literal
  • None

Try it

Concept detail

Literals are fixed values written directly in source code:

  • Integer: 42, -7, 0, 0xFF (hex), 0b1010 (binary), 0o77 (octal)
  • Float: 3.14, 2.0, 1e6, .5
  • String: “hello”, ‘world’, “”“triple”“”, r“raw\n“
  • Boolean: True, False (capital!)
  • None: None (capital!)
  • List: [1, 2, 3], Dict: {“a”: 1}, Tuple: (1, 2), Set: {1, 2, 3} The type of a literal is determined by its syntax, not its content. ‘42’ is always a str; 42 is always an int; 42.0 is always a float.

Solution

def parse_setting(key):
    settings = {
        "max_retries": 3,
        "timeout": 30.0,
        "app_name": "MyApp",
        "debug": True,
        "empty": None,
    }
    return settings.get(key)

Tests

def test_max_retries_is_int():
    val = parse_setting("max_retries")
    assert val == 3
    assert type(val) == int

def test_timeout_is_float():
    val = parse_setting("timeout")
    assert val == 30.0
    assert type(val) == float

def test_app_name_is_str():
    val = parse_setting("app_name")
    assert val == "MyApp"
    assert type(val) == str

def test_debug_is_bool():
    val = parse_setting("debug")
    assert val == True
    assert type(val) == bool

def test_empty_is_none():
    val = parse_setting("empty")
    assert val is None

Resources