Fundamentals 28 min read

Python Frequently Asked Questions: Indentation, Float Precision, Immutability, and Core Language Design

This article answers twenty‑seven common Python questions covering why indentation is used for block grouping, the reasons behind surprising floating‑point results, the immutability of strings, the explicit use of self, the absence of assignment in expressions, and many other design choices that shape Python’s core behavior.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Frequently Asked Questions: Indentation, Float Precision, Immutability, and Core Language Design

01. Why use indentation to group statements?

Guido van Rossum believes indentation is elegant and greatly improves the readability of ordinary Python programs. Without explicit start/end brackets, the parser’s grouping matches the human reader’s expectations, avoiding ambiguities common in C‑style code.

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

Because there are no braces, Python code is less prone to accidental mis‑grouping, and functions tend to fit on a single screen, making code more concise.

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

See the next question.

03. Why is floating‑point arithmetic inaccurate?

Users often think a surprising result is a Python bug, but it is due to how the underlying platform handles floating‑point numbers.

CPython stores floats using the C double type, which has a fixed 53‑bit precision (about 15‑16 decimal digits). Many decimal numbers cannot be represented exactly in binary, leading to small rounding errors.

<code>>> 1.2 - 1.00 .19999999999999996
</code>

For example, the decimal 1.2 is stored as the binary fraction:

<code>1.0011001100110011001100110011001100110011001100110011 (binary)
</code>

which corresponds to the decimal value 1.19999999999999995559….

04. Why are Python strings immutable?

Immutability offers performance benefits because the memory layout can be fixed at creation, and strings behave like basic types such as numbers.

05. Why must "self" be used explicitly in method definitions and calls?

The explicit self makes it clear whether an attribute belongs to the instance or is a local variable, aids readability, and avoids the need for special syntax when calling overridden methods.

06. Why can’t assignment appear inside expressions?

Allowing assignment in expressions would re‑introduce subtle bugs common in C (e.g., writing if (x = 0) instead of if (x == 0) ). Python forces the assignment to be a separate statement.

07. Why are some operations methods (e.g., list.sort() ) while others are functions (e.g., len(list) )?

Methods read more naturally for operations that act on the object itself, while functions like len() clearly indicate they return a property of the argument.

08. Why is join() a string method rather than a list method?

The separator string’s join() method iterates over a sequence and inserts the separator between elements, which works for any iterable, not just lists.

09. How fast are exceptions?

When no exception is raised, try/except blocks have negligible overhead. Raising and catching an exception is comparatively expensive, so use if checks when missing keys are expected.

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

Python encourages if…elif…else chains; a switch proposal exists but has not reached consensus. A dictionary mapping can emulate switch behavior.

11. Can Python simulate threads without OS‑level threads?

Standard CPython requires C‑level thread support, but Stackless Python provides a redesigned interpreter that avoids OS threads.

12. Why can lambda expressions not contain statements?

Python’s syntax does not allow statements inside expressions; lambdas are meant as concise function literals, not full statements.

13. Can Python be compiled to machine code or other languages?

Cython compiles Python with optional type annotations to C extensions; Nuitka compiles to C++; VOC can target Java.

14. How does Python manage memory?

CPython uses reference counting plus a cyclic‑garbage collector. Other implementations (Jython, PyPy) use different strategies, which can affect portability.

15. Why doesn’t CPython use a traditional garbage collector?

Traditional GC libraries are not portable across all platforms, and embedding Python in other applications may require custom memory allocators.

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

Objects referenced from the global namespace or involved in reference cycles may not be reclaimed; the atexit module can be used to force cleanup.

17. Why are tuples and lists separate types?

Tuples are immutable collections suitable for fixed‑size records (e.g., coordinates), while lists are mutable arrays intended for homogeneous collections that change size.

18. How are lists implemented in CPython?

Lists are variable‑length arrays of object references; resizing is amortized O(1) by allocating extra space.

19. How are dictionaries implemented in CPython?

Dicts are resizable hash tables; keys are hashed with hash() , and lookups are O(1) on average.

20. Why must dictionary keys be immutable?

Mutable objects can change their hash value, breaking the hash table invariants. Immutable types like tuples can safely serve as keys.

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

Sorting in place avoids the overhead of copying large lists; use the built‑in sorted() function when a new list is needed.

22. How can interfaces be specified and enforced in Python?

Python 2.6 introduced the abc module for abstract base classes; isinstance() and issubclass() can check compliance. Testing frameworks (unittest, doctest) provide practical enforcement.

23. Why doesn’t Python have goto ?

Exceptions can simulate goto‑like jumps, but jumping into the middle of loops is discouraged and considered misuse.

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

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

25. Why isn’t there a with statement for attribute assignment?

Python’s dynamic typing makes it impossible to know at compile time which attribute is being assigned, unlike statically‑typed languages.

26. Why do if , while , def , and class statements require a colon?

The colon improves readability and helps editors determine where a block begins.

27. Why does Python allow a trailing comma in list, tuple, and dict literals?

Trailing commas make it easier to add, reorder, or comment out elements and avoid subtle bugs when a comma is accidentally omitted.

Memory ManagementPythonProgrammingdata structuresLanguage DesignFAQ
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.