Python collection is a module that provides specialized container datatypes that are alternatives to the built-in containers such as list, dict, set, and tuple. These datatypes are useful for organizing and manipulating data in different ways, such as counting, ordering, grouping, and naming.
List
A list is a data type in Python that can store multiple items in a single variable. Lists are ordered, changeable, and allow duplicate values. You can create a list by using square brackets and separating the items by commas. For example, you can create a list of dog types like this:
dog_types = ["Dalmatian", "Corgi", "Shiba Inu"] # list uses square brackets "[]"
You can access the items in a list by using their index, which starts from 0. For example, you can print the first item in the fruits list like this:
print(dog_types[0]) # Dalmatian
print(dog_types[1]) # Corgi
print(dog_types[2]) # Shiba Inu
You can also modify, add, or remove items in a list using various methods. For example, you can change the second item in the dog types list to “Welsh Corgi” like this:
dog_types[1] = "Welsh Corgi"
print(dog_types[1]) # Welsh Corgi
You can append a new item to the end of the list using the append () method. For example, you can add ” Chihuahua” to the fruits list like this:
dog_types.append("Chihuahua")
You can remove an item from the list using the remove () method. For example, you can remove “Dalmatian” from the dog_types list like this:
dog_types.remove("Dalmatian")
You can also use other methods such as sort (), reverse (), count (), index (), etc. to perform different operations on lists.
Tuple
A tuple is a data type in Python that can store multiple items in a single variable. Tuples are ordered, unchangeable, and allow duplicate values. You can create a tuple by using parentheses and separating the items by commas. For example, you can create a tuple of dog types like this:
dog_types = ("Dalmatian", "Corgi", "Shiba Inu") # tuple uses parentheses "()"
You can access the items in a tuple by using their index, which starts from 0. For example, you can print the first item in the dog_types tuple like this:
print(dog_types[0]) # Dalmatian
You can also modify, add, or remove items in a tuple using various methods. However, tuples are immutable, meaning that you cannot change the items once the tuple is created. Instead, you have to create a new tuple with the modified items. For example, you can create a new tuple with “Welsh Corgi” instead of “Corgi” like this:
new_dog_types = ("Dalmatian", "Welsh Corgi", "Shiba Inu")
You can also use other methods such as count (), index (), etc. to perform different operations on tuples.
Set
A set is a data type in Python that can store multiple items in a single variable. Sets are unordered, unchangeable, and do not allow duplicate values. You can create a set by using curly brackets and separating the items by commas. For example, you can create a set of dog_types like this:
dog_types = {"Dalmatian", "Corgi", "Shiba Inu"} # set uses curly brackets "{}" & separate by comma ","
You can access the items in a set by using a for loop or by checking if an item is in the set using the in operator. For example, you can print the items in the dog types set like this:
for dog_type in dog_types:
print(dog_type)
Or you can check if “apple” is in the fruits set like this:
print("Corgi" in dog_types) # True
You can also modify, add, or remove items in a set using various methods. However, sets are immutable, meaning that you cannot change the items once the set is created. Instead, you have to create a new set with the modified items. For example, you can create a new set with “Welsh Corgi” instead of “Corgi” like this:
new_dog_types = dog_types - {"Corgi"} | {"Welsh Corgi"}
You can also use other methods such as union (), intersection (), difference (), symmetric_difference (), etc. to perform different operations on sets.
Dictionary
A dictionary in Python is a data type that can store multiple items in a single variable. Each item consists of a key and a value, which are separated by a colon. The keys are used to access the values in the dictionary, and they must be unique and immutable. The values can be of any data type, and they can be changed or modified. You can create a dictionary by using curly braces and separating the items by commas, or by using the dict () function with a sequence of key-value pairs. For example, you can create a dictionary of dog types like this:
dog_types = {"Dalmatian": "Corgi", "Shiba Inu": "Chihuahua", "Husky": "Maltipoo"}
Or like this:
dog_types = dict([("Dalmatian", "white coat marked with black spots in Croatia"), ("Corgi", "dwarf dog in Wales"), ("Shiba Inu", "A small, alert, and agile dog in Japan")])
You can access the values in a dictionary by using the keys inside square brackets, or by using the get () method with a default value. For example, you can print the description of Dalmatian dog like this:
print(dog_types["Dalmatian"]) # white coat marked with black spots in Croatia
# Or like this:
print(dog_types.get("Chihuahua", "unknown")) # red
You can also modify, add, or remove items in a dictionary using various methods, such as update (), pop (), popitem (), clear (), etc. For example, you can change the color of banana to green like this:
dog_type["Corgi"] = "dwarf dog in South West Wales"
# Or like this:
dog_type.update({"Corgi": "dwarf dog in South West Wales"})
You can learn more about List, Tuple, Set, and Dictionary and their methods from these sources:
https://docs.python.org/3/tutorial/datastructures.html