Python Data Types and Data Structures for DevOps

Python Data Types and Data Structures for DevOps

#90DaysOfDevOps

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 actually classes and variables are instances (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 what is the data type of the variable used, we can simply write: your_variable=100 type(your_variable)

Data Structure

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

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

Tasks

  • Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuples, Set, and Dictionary, all with different qualities and usage.

Lists are created using square brackets:

For example:-

a = ["Mango", "Apple", "Grapes"]

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection that is ordered and unchangeable.

For example:

tuple_ = ("Mango", "Apple", "Grapes")

Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.

A set is a collection that is unordered, unchangeable, and unindexed.

For example:-

set_ = {"apple", "banana", "cherry"}
print(set_)

  • 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" }

  • Create a List of cloud service providers eg.

    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.

Thanks for reading my article. If you like it kindly follow me.

~Prabhakar Yadav