Day 14 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start 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 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 fundamentals of these data structures in a simpler way as compared to other programming languages.
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 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.
Set is a collection of all unique elements. Indexing is not supported in sets.
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.
Tasks:
- Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.
# List is a complex data type in python.
# List are one of 4 built in data type in python used to store collections of data.
# List are used to store multiple items in a single variable.
thisList = ["apple", "banana", "grapes"]
print(thisList)
# Tuples are like List (sequence of objects) but they are immutable i.e., once they have been defined we cannot change them.
# Tuples are faster than List.
# Tuples are used to store multiple items in a single variable.
thisTuple = ("apple", "banana", "grapes")
print(thisTuple)
# Set are a collection of all unique elements.
# A set is a collection which is unordered, unchangeable*, and unindexed.
thisSet = {"apple", "banana", "grapes"}
print(thisSet)
- Create below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionary.
# Dictionaries are used to store data values in key:value pairs.
# A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
fav_tools = {
1:"Linux",
2:"Git",
3:"Docker",
4:"Kubernetes",
5:"Terraform",
6:"Ansible",
7:"Chef"
}
print(fav_tools[1])
- Create a List of cloud service providers.
cloud_providers = ["AWS", "GCP", "Azure"]
print(cloud_providers)
Write a program to add Digital Ocean
to the list of cloud_providers and sort the list in alphabetical order.
cloud_providers = ["AWS", "GCP", "Azure", "Alibaba",]
print(cloud_providers)
cloud_providers.append("Digital Ocean")
print(sorted(cloud_providers))
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)