๐Ÿš€Day 14 Task: Python Data Types and Data Structures for DevOps๐Ÿ

๐Ÿš€Day 14 Task: Python Data Types and Data Structures for DevOps๐Ÿ

ยท

4 min read

New day, New Topic.... Let's learn along ๐Ÿ˜‰

In this session, we navigated through Python's versatile data types and structures. Understanding these concepts is crucial for effective programming and automation in a DevOps environment. We applied our knowledge with practical tasks, reinforcing our comprehension of lists, tuples, sets, dictionaries, and their applications. Armed with this foundational understanding, we're better equipped to tackle more complex challenges in our DevOps journey. Happy coding! ๐Ÿš€โœจ

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 (objects) 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.

Difference Between List, Tuple, and Set:

  1. List:

      • A list is an ordered collection of items, and it allows duplicate values.

        • Lists are mutable, which means you can change, add, or remove elements after the list is created.

        • Lists are defined using square brackets ([]).

    • Example:

    • When you run this code, the output will be:

      
        my_list = [1, 2, 3, 'DevOps', True]
      
  2. Tuple:

      • A tuple is similar to a list, but it is immutable, which means you cannot change its elements after it is created.

        • Tuples are typically used to store a sequence of related data.

        • Tuples are defined using parentheses (()).

    • Example:

      • 
          my_tuple = (1, 2, 'Python', False)
        
  3. Set:

      • A set is an unordered collection of unique elements.

        • Sets do not allow duplicate values, and the order of elements is not guaranteed.

        • Sets are useful when you want to store a collection of items, but you don't care about their order or duplicates.

        • Sets are defined using curly braces ({}) or the set() function.

    • Example:

      • 
          my_set = {1, 2, 'Cloud', True}
        

2. Create the below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.

Now, let's move on to the dictionary task.

To print your favorite tool using the keys of the dictionary, you can use the following code:

  • 
      fav_tools = {
        1: "Linux",
        2: "Git",
        3: "Docker",
        4: "Kubernetes",
        5: "Terraform",
        6: "Ansible",
        7: "Chef"
      }
    
      favorite_key = 1  # Replace with the key corresponding to your favorite tool
      favorite_tool = fav_tools.get(1)
      if favorite_tool:
          print("My favorite tool is", "Linux")
      else:
          print("Favorite tool not found")
    

When you run this code, the output will be:

    •     My favorite tool is Linux
      

3. Create a List of cloud service providers eg.

Moving on to the next task, to add "Digital Ocean" to the list of cloud_providers and sort the list in alphabetical order, you can use the following code:

1. First, we define a list called cloud_providers with three initial elements: "AWS", "GCP", and "Azure".

2. We use the append() method to add the string "Digital Ocean" to the cloud_providers list. This method appends the specified element to the end of the list.

3. Next, we use the sort() method to sort the elements of the cloud_providers list in alphabetical order. The sort() method modifies the list in place.

4. Finally, we use the print() function to display the sorted list of cloud providers.

Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order:-

cloud_providers = ["AWS", "GCP", "Azure"]
cloud_providers.append("Digital Ocean")
cloud_providers.sort()

print(cloud_providers)

When you run this code, the output will be:

['AWS', 'Azure', 'Digital Ocean', 'GCP'] ['AWS', 'Azure', 'Digital Ocean', 'GCP']

As you can see, "Digital Ocean" has been added to the list, and the list is now sorted in alphabetical order.

ย