Sequence
- A sequence is an ensemble (collection) of items, arranged in a specific order.
- Elements in a sequence can be accessed using index numbers.
- Sequences are ordered sets of elements, meaning their order is preserved.
- Common types of sequences include. Those are String, list, set, and Dictionary, etc.
- Sequences are widely used in Python for data manipulation and storage.
Mutable Objects:
Allow changes/updates at the same memory address (e.g., list, set, dict).
Mutable objects, such as list, set, and dict, can be modified in place, meaning operations like item assignment, addition, or removal update the object without creating a new one, preserving the same memory address (verified by id()).
- The id() remains unchanged for list, set, and dict, confirming in-place updates.
- Ties to dict (your prior query) and bytes (immutable, from your bytes query).
- Memory management with del
Examples of Mutable Objects: list, set, dict
The announcement lists list, set, and dict as mutable objects, which support operations like item assignment, addition, removal, or updates, all performed in place. This example showcases typical mutable operations for each type.
- Each type (list, set, dict) supports distinct mutable operations, all in-place.
- bytearray is included as another mutable type from your prior query.
- del is used in dict, reinforcing your del query.
- NoneType appears in function return, linking to your NoneType query.
Immutable Objects:
Do not allow changes/updates at the same address, modifications create new objects at different addresses.
When an immutable object (e.g., str, tuple, bytes) is modified, Python creates a new object at a different memory address rather than updating the original. This is verified by comparing ID () values before and after modification.
- The id() changes for str, tuple, and bytes, confirming new objects are created.
- The dict retains the same id(), showing mutability (ties to your dict query).
Do not allow item assignment (e.g., str, tuple, bytes, implied from course context).
Immutable objects prohibit modifying individual elements via assignment (e.g., obj[0] = value), raising a TypeError. This ensures the object’s contents remain fixed.
- str, tuple, and bytes reject item assignment, reinforcing immutability.
- bytearray allows assignment, showing mutability (connects to your bytearray query).
- bytes can be a dictionary key (immutable), but bytearray cannot (mutable).
Heterogeneous Data Types: Store elements of different data types:
Heterogeneous data structures like list, tuple, set, and dict (values) allow elements of various types (e.g., int, str, float, NoneType) in the same object. This flexibility is key for general-purpose data storage.
- list, tuple, set, and dict store mixed types, including NoneType (from your NoneType query).
- list and dict are mutable (ties to your mutable query); tuple is immutable.
- set requires hashable types (e.g., no lists), but still heterogeneous.
- del is used for dict, linking to your del query.
Homogeneous Data Types: Store elements of the same data type.
Homogeneous data structures like bytes, bytearray, and str restrict elements to a single type (or closely related). For bytes and bytearray, this is integers from 0 to 255; for str, it’s characters (Unicode in Python 3).
- bytes and bytearray (from your bytes and bytearray queries) are homogeneous, storing only integers (0-255).
- str is homogeneous (characters), immutable like bytes.
- bytearray is mutable, unlike bytes and str (ties to your mutable query).
- dict keys can be homogeneous (e.g., all bytes), linking to your dict query.
- del and NoneType are included for continuity.
Ordered Data Types:
- Maintain the insertion order of elements, meaning elements are stored and retrieved in the order they were added.
- Examples in Python (Python 3.7+): list, tuple, dict, bytes, bytearray.
- Ordered types are sequences (e.g., list, tuple) or collections that preserve order (e.g., dict since Python 3.7).
- Useful when order matters, such as in sequences or key-value mappings
Unordered Data Types:
- Do not maintain insertion order, meaning elements may appear in any order when iterated or accessed.
- Examples in Python: set, frozenset.
- Unordered types are typically used for membership testing or unique collections, where order is irrelevant.
- Note: In Python 3.6 and earlier, dict was unordered, but this changed in Python 3.7+.
- An iterable is any Python object capable of returning its elements one at a time, typically via a for loop, list comprehension, or other iteration constructs.
- Iterables implement the __iter__() method (returning an iterator) or __getitem__() (for sequence types, allowing indexed access).
- Examples: list, tuple, set, dict, str, bytes, bytearray, frozenset, and custom objects with __iter__() or __getitem__().
- Non-iterable examples (not covered in the course y
Key Characteristics:
- Iterables support iteration, enabling processing of elements sequentially.
- Iteration order depends on the data type: ordered (e.g., list, dict) or unordered (e.g., set).
- Iterables are central to Python’s data processing, used in loops, comprehensions, and functions like len(), zip(), or map().