Day 14 : Python Data Types and Data Structures for DevOps

Photo by Zach Graves on Unsplash

Day 14 : Python Data Types and Data Structures for DevOps

Data Types in Python

Python has several built-in data types, which are categorized into different groups based on their behavior and purpose. Here’s an overview:


1. Numeric Types

These data types represent numbers.

  • int: Represents integers (e.g., 10, -5).

  • float: Represents floating-point numbers (e.g., 3.14, -0.01).

  • complex: Represents complex numbers (e.g., 3 + 4j, -1 + 2j).


2. Sequence Types

These are used to store collections of items.

  • list: A mutable ordered collection (e.g., [1, 2, 3], ['a', 'b', 'c']).

  • tuple: An immutable ordered collection (e.g., (1, 2, 3), ('a', 'b', 'c')).

  • range: Represents an immutable sequence of numbers (e.g., range(0, 10)).


3. Text Type

Used to store and manipulate text.

  • str: A sequence of Unicode characters (e.g., "Hello", 'Python').

4. Set Types

Unordered collections of unique items.

  • set: A mutable unordered collection (e.g., {1, 2, 3}, {'a', 'b', 'c'}).

  • frozenset: An immutable unordered collection (e.g., frozenset([1, 2, 3])).


5. Mapping Type

Used to store key-value pairs.

  • dict: A mutable collection of key-value pairs (e.g., {"name": "Alice", "age": 25}).

6. Boolean Type

Represents truth values.

  • bool: Can take values True or False.

7. Binary Types

Used for handling binary data.

  • bytes: Immutable sequence of bytes (e.g., b'Hello').

  • bytearray: Mutable sequence of bytes (e.g., bytearray(b'Hello')).

  • memoryview: A view of the memory of another binary object.


8. None Type

Represents the absence of a value or a null value.

  • NoneType: Has only one value, None.

Data Structures in Python

Python provides several built-in data structures to handle and organize data efficiently. These data structures are essential for programming and problem-solving. Here's a summary of Python's primary data structures:


1. Lists

  • Definition: Ordered, mutable collections of items that can hold heterogeneous data types.

  • Characteristics:

    • Items are indexed.

    • Can grow or shrink dynamically.

    • Duplicates are allowed.

  • Example:

    mylist = ["apple", "banana", "cherry"]

    print(mylist)


2. Tuples

  • Definition: Ordered, immutable collections of items.

  • Characteristics:

    • Similar to lists, but cannot be modified after creation.

    • Useful for fixed collections of data.

  • Example:

mytuple = (10, 20, 30)

3. Dictionaries

  • Definition: Unordered collections of key-value pairs.

  • Characteristics:

    • Keys are unique and immutable.

    • Values can be of any data type.

  • Example:

d1 = {"name": 'Bob', "Gender": 'Male', "Age": '25'}

print(d1)

4. Sets

  • Definition: Unordered collections of unique items.

  • Characteristics:

    • No duplicate items.

    • Used for membership testing and eliminating duplicates.

  • Example:

numbers = {10, 50, 20}

print(numbers)

5. Frozensets

  • Definition: Immutable version of a set.

  • Characteristics:

    • Like sets but cannot be modified after creation.
  • Example:

    fruits = frozenset(["apple", "banana", "orange"])

    print(fruits)


6. Strings

  • Definition: Immutable sequences of Unicode characters.

  • Characteristics:

    • Indexed, iterable, and immutable.

    • Can be sliced and concatenated.

  • Example:

    my_str = “I am learning python programming language”

7. Stacks

  • Definition: Linear data structure following the LIFO (Last In, First Out) principle.

  • Implementation:

    • Can be implemented using a list.
  • Example:

    stack = [‘a’, ’b’, ’c’]


8. Queues

  • Definition: Linear data structure following the FIFO (First In, First Out) principle.

  • Implementation:

    • Can be implemented using collections.deque.
  • Example:

    from collections import deque

    queue = deque()


9. Linked Lists

  • Definition: A sequence of nodes where each node contains data and a reference to the next node.

  • Implementation:

    • Not built into Python but can be implemented manually.
  • Example:

    class Node:

    def init(self, data):

    self.data = data

    self.next = None


10. Heaps

  • Definition: Binary trees where the parent node is always smaller (min-heap) or larger (max-heap) than its children.

  • Implementation:

    • Use heapq module in Python.
  • Example:

    import heapq

    heap = [5, 1, 10, 2]

    heapq.heapify(heap)

    print(heap)


11. Graphs

  • Definition: Represented as a collection of nodes (vertices) and edges.

  • Implementation:

    • Can be implemented using dictionaries or adjacency lists.
  • Example:

    graph = { 'A': ['B', 'C'],

    'B': ['A', 'D'],

    'C': ['A', 'D'],

    'D': ['B', 'C']

    }

12. Arrays

  • Definition: Homogeneous collections of data (all items of the same type).

  • Implementation:

    • Use the array module.
  • Example

    import array

    arr = array.array('i', [1, 2, 3])

Tasks

  1. Give the Difference between List, Tuple and set. Do hands on and put screenshots as per your understanding.
ListSetTuple
Lists is MutableSet is MutableTuple is Immutable
It is Ordered collection of itemsIt is Unordered collection of itemsIt is Ordered collection of items
Items in list can be replaced or changedItems in set cannot be changed or replaced but you can remove and add new items.Items in tuple cannot be changed or replaced

Example:

List

Tuple

Set

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

  1. 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.