Day 11: Mastering Dictionaries and Sets in Python for DevOps

CHANDRESH PATLECHANDRESH PATLE
3 min read

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

0
Subscribe to my newsletter

Read articles from CHANDRESH PATLE directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CHANDRESH PATLE
CHANDRESH PATLE

Hi, I'm Chandresh Patle, an aspiring DevOps Engineer with a diverse background in field supervision, manufacturing, and service consulting. With a strong foundation in engineering and project management, I bring a unique perspective to my work. I recently completed a Post Graduate Diploma in Advanced Computing (PG-DAC), where I honed my skills in web development, frontend and backend technologies, databases, and DevOps practices. My proficiency extends to Core Java, Oracle, MySQL, SDLC, AWS, Docker, Kubernetes, Ansible, Linux, GitHub, Terraform, Grafana, Selenium, and Jira. I am passionate about leveraging technology to drive efficient and reliable software delivery. With a focus on DevOps principles and automation, I strive to optimize workflows and enhance collaboration among teams. I am constantly seeking new opportunities to expand my knowledge and stay up-to-date with the latest industry trends. If you have any questions, collaboration ideas, or professional opportunities, feel free to reach out to me at patle269@gmail.com. I'm always open to connecting with fellow tech enthusiasts and exploring ways to contribute to the DevOps community. Let's build a better future through innovation and continuous improvement!