Python Automation for DevOps 🔥 | If Else, For Loop, While Loop Explained

 As a DevOps engineer, automation is part of our daily work. Tools like Python help us automate tasks, manage systems, and write scripts efficiently.

Today, I’m going to explain Conditions, Loops, and Import in Python in a very simple and practical way.

What is a Condition in Python?

A condition means checking something as True or False.

Example:

i < 3

Explanation:

  • i → variable
  • < → condition operator
  • 3 → value

In simple words:
👉 Check if i is less than 3

Example: Simple Calculator Using Conditions

Now, open VS Code and write the following code:

num1 = float(input("Enter num 1: "))   # Taking first number
num2 = float(input("Enter num 2: ")) # Taking second number
opr = input("Enter operator (+, -, *): ")
output = None  # Default value
if opr == "+":
output = num1 + num2
elif opr == "-":
output = num1 - num2
elif opr == "*":
output = num1 * num2
print("Your calculation is:", output)

Why output = None is important?

  • If the user enters a valid operator, the result is stored in output
  • If not, output remains None
  • This avoids errors and keeps code safe (very important in DevOps scripts)

Using If, Elif, and Else Conditions

if num1 < 3:
print("num1 is less than 3")
elif num1 == 3:
print("num1 is equal to 3")
else:
print("num1 is greater than 3")

Explanation:

  • if → first condition
  • elif → extra condition
  • else → runs when all conditions are false

👉 If num1 = 3

  • First condition is false
  • Second condition is true
  • Output: num1 is equal to 3

This is how decision-making works in Python.

What is a Loop?

A loop means doing the same task again and again.

Example in DevOps:

  • Checking server status repeatedly
  • Running health checks
  • Monitoring logs

Types of Loops in Python

  1. For Loop
  2. While Loop

For Loop Example

Create a new file: loops.py

for i in range(10):
print(i)

Explanation:

  • i → variable
  • range(10) → numbers from 0 to 9
  • print() → displays value

👉 Output:

0
1
2
3
...
9

How to Run:

Ctrl + S   # Save file
python loops.py

Understanding range() Function

range(start, end, step)

Example:

for i in range(0, 21, 2):
print(i)

Explanation:

  • Start → 0
  • End → 21 (not included)
  • Step → 2

👉 Output (Even numbers):

0 2 4 6 8 10 12 14 16 18 20

While Loop Example

i = 0
while i < 5:
print(i)
i = i + 1

Explanation:

  • Loop starts from 0
  • Runs while i < 5
  • i = i + 1 increases value every time
  • Without increment → infinite loop (dangerous in DevOps 😄)

What is Import in Python?

Import means bringing something from outside.

In Python, many built-in libraries already exist (like DevOps tools).

Import Example with time Library

import time
i = 0
while i < 5:
print(i)
i = i + 1
while True:
time.sleep(2)
print("boom")

Explanation:

  • import time → importing time library
  • time.sleep(2) → wait for 2 seconds
  • while True → infinite loop
  • Prints boom every 2 seconds

👉 This type of logic is used in:

  • Monitoring scripts
  • Background services
  • Health checks

Final Words

As a DevOps engineer:

  • Conditions help in decision-making
  • Loops help in automation
  • Import helps us use powerful libraries

I hope this blog made Python basics very easy for you.
Please share and follow for more DevOps + Python automation content 🚀

🌐 Online References


Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator