Lists, Tuples, Sets, and Dictionary
Lists, Tuples, Sets, and Dictionary
Lists are mutable sequences of elements. This means that they can be
changed after they are created. Lists can store any type of element, including
other lists.
To create a list, you use square brackets ([]). For example, the following code
creates a list of the numbers 1, 2, and 3:
Python
my_list = [1, 2, 3]
You can access the items in a list by their index. The index of the first item in a
list is 0, and the index of the last item in a list is len(list) - 1. For example,
the following code prints the second item in the my_list list:
Python
print(my_list[1])
You can also iterate over the items in a list using a for loop. For example, the
following code prints all of the items in the my_list list:
Python
for item in my_list:
print(item)
Lists are versatile data structures that can be used to store a variety of data.
For example, you can use a list to store the items in a shopping cart, the
names of the students in a class, or the scores in a video game.
Tuples
Tuples are like lists, but they are immutable. This means that they cannot be
changed once they are created. Tuples are created using parentheses (()).
For example, the following code creates a tuple of the numbers 1, 2, and 3:
Python
my_tuple = (1, 2, 3)
Tuples are accessed and iterated over in the same way as lists. However,
since tuples are immutable, you cannot use the append(), insert(),
remove(), or pop() functions on them.
Tuples are often used to store data that does not need to be changed, such
as the days of the week or the months of the year.
Sets
Sets are unordered collections of unique elements. This means that they
cannot contain duplicates, and the order of the elements in a set is not
guaranteed. Sets are created using curly braces ({}). For example, the
following code creates a set of the numbers 1, 2, and 3:
Python
my_set = {1, 2, 3}
Sets are accessed and iterated over in the same way as lists. However, since
sets are unordered, you cannot access the items in a set by their index.
Sets are often used to store data that needs to be unique, such as the unique
words in a document or the unique products in a shopping cart.
Dictionaries
Dictionaries are unordered mappings of keys to values. This means that they
store key-value pairs, where each key is unique and each value can be
anything. Dictionaries are created using curly braces ({}). For example, the
following code creates a dictionary of people's names and ages:
Python
my_dict = {"Alice": 25, "Bob": 30}
To access the value of a key in a dictionary, you use the [] operator. For
example, the following code prints the age of Alice:
Python
print(my_dict["Alice"])
You can also iterate over the key-value pairs in a dictionary using a for loop.
For example, the following code prints all of the key-value pairs in the my_dict
dictionary:
Python
for key, value in my_dict.items():
print(f"{key}: {value}")
Dictionaries are often used to store data that needs to be organized by key,
such as the data in a database or the configuration settings for an application.