Python Data Types and Data Structures for DevOps

Python Data Types and Data Structures for DevOps


Data Types

  • Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.
  • Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
  • Python has the following data types built-in by default: Numeric(Integer, complex, float), Sequential(string,lists, tuples), Boolean, Set, Dictionaries, etc

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 Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type
  • Tuple Python Tuple is a collection of Python objects much like a list but Tuples are immutable in nature i.e. the elements in the tuple cannot be added or removed once created. Just like a List, a Tuple can also contain elements of various types.
  • Dictionary Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized

  • Tasks

  1. Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.

Lists:

  • Lists are ordered collections of items.
  • Lists are mutable, meaning you can change, add, and remove items after the list is created.
  • Lists are defined using square brackets [].

Tuples:

  • Tuples are ordered collections of items.
  • Tuples are immutable, meaning once they are created, their elements cannot be changed, added, or removed.
  • Tuples are defined using parentheses ().


?

Sets:

  • Sets are unordered collections of unique items.
  • Sets are mutable, meaning you can add or remove items after the set is created, but you cannot change the items.
  • Sets do not allow duplicate elements.
  • Sets are defined using curly braces {}.

  1. Changing an element in a list (mutable):

my_list[0] = 10 print("Modified List:", my_list)

  1. Trying to change an element in a tuple (immutable):

try: my_tuple[0] = 10 except TypeError as e: print("Error:", e)

  1. Adding an element to a set (mutable, unique):

my_set.add(6) print("Modified Set:", my_set)

  1. Adding a duplicate element to a set (ignores duplicates):

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:

  • We start with the initial list cloud_providers.
  • We use the append() method to add "Digital Ocean" to the list.
  • Then, we use the sort() method to sort the list in alphabetical order.
  • Finally, we print the sorted list.

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.

?

要查看或添加评论,请登录

社区洞察

其他会员也浏览了