Day 14: Essential Python Data Types and Structures for DevOps

Day 14: Essential Python Data Types and Structures for DevOps

ยท

4 min read

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 instances (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 on the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamentals 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 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

Hands-On Tasks

  1. Give the Difference between List, Tuple and set. Do Handson.

    • List

      • The list is mutable, which means you can modify their elements after creation.

      • The list is defined by using the square brackets '[ ]'.

      • Lists maintain the order of elements, so you can access them with an index.

      • Lists can contain duplicate elements.

    • Tuple

      • The tuple is immutable, which mean you cannot modify their elements once defined.

      • Tuples are defined by using paratheses '( )'.

      • It also maintains the order of elements.

      • A tuple also contains duplicate elements.

    • Set

      • Sets are mutable, which means you can add or remove elements after creation.

      • Sets are defined using the curly braces '{ }'.

      • Sets do not maintain the order of elements, you cannot access them by index.

      • Sets only contain unique elements, duplicate elements are removed automatically.

  2. Create the 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" }

    • Steps:

      • We start by creating the dictionary named " fav_tools ". This dictionary contains the integer '(1 to 7)' with a value in it.

      • You should specify the key of your favourite tools by favorite_tool_key = 1.

      • We use an if statement to check if the favorite_tool_key exits in the fav_tools dictionary.

      • If the key exists, the tool name will print.

      • If the key is not found in the dictionary, we print a message indicating that it doesn't exist.

  3. Create a List of cloud service providers e.g. 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]

    Steps:

    • Create a list named cloud_provider containing the 'AWS', 'GCP', and 'Azure'.

    • Add the Digital Ocean to the list using the append () method.

    • The sort () method is applied to sort the list in alphabetical order.

    • Then we print the sorted list of cloud providers.

<Thats all for today, Make sure you do hands-on practice. Follow me to join me in the journey of DevOps>

ย