- Lists are mutable, meaning you can modify them after creation by adding, removing, or changing elements.
- Lists have dynamic sizing, automatically resizing as elements are added or removed, unlike fixed-size arrays in other languages.
- Lists can store heterogeneous data types, including integers, strings, and objects, offering great flexibility.
- Lists maintain a defined order, with elements arranged in a specific sequence that remains consistent unless explicitly changed.
- Elements are accessed via zero-based indexing, where the first element is at index 0, the second at index 1, and so on.
- Negative indexing is supported, allowing access to elements from the end of the list, with -1 referring to the last element.
- Lists can contain duplicate values, enabling the same value to appear multiple times within the same list.
- The list() function can be used to create lists, either empty or by converting other iterables like strings or tuples.
- You can access sublists using slicing notation, specifying start, stop, and step indices within square brackets (e.g., list[start:stop]).
- List comprehensions provide a concise way to create lists, using an expression and for-loop within square brackets.
- The append() method adds elements to the end of the list, increasing the list's size dynamically.
- The insert() method allows adding elements at a specific index, shifting subsequent elements to the right.
- Elements can be removed by value using the remove() method, which deletes the first matching element found.
- Lists can be sorted in place using the sort() method, which arranges elements in ascending or descending order.
- The len() function returns the number of elements in a list, providing a quick way to determine its size.
- Tuples are immutable, meaning their contents cannot be changed after creation.
- Tuples maintain a defined order, with elements arranged in a specific sequence that remains consistent.
- Tuples can store heterogeneous data types, including integers, strings, lists, and other tuples.
- Elements in tuples are accessed via zero-based indexing, similar to lists.
- Negative indexing is supported, allowing access to elements from the end of the tuple.
- Tuples provide data integrity, ensuring that the data remains constant throughout the program's execution.
- Tuples are hashable, making them usable as keys in dictionaries and elements in sets.
- Slicing allows access to a range of elements within tuples, specifying start, stop, and step indices.
- Tuples are useful for storing read-only data, ensuring stability and preventing accidental modification.