Introduction
In Python, there are several different types of collections that you can use to store and manipulate data. These collections include lists, tuples, sets, and dictionaries.
A list is an ordered collection of items that is defined using square brackets [ ] and the items in a list are separated by commas. Lists are mutable, which means that you can change the contents of a list after it is created. You can access the items in a list by their index and you can modify the items in a list by assigning a new value to an index.
A tuple is an ordered collection of items that is similar to a list, but unlike a list, a tuple is immutable, which means that you cannot change the contents of a tuple after it is created. Tuples are defined using parentheses ( ) and the items in a tuple are separated by commas. You can access the items in a tuple by their index, but you cannot modify the items in a tuple by assigning a new value to an index.
A set is an unordered collection of unique items. Sets are defined using curly braces { } and the items in a set are separated by commas. Sets do not have an index, so you cannot access the items in a set using an index. You can add items to a set using the add
method and you can remove items from a set using the remove
method.
A dictionary is a collection of key-value pairs. Dictionaries are defined using curly braces { } and the items in a dictionary are separated by commas. The items in a dictionary are key-value pairs, where the key is a unique identifier for the value. You can access the values in a dictionary using the keys and you can add or remove key-value pairs from a dictionary using the syntax my_dict[key] = value
and the del
statement, respectively.
It is important to choose the right collection type for your needs based on the characteristics and uses of each type. Lists are suitable for data that may change, tuples are good for data that should not be changed, sets are useful for storing unique items, and dictionaries are useful for storing key-value pairs.
Lists
A list is an ordered collection of items that is defined using square brackets [ ] and the items in a list are separated by commas. Lists are mutable, which means that you can change the contents of a list after it is created.
Here is an example of a list:
my_list = [1, 2, 3, 4]
PythonYou can access the items in a list by their index, which is the position of the item in the list. The index of the first item in a list is 0. You can access an item in a list using the syntax my_list[index]
.
first_item = my_list[0] # first_item will be 1
second_item = my_list[1] # second_item will be 2
PythonYou can also modify the items in a list by assigning a new value to an index:
my_list[1] = 5 # now my_list is [1, 5, 3, 4]
PythonTuples
A tuple is an ordered collection of items that is similar to a list, but unlike a list, a tuple is immutable, which means that you cannot change the contents of a tuple after it is created. Tuples are defined using parentheses ( ) and the items in a tuple are separated by commas. Here is an example of a tuple:
my_tuple = (1, 2, 3, 4)
PythonLike lists, you can access the items in a tuple by their index. However, you cannot modify the items in a tuple by assigning a new value to an index. If you try to do this, you will get a TypeError.
first_item = my_tuple[0] # first_item will be 1
second_item = my_tuple[1] # second_item will be 2
try:
my_tuple[1] = 5
except TypeError:
print("Cannot modify the contents of a tuple")
PythonSets
A set is an unordered collection of unique items. Sets are defined using curly braces { } and the items in a set are separated by commas. Sets do not have an index, so you cannot access the items in a set using an index. Here is an example of a set:
my_set = {1, 2, 3, 4}
PythonYou can add items to a set using the add
method:
my_set.add(5) # now my_set is {1, 2, 3, 4, 5}
PythonYou can remove items from a set using the remove
method:
my_set.remove(3) # now my_set is {1, 2, 4, 5}
PythonA dictionary is a collection of key-value pairs. Dictionaries are defined using curly braces { } and the items in a dictionary are separated by commas. The items in a dictionary are key-value pairs, where the key is a unique identifier for the value. Here is an example of a dictionary:
my_dict = {'key1': 'value1', 'key2': 'value2'}
PythonYou can access the values in a dictionary using the keys:
value1 = my_dict['key1'] # value1 will be</code>
value2 = my_dict['key2'] # value2 will be 'value2'
PythonYou can add new key-value pairs to a dictionary using the syntax my_dict[key] = value
:
my_dict['key3'] = 'value3' # now my_dict is {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
PythonYou can remove a key-value pair from a dictionary using the del
statement:
del my_dict['key1'] # now my_dict is {'key2': 'value2', 'key3': 'value3'}
PythonDictionaries
A dictionary is a collection of key-value pairs. Dictionaries are defined using curly braces { } and the items in a dictionary are separated by commas. The items in a dictionary are key-value pairs, where the key is a unique identifier for the value.
Here is an example of a dictionary:
my_dict = {'key1': 'value1', 'key2': 'value2'}
PythonYou can access the values in a dictionary using the keys:
value1 = my_dict['key1'] # value1 will be 'value1'
value2 = my_dict['key2'] # value2 will be 'value2'
PythonYou can add new key-value pairs to a dictionary using the syntax my_dict[key] = value
:
my_dict['key3'] = 'value3' # now my_dict is {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
PythonYou can remove a key-value pair from a dictionary using the del
statement:
del my_dict['key1'] # now my_dict is {'key2': 'value2', 'key3': 'value3'}
Python