Day14/90DaysofDevOps Challenge Python Data Types and Data Structures for DevOps

Day14/90DaysofDevOps Challenge 
Python Data Types and Data Structures for DevOps

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 classes and variables are instances (object) of these classes. These are built-in data types as follows:

  1. Numeric Types:

    • int: Integer values, e.g., 10, -3, 0.

    • float: Floating-point values with decimal precision, e.g., 3.14, -0.5.

    • complex: Complex numbers in the form a + bj, where a and b are floats and j represents the imaginary unit.

  2. Sequence Types:

    • str: Strings of characters, e.g., "Hello, world!", 'Python'.

    • list: Ordered and mutable sequences of elements, enclosed in square brackets ([]), e.g., [1, 2, 3], ['apple', 'banana'].

    • tuple: Ordered and immutable sequences of elements, enclosed in parentheses (()), e.g., (1, 2, 3), ('apple', 'banana').

  3. Mapping Type:

    • dict: Unordered collections of key-value pairs, enclosed in curly braces ({}), e.g., {'name': 'John', 'age': 25}.
  4. Set Types:

    • set: Unordered collections of unique elements, enclosed in curly braces ({}), e.g., {1, 2, 3}, {'apple', 'banana'}.

    • frozenset: Immutable versions of sets, enclosed in parentheses (()), e.g., frozenset({1, 2, 3}).

  5. Boolean Type:

    bool: Represents either True or False.

  6. None Type:

    None: Represents the absence of a value or a null value.

What are Data Structures?

Data Structures allow you to organize your data in such a way that enables you to store collections of data, related to them and perform operations on them accordingly.

The data structures differ based on mutability and order. Mutability refers to the ability to change an object after its creation. Mutable objects can be modified, added, or deleted after they’ve been created, while immutable objects cannot be modified after their creation. Order, in this context, relates to whether the position of an element can be used to access the element.

Data structures in Python include lists, set, tuples, and dictionary. Each of the data structures is unique in its way.

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.

my_list = [1, 2, 'a', True]
my_list.append(3)  # Modifying the list by adding an element
print(my_list)  
# Output: 
[1, 2, 'a', True, 3]

Dictionary

Dictionaries (or dicts for short) are a central data structure. Dicts store an arbitrary number of objects, each identified by a unique dictionary key.

Dictionaries are also often called maps, hashmaps, lookup tables, or associative arrays. They allow for the efficient lookup, insertion, and deletion of any object associated with a given key.

my_dict = {} #empty dictionary
print(my_dict)
my_dict = {1: 'Python', 2: 'Java'} #dictionary with elements
print(my_dict)

#output
{}
{1: ‘Python’, 2: ‘Java’}

Tuple

Tuples are the same as lists are with the exception that the data once entered into the tuple cannot be changed no matter what. The only exception is when the data inside the tuple is mutable, only then the tuple data can be changed. The example program will help you understand better.

my_tuple = (1, 2, 'a', True)
print(my_tuple[0])  # Accessing an element of the tuple, 
#Output:
 1

Task 1:

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

In Python, list, tuple, and set are three different types of data structures, each with its own characteristics and use cases. Here's a breakdown of their differences:

List:

  • A list is an ordered collection of items, enclosed in square brackets [ ].

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

  • Elements in a list can be of different data types, and duplicates are allowed.

  • Lists are typically used when you need a dynamic collection of items that can be modified.

    1. Tuple:

      • A tuple is an ordered collection of items, enclosed in parentheses ( ) or without any enclosing brackets.

      • Tuples are immutable, which means you cannot modify, add, or remove elements after creation.

      • Elements in a tuple can be of different data types, and you can have duplicate elements.

      • Tuples are typically used when you want to store a collection of related values that should not be changed.

    2. Set:

      • A set is an unordered collection of unique elements, enclosed in curly braces { }.

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

      • Elements in a set must be hashable, so you cannot have mutable objects like lists as elements.

      • Sets are typically used when you want to perform mathematical set operations like union, intersection, and difference.

TASK2:

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"
}

TASK3:

Create a List of cloud service providers

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.

Thank you for giving your precious time to read this blog, if any suggestions on my blog comment on them and like 👍the blog if you feel helpful.

Happy Learning ✨✨!!!