{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "---\n", "title: \"Part 2: Language Core (Control Flow & Comprehensions)\"\n", "---" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "[](https://colab.research.google.com/github/sambaiga/ds-mlops-path/blob/main/tutorials/01-python-basics/02-control-flow.ipynb) [](https://raw.githubusercontent.com/sambaiga/ds-mlops-path/main/tutorials/01-python-basics/02-control-flow.ipynb)" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "**DS-MLOps Python Foundations**\n", "\n", "**Python 3.12+ | Author: Anthony Faustine**\n", "\n", "## Before you begin\n", "\n", "This notebook assumes you have completed Part 1 (`01-python-core.ipynb`). If you have not, start there. Part 2 picks up immediately where Part 1 left off, using the same **university analytics platform** scenario, and covers everything that decides *what runs and how many times*: conditionals, pattern matching, loops, and comprehensions.\n", "\n", "> Callout markers used throughout this notebook are explained on the [book cover page](../../index.qmd#callout-guide)." ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "::: {.callout-note collapse=\"true\" icon=false}\n", "## Learning Objectives\n", "\n", "By the end of Part 2 you will be able to:\n", "\n", "| # | Skill | Covered in |\n", "|---|---|---|\n", "| 1 | Write `if` / `elif` / `else` and structural `match` / `case` | Sec. 1 |\n", "| 2 | Replace manual index counters with `for`, `enumerate`, and `zip` | Sec. 2 |\n", "| 3 | Use `while`, `break`, and `continue` for indefinite loops | Sec. 3 |\n", "| 4 | Replace loops with list, dict, set, and generator comprehensions | Sec. 4 |\n", ":::\n" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## 1. Control Flow: if / elif / else & match / case\n", "\n", "So far every cell runs its lines from top to bottom, once, in order. **Control flow** lets you change that:\n", "- `if / elif / else`: run one branch based on a condition\n", "- `match / case`: route structured data to different handlers (Python 3.10+)\n", "\n", "
describe_status(code) function using match/case that returns a short description.\n",
"describe_status(200) -> '200 OK'\n",
"describe_status(404) -> '404 Not Found'\n",
"describe_status(500) -> '500 Server Error'\n",
"describe_status(301) -> '3xx Redirect'\n",
"describe_status(999) -> 'Unknown code'\n",
"Hint: Use case 2xx patterns are not valid. Use guard conditions instead: case c if 200 <= c < 300.\n",
"for + .append() loops and are idiomatic Python.\n",
"\n",
"| [expr for x in it if cond] | list |
| {k: v for x in it if cond} | dict |
| {expr for x in it if cond} | set |
| (expr for x in it if cond) | generator (lazy, no list in memory) |
records.\n",
"records = [\n",
" {'name': 'Alice', 'scores': [88, 92, 85]},\n",
" {'name': 'Bob', 'scores': [62, 70, 58]},\n",
" {'name': 'Carol', 'scores': [91, 95, 89]},\n",
"]\n",
"\n",
"# 1. List of averages (one float per student)\n",
"averages = [82.33, 63.33, 91.67]\n",
"\n",
"# 2. Dict mapping name -> average (rounded to 2 dp)\n",
"avg_map = {'Alice': 88.33, 'Bob': 63.33, 'Carol': 91.67}\n",
"\n",
"# 3. Set of unique student names who scored >= 80 average\n",
"top = {'Alice', 'Carol'}\n",
"N_POINTS: int, pi_estimate: floatin_unit_circle() with type hintssum(1 for _ in range(n) if ...)seed() for reproducibility, uniform() for samplingmath.pi as ground truth