Python Data Types and Data Structures for DevOps

Python

Python is a flexible, high-level programming language for diverse tasks, including web development, data analysis, scientific computing, automation, and more. Python consists of vast libraries and various frameworks like Django, Tensorflow, Flask, Pandas, Keras, etc. Python is known for its simplicity and readability. Python is an open-source programming language, which means it is freely available to the public to use and modify without any restrictions.

Basic Python Data Types

  1. Booleans (bool): are used to represent true or false values. Booleans are used for logical operations and conditional statements.

    is_true = True

    is_false = False

  2. Integers (int): are used to represent whole numbers, both positive and negative, without a decimal point.

    a = 7

  3. Floating-point Numbers (float): are used to represent numbers with decimal points.

    b = 2.34

  4. Strings (str): are used to represent text data.

    text = "Hello!"

  5. Lists (list): are ordered collections of items. Lists can contain elements of different data types.

    my_list = [1, 2, "three"]

  6. Sets (set): are unordered collections of unique elements. They are often used to eliminate duplicates or perform mathematical set operations.

    unique_numbers = {1, 2, 3, 4}

  7. Dictionaries (dict): contains key-value pairs. It is useful for creating mappings and associative arrays.

    person = {"name": "Jane", "age": 30}

  8. Tuples (tuple): are similar to lists but immutable, meaning you cannot change the elements after creation.

    (1, 2, 3, 4)

  9. NoneType (None): Represents the absence of a value. It's often used to indicate the lack of a specific result or uninitialized variables.

    result = None

Data Structures

Data Structures are a way of organizing data to be accessed more efficiently depending on the situation. Data Structures are fundamentals of any programming language a program uses. Python helps to learn the fundamentals of these data structures more simply than other programming languages.

  • Lists Python: Lists are like the arrays declared in other languages, an ordered data collection. It is very flexible, as the items in a list do not need to be of the same type.

  • Tuple Python: Tuple is a collection of Python objects much like a list, but Tuples are immutable, i.e., the elements in the tuple cannot be added or removed once created. Like a List, a Tuple can also contain elements of various types.

  • Dictionary: Python dictionary is like hash tables in any other language with the time complexity of O(1). It is an unordered collection of data values used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary contains the key: value pair. Key-value is provided in the Dictionary to make it more optimized.

Tasks

  1. Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.
CharacteristicListsTuplesSets
MutabilityMutable (can be changed)Immutable (cannot be changed)Mutable (can be changed)
SyntaxEnclosed in square brackets [ ]Enclosed in parentheses ( )Enclosed in curly braces { }
OrderOrdered (elements maintain the order of insertion)Ordered (elements maintain the order of insertion)Unordered (elements have no specific order)
DuplicatesAllows duplicate elementsAllows duplicate elementsDoes not allow duplicate elements
AccessElements can be accessed by indexElements can be accessed by indexElements can be accessed using iteration (no index)
Examplesmy_list = [1, 2, 3]my_tuple = (1, 2, 3)my_set = {1, 2, 3}

Lists

Tuples

Sets

  1. Create the below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.

    fav_too l =

    { 1: "Linux", 2: "Git", 3: "Docker", 4: "Kubernetes", 5: "Terraform", 6: "Ansible", 7: "Chef" }

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

Thanks for reading!