Python has several built-in data structures that are used to store and organize data. Here is a short description of the most commonly used data structures in Python:
Mutable Data structures
Lists
Lists are a collection of items in a specific order. They are defined using square brackets [ ] and can store items of different data types. They are mutable
, which means you can add, remove, or change items in a list after it is created. Lists have built-in methods like append()
, insert()
, remove()
etc to manipulate them.
Sets
Sets are a collection of unique items, meaning it does not allow duplicate items. They are defined using curly braces {} or set()
constructor. Sets are mutable
and have useful methods like add()
, remove()
, intersection()
, union()
etc.
Dictionaries
Dictionaries are collections of key-value pairs. They are defined using curly braces {}. The keys in a dictionary are unique and are used to access the corresponding values. Dictionaries are mutable
, which means you can add, remove, or change key-value pairs after it is created. They have various methods like keys()
, values()
, items()
, get()
etc.
Immutable Data structure
Tuples
Tuples are similar to lists, but they are immutable
, which means you cannot add, remove, or change items in a tuple after it is created. They are defined using parentheses () and can also store items of different data types.
These are some of the basic data structures in Python and can be very powerful when used in combination with other built-in functions and control structures of Python.