day14 of #90daysofdevops
Datastructures in python for devops engineers
Photo by Andrew Neel on Unsplash
Today we are going to learn about data structures in python.
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 (objects) 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)
output : <class int>
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: 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 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.
List Tuple Set A list is a collection of ordered data. A tuple is an ordered collection of data. A set is an unordered collection of data. The list is represented by [] A tuple is represented by () Set is represented by {} The list is mutable. This means we can edit/change elements inside the list A tuple is immutable which means we can't able to change values once declared The set is also mutable which means we can change the elements in it. But, unlike a list duplicate elements are not allowed The list is a non-homogeneous data structure that stores elements in a single row, multiple rows and columns A tuple is a non-homogeneous data structure that stores elements in a single row, multiple rows and columns Set is a non-homogeneous data structure that stores elements in only one row EX: list1=[1,[2,3],(4,5), False, 'No'] EX: colors = ('Red', 'Green', 'Blue') EX : myset={2,3,5,6}
Create the below Dictionary and use Dictionary methods to print your favorite 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" }
Here is my favorite DevOps tool i.e; docker. which is present with key 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]
Here we have used the "append" and "sort" built-in functions of lists in python.
That's it for Today. Thanks for your time.
Please share your valuable feedback by liking 👍, sharing🤝 and commenting.
See you tomorrow, with another blog.
#day14 #90DaysOfDevops #python #DevOps