Table of contents
- Difference between List, Tuple and Set
- Create a Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary
- Create a List of cloud service providers eg. cloud_providers = ["AWS","GCP","Azure"]
- Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.
Difference between List, Tuple and Set
In Python, there are three main data structures used to store collections of data: lists, tuples, and sets. Each of these data structures has its unique properties and uses.
Lists are the most versatile data structure in Python. They are ordered, mutable, and can contain any type of data. Lists are typically used to store collections of data that need to be accessed in a specific order, such as a shopping list or a list of names.
Tuples are also ordered, but they are immutable. This means that once a tuple is created, its contents cannot be changed. Tuples are typically used to store data that does not need to be changed, such as a person's name and address.
Sets are unordered and unordered. This means that the order of the elements in a set does not matter, and sets cannot contain duplicate elements. Sets are typically used to store collections of data that need to be unique, such as a set of all the prime numbers.
Here is a table that summarizes the key differences between lists, tuples, and sets:
Feature | List | Tuple | Set |
Ordered | Yes | Yes | No |
Mutable | Yes | No | No |
Can contain duplicates | Yes | No | No |
Hands-on
Here is some hands-on code that demonstrates the differences between lists, tuples, and sets:
# Create a list
list = [1, 2, 3, 4, 5]
# Print the list
print(list)
# Create a tuple
tuple = (1, 2, 3, 4, 5)
# Print the tuple
print(tuple)
# Create a set
set = {1, 2, 3, 4, 5}
# Print the set
print(set)
# Change the value of an element in the list
list[0] = 10
# Try to change the value of an element in the tuple
tuple[0] = 10
# This will raise an error, because tuples are immutable
# Add an element to the list
list.append(6)
# Print the list
print(list)
# Add an element to the set
set.add(6)
# Print the set
print(set)
# This will not add the element to the set, because sets cannot contain duplicate elements
The output of the above code is as follows:
Code snippet
[1, 2, 3, 4, 5]
(1, 2, 3, 4, 5)
{1, 2, 3, 4, 5}
[10, 2, 3, 4, 5]
{1, 2, 3, 4, 5, 6}
As you can see from the output, lists, tuples, and sets all have their unique properties. Lists are the most versatile data structure, but tuples and sets are more efficient for certain tasks.
Create a Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary
Here is the code to create a dictionary and print your favourite tool:
fav_tools = {
1: "Linux",
2: "Git",
3: "Docker",
4: "Kubernetes",
5: "Terraform",
6: "Ansible",
7: "Chef"
}
# Get the key of your favorite tool
favorite_tool_key = int(input("What is your favorite tool? (1-7): "))
# Get the value of the favorite tool
favorite_tool = fav_tools[favorite_tool_key]
# Print the favorite tool
print(f"Your favorite tool is {favorite_tool}")
Output:-
Code snippet
What is your favorite tool? (1-7): 3
Your favorite tool is Docker
Create a List of cloud service providers eg. cloud_providers = ["AWS","GCP","Azure"]
Write a program to add Digital Ocean
to the list of cloud_providers and sort the list in alphabetical order.
Here is the code to create a list of cloud service providers, add Digital Ocean to the list, and sort the list in alphabetical order:
cloud_providers = ["AWS", "GCP", "Azure"]
# Add Digital Ocean to the list
cloud_providers.append("Digital Ocean")
# Sort the list in alphabetical order
cloud_providers.sort()
# Print the list
print(cloud_providers)
The output of the above code is as follows:
Code snippet
['AWS', 'Azure', 'Digital Ocean', 'GCP']
Here is a breakdown of what each line of code does:
cloud_providers = ["AWS", "GCP", "Azure"]
creates a list of cloud service providers and assigns it to the variablecloud_providers
.cloud_providers.append("Digital Ocean")
adds the string "Digital Ocean" to the end of the listcloud_providers
.cloud_providers.sort()
sorts the listcloud_providers
in alphabetical order.print(cloud_providers)
prints the listcloud_providers
.
To connect with me - https://www.linkedin.com/in/subhodey/