Day 14:Python Data Types and Data Structures for DevOps

Python important DataStructre with their explanation:-
Tuples:-
A tuple is a collection of ordered, immutable elements enclosed in parentheses.
You can also create a tuple using the built-in tuple() function, like this
Tuples support indexing and slicing, just like strings.


In Python, you can concatenate two or more tuples using the + operator. When you concatenate tuples, you create a new tuple that contains all the elements of the original tuples in the order they were concatenated. Here's an example:


Lists:-
A list is a collection of ordered, mutable elements enclosed in square brackets. Each element in the list can be of any data type such as integer, string, float, or even another list. It also defines by using the list() method.


There are many built-in methods that you can use to manipulate lists. Here are some of the most commonly used list methods:
append()- Adds an element to the end of the listextend()- Adds multiple elements to the end of the listinsert()- Inserts an element at a specific position in the listremove()- Removes the first occurrence of an element from the listpop()- Removes and returns the last element of the listindex()- Returns the index of the first occurrence of an element in the listcount()- Returns the number of times an element appears in the listsort()- Sorts the list in ascending order
Sets:-
A set is an unordered collection of unique elements. They are similar to lists and tuples, but they cannot have duplicate elements, and their order is not guaranteed. Sets are defined using curly braces {} or using the set() function.


Sets have many built-in methods that you can use to manipulate them.
add()- Adds an element to the setupdate()- Adds multiple elements to the setremove()- Removes an element from the setdiscard()- Removes an element from the set if it is a member. If the element is not a member, it does not raise an error.pop()- Removes and returns an arbitrary element from the setclear()- Removes all elements from the setunion()- Returns the union of two sets (i.e., all the elements that are in either set)intersection()- Returns the intersection of two sets (i.e., all the elements that are in both sets)difference()- Returns the difference between two sets (i.e., all the elements that are in set1 but not in set2)
Dictionary:-
A dictionary is an unordered collection of key-value pairs, where each key is unique and associated with a value. Dictionaries are defined using curly braces {} or using the dict() function.


Dictionaries have many built-in methods that you can use to manipulate them.
get()- Returns the value associated with a key. If the key is not found, it returns a default value (None by default).keys()- Returns a list of all the keys in the dictionaryvalues()- Returns a list of all the values in the dictionaryitems()- Returns a list of all the key-value pairs in the dictionary as tuplespop()- Removes and returns the value associated with a keyupdate()- Adds or updates key-value pairs in the dictionaryclear()- Removes all key-value pairs from the dictionary
Tasks:-
- Give the Difference between List, Tuple and Set.
| Collection VS Features | Mutable | Ordered | Indexing | Duplicate Data |
| List | ✔ | ✔ | ✔ | ✔ |
| Tuple | 𐄂 | ✔ | ✔ | ✔ |
| Set | ✔ | 𐄂 | 𐄂 | 𐄂 |
| Dictionary | ✔ | ✔ | ✔ | 𐄂 |
- Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.
fav_tools =
{
1:"Linux",
2:"Git",
3:"Docker",
4:"Kubernetes",
5:"Terraform",
6:"Ansible",
7:"Chef"
}
Input:-
fav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef"}
favorite_tool_key = 3
favorite_tool = fav_tools[favorite_tool_key]
print("my favorite tool is",favorite_tool)
Output:-

3. 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.
Input:-
cloud_providers = ["AWS","GCP","Azure"]
cloud_providers.append("Digital Ocean")
cloud_providers.sort()
print(cloud_providers)
Output:

About the author
AWS DevOps Engineer
Comments
Join the discussionNo comments yet. Be the first to comment.




