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 < 3Explanation:
i→ variable<→ condition operator3→ 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,
outputremainsNone - 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 conditionelif→ extra conditionelse→ 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
- For Loop
- While Loop
For Loop Example
Create a new file: loops.py
for i in range(10):
print(i)Explanation:
i→ variablerange(10)→ numbers from0to9print()→ displays value
👉 Output:
0
1
2
3
...
9How to Run:
Ctrl + S # Save file
python loops.pyUnderstanding 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 20While Loop Example
i = 0while i < 5:
print(i)
i = i + 1
Explanation:
- Loop starts from
0 - Runs while
i < 5 i = i + 1increases 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 timei = 0
while i < 5:
print(i)
i = i + 1
while True:
time.sleep(2)
print("boom")
Explanation:
import time→ importing time librarytime.sleep(2)→ wait for 2 secondswhile 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
- LinkedIn: https://www.linkedin.com/in/raees-yaqoob-qazi-ryqs/
- Medium Blog: https://medium.com/@raeesyaqubqazi
- Blogspot: https://brillertechnologies.blogspot.com/
- Facebook Page: https://web.facebook.com/profile.php?id=61553548371216
- YouTube Channel: https://www.youtube.com/@RaeesQ.
- TikTok: https://www.tiktok.com/@mrryqs?_t=ZS-8y7t0fQfJKu&_r=1
Comments
Post a Comment