Chapter 4. Data Types and Structures
Bad programmers worry about the code. Good programmers worry about data structures and their relationships.
Linus Torvalds
This chapter builds a modern foundation for working with Python’s core data types and containers. It shows how to choose between built-in types, avoid common pitfalls with mutability and copying, and write concise, readable transformations that prepare data for later chapters on NumPy, pandas, and beyond.
The chapter covers core scalars such as int, float, bool, and str; core
containers such as list, tuple, dict, and set; concise transformations with
comprehensions and unpacking; and copying fundamentals that help prevent subtle bugs. It
also shows how Python’s dynamic typing works, how type hints document intent, where
floating-point precision can surprise you, and when decimal is the safer tool.
To follow along, you need a working python 3.11+ interpreter (see
Chapter 3) and basic shell skills to run short scripts. No external packages ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access