Python provides several built-in data structures that are versatile and widely used for storing and manipulating data. Here’s a rundown of the main ones:
1. List :
* A mutable, ordered sequence of elements.
* Elements can be of any data type (e.g., integers, strings, or even other lists).
* Defined using square brackets: `my_list = [1, 2, 3, "hello"]
`.
* Supports operations like appending, removing, and indexing.
2. Tuple :
* An immutable, ordered sequence of elements.
* Similar to lists but cannot be modified after creation.
* Defined using parentheses: `my_tuple = (1, 2, 3, "world")
`.
* Useful for fixed collections of items or as dictionary keys.
3. Dictionary :
* A mutable, unordered collection of key-value pairs.
* Keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any type.
* Defined using curly braces: `my_dict = {"name": "Alice", "age": 25}
`.
* Great for fast lookups by key.
4. Set :
* A mutable, unordered collection of unique elements.
* Useful for removing duplicates or performing mathematical set operations (union, intersection, etc.).
* Defined using curly braces or the `set()
` function: `my_set = {1, 2, 3}` or `my_set = set([1, 2, 2])` (results in `{1, 2}`)
.
* There’s also `frozenset`, an immutable version of a set.
5. String :
* An immutable sequence of characters.
* Defined using single, double, or triple quotes: `my_string = "hello"
`.
* Supports operations like slicing, concatenation, and formatting.
These are the core built-in data structures in Python. Each has its own strengths depending on whether you need mutability, order, uniqueness, or key-based access. There are also other specialized types like `
bytes`, `
bytearray`, and collections from the `
collections`
module (e.g., `deque`, `Counter`), but the ones above are the most fundamental and commonly used. Let me know if you’d like examples or deeper details on any of these!