Python Data Types and Data 
           Structures for DevOps

Python Data Types and Data Structures for DevOps

Python open-source language, meaning it is free to use and its source code can be modified by anyone. Python is widely used in various fields, including web development, data science, artificial intelligence, and automation.

One of the key strengths of Python is its simplicity and readability. It has a relatively easy-to-learn syntax that emphasizes code readability and reduces the cost of program maintenance. Python code is often described as being "executable pseudocode" because of its clear, concise, and expressive nature.

Data Types

In Python, a data type is a classification of data items, which tells the compiler or interpreter how to interpret the data.

  1. Numeric Types: Numeric data types are used to represent numbers. Python has three types of numeric data types: int (integer), float (floating-point), and complex (complex numbers). Integers represent whole numbers, while floating-point numbers represent decimal numbers. Complex numbers are represented by a real and imaginary component.

  2. Boolean Type: The Boolean data type is used to represent logical values. The two Boolean values are True and False. They are often used in conditional statements and loops.

  3. String Type: The string data type is used to represent a sequence of characters. Strings are enclosed in quotation marks (either single or double). They can be manipulated using various built-in functions and operators.

  4. List Type: The list data type is used to represent an ordered sequence of elements. Elements can be of different data types, including other lists. Lists are mutable, which means that they can be modified by adding, removing, or modifying elements.

  5. Tuple Type: The tuple data type is used to represent an ordered sequence of elements, similar to lists. However, tuples are immutable, meaning they cannot be modified once created.

  6. Set Type: The set data type is used to represent an unordered collection of unique elements. Sets can be modified by adding or removing elements.

  7. Dictionary Type: The dictionary data type is used to represent a collection of key-value pairs. Each key must be unique, and its corresponding value can be of any data type.

Data Structures

In Python, a data structure is a way of organizing and storing data in a computer memory so that it can be accessed and used efficiently.

  1. List: A list is an ordered collection of elements. Elements can be of different data types, including other lists. Lists are mutable, meaning they can be modified by adding, removing, or modifying elements. Lists are enclosed in square brackets.

  2. Tuple: A tuple is an ordered collection of elements, similar to lists. However, tuples are immutable, meaning they cannot be modified once created. Tuples are enclosed in parentheses.

  3. Set: A set is an unordered collection of unique elements. Sets can be modified by adding or removing elements. Sets are enclosed in curly braces.

  4. Dictionary: A dictionary is a collection of key-value pairs. Each key must be unique, and its corresponding value can be of any data type. Dictionaries are enclosed in curly braces, and each key-value pair is separated by a colon.

  5. Strings: Strings in Python are sequences of characters and are also considered as a data structure. They are immutable, which means they cannot be modified once created.

  6. Arrays: Python also has built-in support for arrays. Arrays are similar to lists, but all elements must be of the same data type. Arrays are more memory-efficient than lists when working with large sets of data.

  7. Stack: A stack is a last-in, first-out (LIFO) data structure. Elements are added and removed from the top of the stack.

  8. Queue: A queue is a first-in, first-out (FIFO) data structure. Elements are added to the rear and removed from the front.

Task 1:Difference between List, Tuple and set

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 replacedItems in tuple cannot be changed or replaced

List Example

Devopstool=[10]
Devopstool[0:5]="Docker","Jenkins","grafana","Anisble","AWS"
print(Devopstool)
[pavan@localhost Documents]$ python3 python.py
['Docker', 'Jenkins', 'grafana', 'Anisble', 'AWS']

Tuple Example

Devopstool=("Docker","Jenkins","grafana","Anisble","AWS")
print(Devopstool)
[pavan@localhost Documents]$ python3 python.py
['Docker', 'Jenkins', 'grafana', 'Anisble', 'AWS']

Set Example

Devopstool={"Docker","Jenkins","grafana","Anisble","AWS"}
print(Devopstool)
[pavan@localhost Documents]$ python3 python.py
['Docker', 'Jenkins', 'grafana', 'Anisble', 'AWS']

Task 2:Create 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"
}
print(fav_tools[3])
[pavan@localhost Documents]$ python3 python.py
Docker

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

cloud_providers = ["AWS","GCP","Azure"]
cloud_providers.append("Digital Ocean")
cloud_providers.sort()
print(cloud_providers)
[pavan@localhost Documents]$ python3 python.py
['AWS', 'Azure', 'Digital Ocean', 'GCP']

#Python #DevOps

Please free to write your thoughts and can connect with me on LinkedIn