📍Introduction :
Today, we'll explore Python's powerful data types and data structures, essential for any DevOps engineer.
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 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)
Data Structure
In Python, data structures are like containers that help us store and manage information neatly and efficiently. They are tools that make working with data simple and organized. Here are some common data structures in Python:
Lists: Lists are like ordered shopping bags that can hold different things together. You can add, remove, or change items in the bag.
Tuples: Tuples are similar to lists but more like sealed packages. Once you put things inside, you can't change them.
Dictionaries: Dictionaries work like phone books, where you find names (keys) and their corresponding numbers (values). It helps you quickly find what you need.
Sets: Sets are like collections of unique items, just like a group of friends without any duplicates.
Strings: Strings are like sentences made up of letters. They help us work with text and words.
Arrays: Arrays are specialized tools to work with numbers efficiently. They are helpful for advanced math and calculations.
💼Task 1 :
Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.
List
Lists are ordered collections of items, and they are mutable, meaning you can change their contents after creation. Lists are enclosed in square brackets [ ]
and can contain elements of different data types.
For example,
List of having only strings
a = ["apple", "banana", "orange"]
print(a)
You can access elements in a list using index positions, and you can modify, add, or remove elements.
Tuples :
Tuples are similar to lists, but they are immutable, meaning you cannot change their contents after creation. Tuples are enclosed in parentheses ( ).
colors = ("red", "green", "blue")
print(colors)
Tuples are useful when you want to create a fixed collection of elements that should not be modified.
Dictionaries :
Dictionaries are unordered collections of key-value pairs, and they are mutable. Each key in a dictionary must be unique. Dictionaries are enclosed in curly braces { }.
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person)
You can access values in a dictionary using their keys, and you can add, modify, or delete key-value pairs
List: Use lists when you need an ordered collection of elements that can be modified.
Tuple: Use tuples when you want an ordered collection of elements that should not be modified.
Dictionary: Use dictionaries when you need to store data as key-value pairs, and you want to be able to access, add, modify, or delete the data based on its keys.
💼Task 2 :
Create 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"
}
print(fav_tools)
print("My favourite tool is", fav_tools[4])
print("My next favourite tool is", fav_tools[5])
💼Task 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.
print ("List of cloud service providers")
cloud_providers = ["AWS","GCP","Azure"]
print (cloud_providers)
print ("adding another cloud service provider Digital Ocean & sorting the list in alphabetical order")
cloud_providers.append("Digital Ocean")
cloud_providers.sort()
print (cloud_providers
📍 Conclusion :
Python's data types and data structures are the building blocks of DevOps. They enable you to handle data with ease and efficiency. Whether it's lists, tuples, or dictionaries, understanding and using these data structures will make your Python programming journey smoother and more enjoyable.
Thank you for reading!
Contact me on : Linkedin 🤝
Check out my GitHub for more resources 📚