Python Data Types and Data Structures for DevOps

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.

  • Since everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes.

  • Python has the following data types built in by default:

    Numeric data (Integer, complex, float)

    Sequential data (string, lists, tuples)

    Boolean data

    Set, Dictionaries, etc.

  • To check what is the data type of the variable used, we can simply write: your_variable=100 type(your_variable)

Data Structures

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

  • Lists in Python: Lists are just like 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

  • Tuple in 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. Just like a List, a Tuple can also contain elements of various types.

  • Dictionary in 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, a Dictionary holds 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.

  1. Lists: Lists are like dynamic-sized arrays, declared in other languages such as ArrayList in Java. It is the most powerful tool in Python. The main characteristics of lists are –

    A list is an ordered collection of data with elements separated by a comma and enclosed within square brackets.

    Lists are mutable and can be modified after creation.

    Lists can store any type of element.

    Example:

  2. Tuple: Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’. The main characteristics of tuples are –

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

    Example:

  3. Set: Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements. The major advantage of using a set is that it has a highly optimized method for checking whether a specific element is contained in the set. The main characteristics of set are –

    Sets are an unordered collection of elements or unintended collections 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.

    Example:

2] Create the 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"
}

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.

[Hint: Use keys to built-in functions for Lists]

That's all for today

References :

Data structures and Algorithms

Python for begineer


Thanking for reading!

Thanks for your valuable time.

Keep learning with me and stay happy 😊