In Python, collections are data types used for storing and organizing data. They are also useful for data analysis and statistics. Usually, to use a collection, you would have to import a module but there are 4 common built-in collections in Python that don't require you to use a module. Those include lists, dictionaries, sets, and tuples. Each built-in collection has its own differences which is why some built-in collections are better than others in certain scenarios.
A list is a built-in collection that is ordered, mutable, and allows duplicate values. Think of a list like a bus and each seat in the bus is a data-type. Lists can store multiple data types and are very useful when order and mutability are required. An ordered collection means that it will store the values the way you ordered them. For example, if you store a list like [1, 5, 3, 4] and when you print it, it will always display [1, 5, 3, 4]. On the other hand, an unordered list might display [4, 1, 5, 3] when you store it as [1, 5, 3, 4]. Mutability means that you can change a list even after you have created it.
A list, just like dictionaries and tuples (not sets) has a way to organize its elements called index numbers. Index numbers start from 0 until the end of the list. For example, the 1st element of a list has an index of 0 and the 5th element of a list has an index of 4. Here is an example of this system.
Here are some ways to use a list:
You can create a list like this:
You can add an item at the end of the list (append) like this:
You can remove an item in a list with the index number like this:
You can remove an item in a list with the actual value like this:
You can add the items of one list into another like this:
You can add items in the middle of the list like this:
You can see the length of a list like this:
A dictionary is a built-in collection in Python that are used to store data in key:value pairs. This means that in a dictionary, there is a key item, and a value associated to that key. Just like indices, keys and values also start from 0. Dictionaries are mutable, ordered, and do not allow duplicates. In a dictionary, the value 1 is considered a duplicate to the value True and the value 0 is considered a duplicate to the value False. Here is an example of a dictionary:
You can access the value to a key like this:
You can also access all the keys in a dictionary like this:
You can access all the values in a dictionary like this:
You can add items in a dictionary like this:
You can update items in a dictionary like this:
You can remove items in a dictionary like this:
Dictionaries are used because they are fast ways to store complex types of data.
Sets and tuples are similar to dictionaries but both are unchangeable. Sets are unordered while tuples aren't and sets don't allow duplicate values while tuples do. In a set, the value 1 is considered a duplicate to the value True and the value 0 is considered a duplicate to the value False. Here are examples of tuples and sets:
1. Identify if the items are either lists, dictionaries, sets, or tuples.
2. Print all the values of a list individually.
3. Find the average of all the values in a list.
4. Create a set from a string to find all the unique characters
5. Unpack a tuple into individual variables
6. Reverse the keys and values in a dictionary
7. BONUS: Create a shopping cart program where items are stored in a list, quantities and prices are stored in a dictionary, and you can display the total using tuples.
Copyright © 2024 PieceOfAPie - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.