Python Data Types and Data Structures for DevOps | Part-2

Python Data Types and Data Structures for DevOps | Part-2

Data Types

Data Types refer to the various types of values or data elements that can be stored and manipulated within a program. In Python, being an object-oriented programming (OOP) language, everything is treated as an object.

To gain a deeper understanding of the OOP concept in Python, I recommend reading this comprehensive article on Python's object-oriented programming.

In Python, Data Types are implemented as classes, and variables are instances (objects) of these classes.

For a detailed explanation of Data Types, including their syntax and examples, I have covered them extensively in this blog post on Python for DevOps. I believe it can be a valuable resource for all of us.

Data Structures

Python offers several built-in data structures that are used to store and organize data efficiently. Here are the main data structures in Python:

  1. Lists: Lists are ordered, mutable sequences that can contain elements of different data types. They are created using square brackets [] and elements are separated by commas. Lists support indexing, slicing, appending, and various other operations.

  2. Tuples: Tuples are similar to lists but are immutable, meaning they cannot be modified after creation. They are created using parentheses () or without any brackets. Tuples are commonly used to store related values together.

  3. Sets: Sets are unordered collections of unique elements. They are created using curly braces {} or the set() constructor. Sets are useful for removing duplicates, performing mathematical set operations (such as union, intersection, and difference), and testing membership.

  4. Dictionaries: Dictionaries are unordered key-value pairs. Each value is associated with a unique key, allowing for efficient retrieval and modification of values. Dictionaries are created using curly braces {} or the dict() constructor. They are commonly used to represent mappings and lookups.

  5. Strings: Strings are sequences of characters. They are created using single quotes '', double quotes "", or triple quotes """. Strings in Python are immutable, meaning they cannot be modified after creation. Python provides many string manipulation methods for working with strings.

  6. Arrays: Arrays are similar to lists but can only contain elements of the same data type. They are created using the array module and provide more efficient storage and operations for numerical data.

  7. Linked Lists: Linked lists are a data structure composed of nodes, where each node contains a value and a reference to the next node. Linked lists can be implemented using custom classes in Python and are useful for dynamically growing and manipulating data.

  8. Stacks: Stacks are a type of data structure that follows the Last-In-First-Out (LIFO) principle. They can be implemented using lists or the deque class from the collections module. Stacks are commonly used in algorithms involving depth-first search and backtracking.

  9. Queues: Queues are a type of data structure that follows the First-In-First-Out (FIFO) principle. They can be implemented using lists or the deque class from the collections module. Queues are commonly used in algorithms involving breadth-first search and task scheduling.

These are the main data structures available in Python. Each data structure has its own characteristics and use cases, and understanding them allows you to choose the appropriate one for your specific needs.

Tasks

Task 1: Give the Difference between List, Tuple, and Set

ListTupleSet
DefinitionOrdered, mutable collection of elementsOrdered, immutable collection of elementsUnordered, mutable collection of unique elements
SyntaxSquare brackets []Parentheses () or without any bracketsCurly braces {} or set() constructor
MutableYesNoYes
IndexingYesYesNo
DuplicatesAllowedAllowedNot allowed (automatically removes duplicates)
UsageSuitable when elements may change or need to be modifiedSuitable when elements should remain constant or not be modifiedSuitable for removing duplicates or performing mathematical set operations
Examplefruits = ["apple", "banana", "orange"]coordinates = (10, 20)unique_numbers = {1, 2, 3, 4, 5}

Lists :

numbers = [2, 4, 6, 8, 10]
numbers.extend([12, 14, 16])
print("Extended:", numbers)

From this example, we can understand that List is mutable.

Tuples :

student = ("John Doe", 25, "Computer Science")
student[1] = 26

From this output, we can understand that tuples are immutable.

Sets :

fruits = {'apple', 'banana', 'orange', 'mango'}
print(fruits)

From the output, we can understand that sets are Unordered.

Task 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"}
print("My favorite tools is ",fav_tools[3])

  1. fav_tools = {1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef"}: This line creates a dictionary called fav_tools. The dictionary contains key-value pairs, where the keys are integers (1, 2, 3, etc.) and the corresponding values are strings representing different tools.

  2. print("My favorite tool is ", fav_tools[3]): This line prints the value associated with the key 3 in the fav_tools dictionary. The expression fav_tools[3] retrieves the value "Docker" from the dictionary based on the key

  3. The output will be My favorite tool is Docker.

Task 3: Create a List of cloud service providers eg. 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")
print(cloud_providers)
cloud_providers.sort()
print(cloud_providers)

The code you provided demonstrates the use of a list data structure in Python. Here's a step-by-step explanation of what each line does:

  1. cloud_providers = ["AWS", "GCP", "Azure"]: This line creates a list called cloud_providers and initializes it with three strings: "AWS", "GCP", and "Azure". The list represents different cloud service providers.

  2. cloud_providers.append("Digital Ocean"): The append() method is used to add an element to the end of the list. In this case, it adds the string "Digital Ocean" to the cloud_providers list.

  3. print(cloud_providers): This line prints the updated cloud_providers list after adding "Digital Ocean". The output will be ["AWS", "GCP", "Azure", "Digital Ocean"].

  4. cloud_providers.sort(): The sort() method is used to sort the elements of a list in ascending order. In this case, it sorts the cloud_providers list in alphabetical order.

  5. print(cloud_providers): This line prints the sorted cloud_providers list. The output will be ["AWS", "Azure", "Digital Ocean", "GCP"].

I can be reached on LinkedIn as Prajwal Zalaki to provide feedback, suggestions, and corrections for my blog. Your input is highly valued, and I am committed to enhancing the quality of my content. Please feel free to connect with me and share your insights.