☑️Day 2: Python For DevOps

Welcome back to my Python for DevOps journey! 🚀

Today, I’m excited to share some essential Python concepts that are key for any aspiring DevOps engineer. We'll explore data structures like lists to handle dynamic data, learn how to use dictionaries to make clean and efficient choices in our code, and dive into the powerful os library for interacting with the operating system. These tools are helping me write more efficient scripts and better manage my system environments—and I hope they’ll do the same for you!

Let’s continue building our DevOps skills together!

🔹Table Of content :

✅ Mastering Python Lists

✅ Implementing Switch Cases in Python

Exploring the os Library

✅ Conclusion and Next Steps

✅1. Mastering Python Lists

Lists are one of the most versatile data structures in Python. They allow you to store multiple items in a single variable. Lists are mutable, meaning you can modify them after creation, making them perfect for dynamic data handling in DevOps scripts.

Here are some basic operations you can perform with lists:

  • Append: Add an item to the end of the list.

  • Insert: Insert an item at a specific position.

  • Length: Get the number of items in a list.

# Creating a list
servers = ['server1', 'server2', 'server3']
print(servers)  # Output: ['server1', 'server2', 'server3']

# Append - Adding a new server to the list
servers.append('server4')
print(servers)  # Output: ['server1', 'server2', 'server3', 'server4']

# Insert - Adding a server at a specific position
servers.insert(1, 'server5')
print(servers)  # Output: ['server1', 'server5', 'server2', 'server3', 'server4']

# Length - Finding the number of servers
num_servers = len(servers)
print(f"Number of servers: {num_servers}")  # Output: Number of servers: 5

✅2. Implementing choices in Python

Instead of a traditional switch-case, Python developers often use dictionaries to manage choices or conditions. This approach is useful for creating clean, readable code when you have multiple options to handle.

# choice to do arthimetic operations

num1=int(input("Enter 1st number: "))
num2=int(input("Enter 2nd number: "))

choice= input("Enter the operation you want to perform: (Options +,-,*,/,%)")

if choice == "+":
    sum=num1 + num2
    print("Addition: ",sum)
elif choice =="-":
    diff=num1-num2
    print("subtraction: ",diff)
elif choice =="*":
    prod=num1*num2
    print("multiplication: ",prod)
elif choice =="/":
    div=num1/num2
    print("division: ",div)
elif choice=="%":
    mod=num1%num2
    print("remainder: ",mod)
else:
    print("Invalid choice")

3. Exploring the os Library

The os library in Python provides a way to interact with the operating system. This is incredibly powerful for DevOps, as it allows you to automate tasks like file manipulation, environment management, and process handling.

Examples of Common os Operations:

  • Get Current Working Directory: Find out where your script is currently running.

  • List Files in a Directory: See the files in a specific directory.

  • Execute System Commands: Run shell commands directly from your Python script.

# python libraries-----
#                     |--> OS (operating sys library)
#                     |--> shutil (shell utlis)
#                     |--> system (sys level)


# Importing a library for os
import os 

# linuc cmd to see diskspace i.e df -h
print(os.system('df -h'))

# we can see kab se sys chalu hey i.e uptime and load average

print(os.system('uptime')) #for linux and mac


print(os.system('systeminfo')) #for windows

print(os.system('sysctl hw.memsize')) #for linux and mac

# Get the current working directory
current_dir = os.getcwd()
print(f"Current Directory: {current_dir}")

# List files in the current directory
files = os.listdir(current_dir)
print(f"Files in {current_dir}: {files}")

# Execute a system command
os.system('echo Hello from Python!')

✅4. Conclusion and Next Steps

Today, you’ve learned about Python lists, how to handle multiple choices with dictionaries, and explored the powerful os library. These tools will help you create more dynamic and efficient DevOps scripts.

Keep practicing these concepts, and don’t forget to apply them in real-world scenarios! Tomorrow, we’ll continue to explore more Python essentials for DevOps. 🚀

Keep Coding, Keep Learning!

Stay tuned for Day 3! #90DaysOfDevOps

Happy Learning!🚀

💡
Follow for more updates on LinkedIn, Github and Twitter(X)
5
Subscribe to my newsletter

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

Written by

Kedar Pattanshetti
Kedar Pattanshetti