Data structures in Python are used to store collections of data in an organized and efficient way. Here are some of the most common ones:
Lists
A list in Python is a collection of items. Lists are ordered, changeable, and allow duplicate values. Lists are written with square brackets.
# Example of list data type
my_list = ["apple", "banana", "cherry"]
Tuples
A tuple in Python is similar to a list. The difference between the two is that tuples are immutable, which means we cannot change the elements of a tuple once it is assigned.
# Example of tuple data type
my_tuple = ("apple", "banana", "cherry")
Sets
A set in Python is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed). However, a set itself is mutable. We can add or remove items from it.
# Example of set data type
my_set = {"apple", "banana", "cherry"}
Dictionaries
A dictionary in Python is an unordered collection of items. Each item of a dictionary has a key/value pair.
# Example of dictionary data type
my_dict = {"name": "John", "age": 30, "city": "New York"}