Welcome back to our Python for DevOps series! In today's session, we'll dive into the powerful world of dictionaries and sets, key components in Python for managing and organizing data efficiently. Let's explore their functionalities and delve into practical examples to elevate your DevOps scripting skills.
๐ถ Dictionaries and Key-Value Pairs ๐ถ
โ What are Dictionaries?
Dictionaries are versatile data structures in Python that store data as key-value pairs. Each key must be unique, and it maps to a specific value. This structure facilitates efficient data retrieval and manipulation.
# Define a dictionary named 'student_info' containing information about a student
student_info = {
"name": "Chandresh", # Key: "name", Value: "Chandresh" (Student's name)
"age": "12", # Key: "age", Value: "12" (Student's age)
"class": "X" # Key: "class", Value: "X" (Student's class)
}
# Print the value associated with the "name" key from the 'student_info' dictionary
print(student_info["name"]) # Output: Chandresh
# Define a list named 'student_info' containing dictionaries with information about students
student_info = [
{
"name": "Chandresh", # Key: "name", Value: "Chandresh" (Student's name)
"age": "12", # Key: "age", Value: "12" (Student's age)
"class": "X" # Key: "class", Value: "X" (Student's class)
},
{
"name": "Harish", # Key: "name", Value: "Harish" (Student's name)
"age": "32", # Key: "age", Value: "32" (Student's age)
"class": "XII" # Key: "class", Value: "XII" (Student's class)
},
{
"name": "Anshika", # Key: "name", Value: "Anshika" (Student's name)
"age": "8", # Key: "age", Value: "8" (Student's age)
"class": "III" # Key: "class", Value: "III" (Student's class)
}
]
# Print the information for each student in the 'student_info' list
print(student_info[1]["class"])
# for student in student_info:
# print("Name:", student["name"])
# print("Age:", student["age"])
# print("Class:", student["class"])
# print("\n")
# Import the requests module to make HTTP requests
import requests
# Specify the URL for the GitHub API endpoint to retrieve pull requests for the Kubernetes repository
url = 'https://api.github.com/repos/kubernetes/kubernetes/pulls'
# Send a GET request to the specified URL and store the response
response = requests.get(url)
# Print the HTTP response status code
print(response)
#output will be
#----# <Response [200]>
# Import the requests module to make HTTP requests
import requests
# Specify the URL for the GitHub API endpoint to retrieve pull requests for the Kubernetes repository
url = 'https://api.github.com/repos/kubernetes/kubernetes/pulls'
# Send a GET request to the specified URL and store the response
response = requests.get(url)
# Parse the response content as JSON and store the complete details of the pull requests
complete_details = response.json()
# Print the ID and title of the first pull request in the list
print(complete_details[0]["id"])
print(complete_details[0]["title"])
# Iterate through the list of pull requests and print the login of the user who created each pull request
for i in range(len(complete_details)):
print(complete_details[i]["user"]["login"])
๐ถ Sets and Set Operations ๐ถ
โ What are Sets?
Sets are unordered collections of unique elements in Python. They are useful for performing mathematical set operations like union, intersection, and difference.
# Example of a set
unique_ports = {80, 443, 8080, 22}
๐ถ Conclusion
Understanding dictionaries and sets is crucial for effective data management in DevOps scripts. These data structures provide a convenient way to organize and retrieve information. Stay tuned for more advanced topics as we continue our Python for DevOps journey!
Note: I am following Abhishek Verraamalla's YouTube playlist for learning.
GitHub Repo: https://github.com/Chandreshpatle28/python-for-devops-av
Happy Learning :)
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps โพ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #PythonforDevOps #python