#90DaysofDevOps #Day 14 Python Data Types and Data Structures for DevOps

#90DaysofDevOps #Day 14 Python Data Types and Data Structures for DevOps

Data Types

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

  • Everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

  • Python has the following data types built-in by default: Numeric(Integer, complex, float), Sequential(string,lists, tuples), Boolean, Set, Dictionaries, etc.

  • To check the datatypes: type(variable_name).

    Data Structures

    It is a way of organizing data so that it can be accessed more efficiently depending upon the condition. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

    • 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

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

    • 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 holds the key:value pair. Key-value is provided in the dictionary to make it more optimized.

      Difference between List, Tuple and set.

    • The main characteristics of lists are –

      • The list is a datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.

      • List are mutable .i.e it can be converted into another data type and can store any data element in it.

      • List can store any type of element.

    • # Creating a List

      List = []

      print("Blank List: ")

      print(List)

      # Creating a List of numbers

      List = [10, 20, 14]

      print("\nList of numbers: ")

      print(List)

      # Creating a List of strings and accessing

      # using index

      List = ["Train", "With", "Subham"]

      print("\nList Items: ")

      print(List[0])

      print(List[2])

    • The main characteristics of tuples are –

    • Tuple is an immutable sequence in python.

    • It cannot be changed or replaced since it is immutable.

    • It is defined under parenthesis().

    • Tuples can store any type of element.

    • # Creating an empty Tuple

      Tuple1 = ()

      print("Initial empty Tuple: ")

      print (Tuple1)

      # Creating a Tuple with

      # the use of list

      list1 = [1, 2, 4, 5, 6]

      print("\nTuple using List: ")

      print(tuple(list1))

      #Creating a Tuple

      #with the use of built-in function

      Tuple1 = tuple('Goutam')

      print("\nTuple with the use of function: ")

      print(Tuple1)

    • The main characteristics of set are –

      • Sets are an unordered collection of elements or unintended collection of items In python.

      • Here the order in which the elements are added into the set is not fixed, it can change frequently.

      • It is defined under curly braces{}

      • Sets are mutable, however, only immutable objects can be stored in it.

      • # Python3 program to demonstrate

        # Set in Python

        # Creating a Set

        set1 = set()

        print("Initial blank Set: ")

        print(set1)

        # Creating a Set with

        # the use of Constructor

        # (Using object to Store String)

        String = 'Trainwithsubham'

        set1 = set(String)

        print("\nSet with the use of an Object: " )

        print(set1)

        # Creating a Set with

        # the use of a List

        set1 = set(["Train", "With", "Subham"])

        print("\nSet with the use of List: ")

        print(set1)

      • Difference between List, Set, and Tuple

        | List | Set | Tuple | | --- | --- | --- | | Lists is Mutable | Set is Mutable | Tuple is Immutable | | It is Ordered collection of items | It is Unordered collection of items | It is Ordered collection of items | | Items in list can be replaced or changed | Items in set cannot be changed or replaced | Items in tuple cannot be changed or replaced |