Python has four built-in classes that are used to create objects that store other objects.
They are: tuple, list, set and dictionary.
We’ll cover them all in detail in the following pages, but here’s a brief summary of what they’re used for and some example code.
# Tuple
c1 = (2, 6, 9)
print(c1)
# List
c2 = ["One", "Two", "Three"]
print(c2)
# Set
c3 = {"dog", "cat", "horse"}
print(c3)
# Dictionary
c4 = {"Mon": "Monday", "Tue": "Tuesday", "Wed": "Wednesday"}
print(c4)
(2, 6, 9)
['One', 'Two', 'Three']
{'horse', 'dog', 'cat'}
{'Mon': 'Monday', 'Tue': 'Tuesday', 'Wed': 'Wednesday'}
- tuple allows you to create a list of objects of any types. They remain in the order you put them. Once you’ve created a tuple, you can’t change it: it’s immutable.
- list is similar to tuple except you can modify it, adding or removing items: it’s mutable.
- set stores unique objects: you can’t have the same object twice in a set. It does not maintain any particular order.
- dict (dictionary) lets you store key-value pairs, so it’s essentially a lookup table. In older versions of Python dict was not ordered, but in more recent versions of Python, dict will keep objects in the order you specified.
Leave a Reply