Day13 - Python Data Types and Data Structures for DevOps
Python is a powerfully composed language; consequently, we don't have to characterize the sort of variable while announcing it. The interpreter binds the value implicitly to its type. Every value has a datatype, and variables can hold values.
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 (object) of these classes. These are built-in data types as follows:
What are Data Structures?
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.
my_list = [1, 2, 'a', True]
my_list.append(3) # Modifying the list by adding an element
print(my_list)
# Output:
[1, 2, 'a', True, 3]
Dictionary
Dictionaries (or dicts for short) are a central data structure. Dicts store an arbitrary number of objects, each identified by a unique dictionary key.
Dictionaries are also often called maps, hashmaps, lookup tables, or associative arrays. They allow for the efficient lookup, insertion, and deletion of any object associated with a given key.
my_dict = {} #empty dictionary
print(my_dict)
my_dict = {1: 'Python', 2: 'Java'} #dictionary with elements
print(my_dict)
#output
{}
{1: ‘Python’, 2: ‘Java’}
Tuple
Tuples are the same as lists are with the exception that the data once entered into the tuple cannot be changed no matter what. The only exception is when the data inside the tuple is mutable, only then the tuple data can be changed. The example program will help you understand better.
my_tuple = (1, 2, 'a', True)
print(my_tuple[0]) # Accessing an element of the tuple,
#Output:
1
Task 1:
Give the Difference between List, Tuple, and set. Do Handson and put screenshots as per your understanding.
In Python, list, tuple, and set are three different types of data structures, each with its own characteristics and use cases. Here's a breakdown of their differences:
List:
Lists are typically used when you need a dynamic collection of items that can be modified.
Tuple:
Set:
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"
}
TASK3:
Create a List of cloud service 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"]
.
Thank you for giving your precious time to read this blog, if any suggestions on my blog comment below and like ??the blog if you feel helpful.
Happy Learning ??!!!
? Infrastructure Engineer ? DevOps ? SRE ? MLOps ? AIOps ? Helping companies scale their platforms to an enterprise grade level
11 个月Great job on delving into Python data types and data structures! Keep up the hard work! ??? Pankaj Bawliya