Python Data Types and Structures
Python's built-in data types are the foundation of every data science project. Choosing the right structure affects performance, readability, and correctness. This lesson covers every major type you will use daily.
Python Data Type Hierarchy
Numeric Types
Integers
Integers are whole numbers with no decimal point. Python handles arbitrarily large integers without overflow.
Floats
Floats are decimal numbers. They use IEEE 754 double precision, which means ~15-17 significant digits.
Complex Numbers
Used in signal processing and physics simulations.
Booleans
Booleans are a subclass of integers. True equals 1 and False equals 0.
Strings
Strings are immutable sequences of Unicode characters. You will use them extensively for text processing in data science.
Lists
Lists are ordered, mutable sequences. They are the most versatile data structure in Python.
When to Use Lists
- You need an ordered collection that changes over time.
- You want to append, insert, or remove elements frequently.
- You need duplicate values.
- You want to iterate in insertion order.
Tuples
Tuples are ordered, immutable sequences. They are faster than lists and can be used as dictionary keys.
When to Use Tuples
- Data should not change (coordinates, RGB colors, database rows).
- You need a hashable type (dictionary keys, set elements).
- Performance matters – tuples are slightly faster than lists.
- You want to enforce immutability as a design constraint.
Dictionaries
Dictionaries store key-value pairs. They are the most important data structure for structured data work.
Nested Dictionaries
When to Use Dictionaries
- You need fast lookups by a unique key (O(1) average).
- You represent structured records or JSON-like data.
- You need to map one set of values to another.
- Data science: column-based data, feature dictionaries, configuration.
Sets
Sets are unordered collections of unique elements. They are optimized for membership testing and set operations.
When to Use Sets
- You need to remove duplicates quickly.
- You need fast membership testing.
- You need mathematical set operations (union, intersection, difference).
- You are comparing two collections for overlap.
Type Conversions
Type Conversion Rules
Type conversions follow implicit and explicit rules. Here are the mathematical representations:
Implicit Promotion Order:
Explicit Casting Rules:
Choosing the Right Structure
Need ordered + mutable? → List
Need ordered + immutable? → Tuple
Need key-value pairs? → Dict
Need unique values? → Set
Need fast lookup by key? → Dict
Need fast membership testing? → Set
Need to enforce no duplicates? → Set
Performance Comparison
Key Takeaways
- Lists are your default ordered collection; use them when you need mutability.
- Tuples protect data from accidental modification and work as dict keys.
- Dictionaries are essential for structured data and fast lookups.
- Sets are irreplaceable for deduplication and membership testing.
- Always choose the structure that best matches your data's constraints and access patterns.