Imagine you have a bunch of fruits (or maybe just some bananas) and you want to keep track of how many you have. You could make a list or a tuple, right? But what if you wanted to sort those fruits by size or color? Or what if you needed to access them quickly without having to iterate through the entire collection every time? That’s where Python’s built-in collection types come in handy!
First up we have lists. Lists are like a grocery list, but for data. You can add and remove items as needed, and they don’t care if you put them in any particular order (unless you sort them). Here’s an example:
# Creating a list of fruits
fruits = ["banana", "apple", "orange"]
# Printing the length of the list
print(len(fruits)) # prints 3
# Printing the second item in the list
print(fruits[1]) # prints 'apple'
# The above code creates a list of fruits and prints the length of the list as well as the second item in the list.
# The len() function is used to get the length of the list and the index [1] is used to access the second item in the list.
Next we have tuples. Tuples are like a basket of fruits, but without the ability to add or remove items (unless you slice them). They also don’t care about order unless you sort them:
# Tuples are immutable data structures, meaning they cannot be modified after creation
# They are defined using parentheses instead of square brackets like lists
# They are useful for storing data that should not be changed, such as days of the week or months of the year
# Create a tuple of fruits
fruits = ("banana", "apple", "orange")
# Use the len() function to get the length of the tuple
print(len(fruits)) # prints 3
# Use indexing to access a specific item in the tuple
print(fruits[1]) # prints 'apple'
Now sets. Sets are like a fruit salad, but without the ability to add or remove items (unless you update them). They also don’t care about order:
# Sets are like a fruit salad, but without the ability to add or remove items (unless you update them).
# They also don't care about order.
# Creating a set named "fruits" with three elements: "banana", "apple", and "orange"
fruits = {"banana", "apple", "orange"}
# Printing the length of the set using the len() function
print(len(fruits)) # prints 3
# Checking if "apple" is in the set using the "in" keyword
# Returns True if "apple" is in the set, False otherwise
print("apple" in fruits)
Finally we have dictionaries. Dictionaries are like a fruit basket with labels on each piece of fruit:
# Dictionaries are like a fruit basket with labels on each piece of fruit.
# They allow us to store data in key-value pairs.
# Create a dictionary called fruits with three key-value pairs.
fruits = {"banana": 10, "apple": 5, "orange": 2}
# Use the len() function to get the number of keys in the dictionary.
# Convert the keys into a list using the list() function.
# Print the length of the list, which is the number of keys in the dictionary.
print(len(list(fruits.keys()))) # prints 3 (the number of keys in the dictionary)
# Use the 'in' keyword to check if 'apple' is a key in the dictionary.
# If 'apple' is a key, the statement will return True, otherwise it will return False.
print("apple" in fruits) # returns True if 'apple' is a key in the dictionary, False otherwise
Python’s collection types: lists for grocery shopping, tuples for fruit baskets, sets for fruit salads, and dictionaries for labeling your produce.