Python is the duct tape of cloud and DevOps work — small enough to write a quick script, capable enough to build a real tool. This first lesson sets you up with the version, editor, and patterns you'll use throughout the rest of the course.
Which Python?
Use Python 3.11 or newer. Older 3.x versions still work, but newer ones bring meaningful speedups, better error messages, and standard-library improvements (like tomllib). Don't rely on whatever Python ships with your OS — distros use it for system tooling and you'll break things by upgrading it.
Recommended installers
- pyenv (Linux/macOS) — installs and switches Python versions per project
- pyenv-win or the official installer (Windows)
- uv — the new Rust-based tool from Astral; can install Python and resolve dependencies at speed
- Homebrew on macOS for a quick "just give me Python" install
# Install pyenv on macOS
brew install pyenv
pyenv install 3.12.5
pyenv global 3.12.5
python --version
# Python 3.12.5
Editor
Pick one and stick with it long enough to learn its shortcuts. Solid choices:
- VS Code with the Microsoft Python and Pylance extensions — free, lightweight, ubiquitous
- PyCharm Community — heavier, batteries included
- Neovim/Vim with pyright LSP — for terminal diehards
What you actually want from an editor: jump-to-definition, autocomplete, in-line type errors, debugging, and an integrated terminal. All three options provide all of that.
Your First Script
#!/usr/bin/env python3
"""Greet someone passed on the command line."""
import sys
def main() -> int:
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"hello, {name}")
return 0
if __name__ == "__main__":
sys.exit(main())
Save as hello.py, then:
chmod +x hello.py
./hello.py
# hello, world
./hello.py alex
# hello, alex
Three things happening here that you'll see throughout the course:
- The shebang
#!/usr/bin/env python3tells the OS how to run the file. sys.argvis the list of command-line arguments. Element 0 is the script name; the rest are user input.- The
if __name__ == "__main__":guard runsmain()when executed directly, but not when the file is imported. This is the conventional shape of any script that might also be reused as a module.
Running Python Code Three Ways
| Mode | When to use |
|---|---|
python script.py | Normal script execution |
python (REPL) | Interactive exploration; quick experiments |
python -c "code" | One-liners in shell pipelines |
Better yet, install ipython (pip install ipython) for a vastly better REPL with tab completion, syntax highlighting, and easy editing of multi-line code.
Formatting and Linting
Don't argue about style — let tools handle it. The current consensus stack:
- ruff — extremely fast linter and formatter (replacing flake8, isort, and now black)
- mypy or pyright — static type checking
pip install ruff
ruff format hello.py
ruff check hello.py
Add these as pre-commit hooks once you start collaborating; you'll never have to manually fix indentation again.
Reading Errors
Python tracebacks are read bottom-up: the last line is the actual exception, and the lines above show how you got there. Modern Python (3.11+) underlines the exact expression that failed, which makes debugging dramatically friendlier:
Traceback (most recent call last):
File "/tmp/x.py", line 5, in <module>
print(items[10])
~~~~~^^^^
IndexError: list index out of range
Don't panic at red text — read it. The traceback nearly always points at the precise problem.