Python Data Types and Data Structures for DevOps
Shabina Tarique
DevSecOps Professional | AWS Certified | Kubernetes, Docker, Jenkins Expert | PCI DSS Compliance Specialist
Data Types
To check what is the data type of the variable used, we can simply write:?your_variable=100?type(your_variable)
Data Structures
Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.
Lists:
Tuples:
?
Sets:
my_list[0] = 10 print("Modified List:", my_list)
try: my_tuple[0] = 10 except TypeError as e: print("Error:", e)
my_set.add(6) print("Modified Set:", my_set)
领英推荐
my_set.add(5) print("Set with Duplicate:", my_set)
After running these operations, you'll notice how lists can be changed, tuples cannot be changed, and sets automatically handle duplicates.
2. Create below Dictionary and use Dictionary methods to print your favourite 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"
}
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.
[Hint: Use keys to built in functions for Lists]
Explanation:
Conclusion:
In this blog post, we explored Python’s data types and data structures essential for DevOps professionals. We discussed various built-in data types in Python, including numeric, sequential, boolean, set, and dictionaries, highlighting their characteristics and usage. Furthermore, we delved into the fundamental data structures like lists, tuples, and dictionaries, understanding their differences and practical applications in DevOps scenarios.
We also performed hands-on exercises to solidify our understanding of lists, tuples, and sets, showcasing their unique properties such as mutability, immutability, and handling of duplicates. By running code snippets, we observed how lists can be modified, tuples remain unchanged, and sets automatically handle duplicate elements, reinforcing the importance of choosing the right data structure based on specific requirements.
Additionally, we demonstrated the use of dictionary methods to retrieve values based on keys, providing a convenient way to access information stored in dictionaries. We also implemented a program to add a new item to a list of cloud service providers and sort it alphabetically, utilizing built-in list functions to accomplish the task efficiently.
Overall, this blog post serves as a comprehensive guide for DevOps professionals to leverage Python’s powerful data types and data structures effectively in their day-to-day tasks, enhancing productivity and enabling efficient management of data and resources in various DevOps workflows.
?