Introduction to Python for DevOps
The most widely used language for automation, tooling, and glue code in DevOps.
Python is the default language of DevOps automation. Itβs readable, runs everywhere, and has libraries for almost everything β talking to cloud APIs, parsing logs, calling web services, and wiring tools together. If Bash starts feeling painful, Python is the next step up.
π― Learning Objectives
By the end of this lesson you will:
- Understand why DevOps engineers reach for Python
- Install Python and check the version
- Run code interactively and from a file
- Write and run your first script
Why Python for DevOps?
You already know Bash from the Linux section. Bash is perfect for short command sequences, but it gets messy fast for anything with logic, data, or APIs. Python shines exactly there:
| Task | Bash | Python |
|---|---|---|
| Chain a few commands | β Great | Overkill |
| Parse JSON from an API | π Painful | β Built-in |
| Loops with real data structures | π Clunky | β Clean |
| Call AWS/Azure APIs | β | β (SDKs) |
| Readable by the whole team | β οΈ | β |
π‘ Bash and Python are teammates, not rivals
A good DevOps engineer uses Bash for quick glue and Python when thereβs real logic or data involved. Knowing when to switch is a skill in itself.
Install Python
Most Linux systems and macOS already ship with Python 3. Check first:
python3 --version
pip3 --versionIf itβs missing on Ubuntu/WSL:
sudo apt update && sudo apt install -y python3 python3-pipβ Use python3, not python
On many systems python still points to old Python 2 (or nothing at all). Always use python3 and pip3 to be safe.
Two Ways to Run Python
1. The interactive shell (REPL) β great for quick experiments:
2. A script file β how real automation lives. Create hello.py:
print("Hello, DevOps!")
print("Python is running from a file.")Then run it:
π§ͺ Hands-on
Your First Python Script
- Confirm Python 3 is installed (
python3 --version). - Open the interactive shell and calculate
24 * 60 * 60(seconds in a day). - Create a file
server.pythat prints your name and a short message. - Run it with
python3 server.py.
π§ Knowledge Check
Why do DevOps engineers often prefer Python over Bash for complex tasks?
Which command correctly checks the installed Python 3 version?
πΌ Interview Preparation
When would you choose Python over a shell script for automation?
Summary
You now know why Python is central to DevOps, how to install it, and how to run code both interactively and from a file. Next, we cover the language basics β variables, data types, and control flow.