Python Data Types and Data Structures for DevOps-Part2

Python Data Types and Data Structures for DevOps-Part2

Let's embark on a deeper exploration of Python as we begin our journey.

Data Type

Data types serve as the classification or categorization of data items, defining the kind of value and operations applicable to a specific data set. In Python programming, where everything is treated as an object, data types are essentially classes, and variables act as instances (objects) of these classes.

Python incorporates various built-in data types, including Numeric (Integer, Complex, Float), Sequential (String, Lists, Tuples), Boolean, Set, Dictionaries, and more.

To ascertain the data type of a variable, one can easily do so by using the type() function.

Data Structures

Data structure serve as a means of efficiently organizing data based on the specific requirements of a situation. They form the foundational structure around which programming languages build programs. Python, in particular, simplifies the learning process of these fundamental data structures compared to other programming languages.

Lists: Python Lists, akin to arrays in other languages, are ordered collections of data. They offer great flexibility as the items within a list are not required to be of the same type.

Tuple: Python Tuples, similar to lists, constitute a collection of Python objects. However, Tuples are immutable, meaning their elements cannot be added or removed once created. Like Lists, a Tuple can accommodate elements of various types.

Dictionary: Python Dictionaries, analogous to hash tables in other languages with a time complexity of O(1), represent an unordered collection of data values. They are employed to store data values in a map-like structure. In contrast to other data types that hold only a single value per element, a Dictionary consists of key-value pairs, enhancing its optimization.

Below, you'll discover a Python script along with its corresponding output, providing a more detailed understanding of the concept.

Task

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

    | Property | List: | Tuple: | Set: | | --- | --- | --- | --- | | Mutability: | Mutable (can be modified after creation). | Immutable (cannot be modified after creation). | Mutable (can be modified after creation). | | Syntax: | Defined with square brackets []. | Defined with square brackets (). | Defined with square brackets {}. | | Indexing: | Access elements by index. | Access elements by index. | No indexing or order. Elements are unordered and unique. | | Use Case: | Suitable when the sequence can be modified, and indexing is crucial. | Appropriate when data should not be modified after creation, e.g., for security reasons. | Ideal when uniqueness and order are not important, and duplicates are not allowed. | | Example: | test_list = [1, 2, 3] | test_tuple = (1, 2, 3) | test_set = {1, 2, 3} |

  2. 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"

    }

    Answer

    \=======

  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]

Answer

I hope that this task has contributed value to your understanding of Python's data types.

Thanks,

Kishor Chavan