Essential Python Data Types and Data Structures for Successful DevOps Implementation
Day-14 #90daysofdevops
Table of contents
- Data Types
- Data Structures
- Task-1 Give the Difference between List, Tuple, and set. Do Handson and put screenshots as per your understanding.
- Task 2 Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.
- Task -3 Create a List of cloud service providers and Write a program to add Digital Ocean to the list of cloud providers and sort the list in alphabetical order.
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
Integer
complex
float
string
Boolean
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
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 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
- 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, Dictionary holds the key: value pair. Key-value is provided in the dictionary to make it more optimized
Task-1 Give the Difference between List, Tuple, and set. Do Handson and put screenshots as per your understanding.
1) List - Lists are mutable, meaning you can change, add, or remove elements after creation.
2)Tuple - Tuples are immutable, meaning that once created, their elements cannot be changed or modified.
3)Set - Sets are mutable, allowing you to add or remove elements after creation.
Task 2 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" }
>>> fav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef" }
>>> print(fav_tools)
{1: 'Linux', 2: 'Git', 3: 'Docker', 4: 'Kubernetes', 5: 'Terraform', 6: 'Ansible', 7: 'Chef'}
>>> print(fav_tools[6])
Ansible
Task -3 Create a List of cloud service providers and 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"]
>>> print(cloud_providers)
['AWS', 'GCP', 'Azure']
>>>
>>> cloud_providers.append("Digital Ocean")
>>> print(cloud_providers)
['AWS', 'GCP', 'Azure', 'Digital Ocean']
>>>
>>>
>>> cloud_providers.sort()
>>> print(cloud_providers)
['AWS', 'Azure', 'Digital Ocean', 'GCP']