{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Python Programming Fundamentals: Conditionals, Loops, and Functions\n",
        "\n",
        "This is part 3 of the Python Fundamentals series. Complete the preceding tutorial before starting.\n",
        "\n",
        "Run each cell in order."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Conditional statements\n",
        "\n",
        "An if statement executes its indented block when a condition is true. Elif checks additional conditions in order, and else handles remaining cases. Use four spaces for indentation."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "score = 0.87\n",
        "if score >= 0.9:\n",
        "    print(\"High\")\n",
        "elif score >= 0.7:\n",
        "    print(\"Moderate\")\n",
        "else:\n",
        "    print(\"Low\")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## For loops\n",
        "\n",
        "A for loop iterates over an iterable. Range produces integers up to but excluding its stop value. Enumerate supplies an index alongside each value."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "for index, score in enumerate([0.8, 0.9, 0.85], start=1):\n",
        "    print(index, score)\n",
        "for i in range(3):\n",
        "    print(i)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## While loops\n",
        "\n",
        "A while loop repeats while its condition remains true. Update the condition inside the loop to avoid unintended infinite execution. Break exits a loop; continue skips the remaining statements in the current iteration."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "count = 0\n",
        "while count < 3:\n",
        "    print(count)\n",
        "    count += 1"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Functions\n",
        "\n",
        "Functions group reusable instructions. Parameters receive inputs and return sends a result back to the caller. A function without an explicit return produces None."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "def mean(values):\n",
        "    if not values:\n",
        "        raise ValueError(\"Expected at least one value\")\n",
        "    return sum(values) / len(values)\n",
        "\n",
        "print(mean([10, 20, 30]))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Arguments and scope\n",
        "\n",
        "Default parameters allow callers to omit optional arguments. Keyword arguments identify parameters by name. Variables assigned inside a function are generally local to that function."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "def meets_threshold(score, threshold=0.9):\n",
        "    return score >= threshold\n",
        "\n",
        "print(meets_threshold(0.85))\n",
        "print(meets_threshold(0.85, threshold=0.8))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Handling exceptions\n",
        "\n",
        "Exceptions signal errors during execution. Use try and except to handle anticipated errors. Catch specific exception types rather than hiding every failure."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "try:\n",
        "    number = float(\"invalid\")\n",
        "except ValueError:\n",
        "    print(\"Please provide a numeric value.\")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Importing modules\n",
        "\n",
        "Modules provide reusable functionality. Python includes a standard library, so many useful tools require no installation. Import only what you need or use the module name to make the origin clear."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "import math\n",
        "from statistics import mean\n",
        "\n",
        "print(math.sqrt(25))\n",
        "print(mean([1, 2, 3]))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Practical example: prediction accuracy\n",
        "\n",
        "Combine functions, loops, comparisons and validation to calculate classification accuracy from known and predicted labels. This evaluates existing predictions rather than training a model."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "def accuracy_score(actual, predicted):\n",
        "    if len(actual) != len(predicted) or not actual:\n",
        "        raise ValueError(\"Inputs must have equal nonzero lengths\")\n",
        "    correct = sum(a == p for a, p in zip(actual, predicted))\n",
        "    return correct / len(actual)\n",
        "\n",
        "print(accuracy_score([0, 1, 1, 0], [0, 1, 0, 0]))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Practice exercises\n",
        "\n",
        "Try these before viewing the solutions."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Write a function returning the square of a number."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use a loop to count values greater than 0.8 in [0.7, 0.9, 0.85]."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Exercise solutions"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "def square(value):\n",
        "    return value ** 2\n",
        "\n",
        "print(square(5))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "count = 0\n",
        "for value in [0.7, 0.9, 0.85]:\n",
        "    if value > 0.8:\n",
        "        count += 1\n",
        "print(count)"
      ],
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}