Fundamentals 26 min read

Why Python Uses Indentation, Handles Floats, and Other Design Decisions – A Comprehensive FAQ

This article explains Python's design choices such as mandatory indentation, floating‑point arithmetic quirks, immutable strings, the absence of switch statements, memory management, and many other language features, providing clear reasoning, code examples, and best‑practice recommendations for Python developers.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Why Python Uses Indentation, Handles Floats, and Other Design Decisions – A Comprehensive FAQ

1. Why use indentation to group statements?

Guido van Rossum considers indentation elegant and greatly improves code clarity because Python lacks explicit start/end brackets, so the parser’s grouping matches the human reader’s expectations.

<code>if (x <= y)
    x++;
    y--;
z++;</code>

Without brackets, the indented block can be misleading to C programmers, who may think only x++ runs when the condition is true.

2. Why do simple arithmetic operations sometimes give strange results?

Python’s float type stores values as C double , which cannot represent many decimal numbers exactly, leading to results like 1.2 - 1.0 == 0.19999999999999996 . This is a property of binary floating‑point arithmetic, not a Python bug.

<code>>> 1.2 - 1.0
0.19999999999999996</code>

3. Why are Python strings immutable?

Immutability allows fixed memory allocation and enables strings to be treated as basic values, similar to numbers, improving performance and simplifying hash‑based structures.

4. Why must "self" be explicit in method definitions and calls?

Explicit self makes it clear whether an attribute belongs to the instance or is a local variable, aids readability, and avoids the need for C‑style naming conventions.

5. Why can’t assignment appear inside expressions?

Allowing assignment in expressions would re‑introduce hard‑to‑detect bugs common in C (e.g., if (x = 0) ). Python forces the more explicit while True pattern or iterator‑based loops.

<code>while True:
    line = f.readline()
    if not line:
        break
    ...</code>

6. Why does Python lack a switch / case statement?

Python encourages if…elif…else chains or dictionary‑based dispatch tables, which are clear and flexible.

<code>dispatch = {'a': func_a, 'b': func_b}
func = dispatch[value]
func()</code>

7. Why does CPython not use a traditional garbage collector?

CPython relies on reference counting with a cyclic‑GC fallback because it integrates tightly with C’s memory allocation and is portable across platforms.

8. Why doesn’t CPython free all memory on exit?

Objects reachable from the global namespace or involved in reference cycles may not be reclaimed; tools like atexit can be used for explicit cleanup.

9. Why are there separate tuple and list types?

Tuples are immutable and suitable for fixed collections (e.g., coordinates), while lists are mutable arrays optimized for frequent modifications.

10. How are lists implemented in CPython?

Lists are dynamic arrays of object references; resizing is amortized O(1) and indexing is O(1).

11. How are dictionaries implemented in CPython?

Dictionaries are resizable hash tables; keys must be immutable so their hash values remain stable.

12. Why does list.sort() not return the sorted list?

Sorting in place avoids unnecessary copying; use sorted() when a new list is needed.

13. How can interfaces be specified in Python?

Abstract Base Classes ( abc ) and protocols in collections.abc allow runtime checks with isinstance and issubclass .

14. Why is there no goto ?

Exceptions can simulate jumps, but unrestricted jumps are considered harmful.

<code>class label(Exception):
    pass
try:
    ...
    if condition:
        raise label()
except label:
    pass</code>

15. Why can raw strings not end with a backslash?

An odd number of trailing backslashes would escape the closing quote, leaving the string unterminated.

16. Why does Python not have a property‑assignment with statement?

Dynamic typing makes it ambiguous whether a name refers to a local variable, global, or attribute, so such syntax would be unclear.

17. Why do if/while/def/class statements require a colon?

Colons improve readability and help editors determine block boundaries without deep parsing.

18. Why does Python allow a trailing comma in lists, tuples, and dicts?

Trailing commas simplify adding, reordering, and generating code, and prevent subtle bugs when a comma is accidentally omitted.

PerformanceprogrammingBest PracticesLanguage DesignInterpreter
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.