{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# If statements\n",
    "\n",
    "In this tutorial, you will learn how to use `if`, `elif`, and `else` to make decisions in your Python code.\n",
    "\n",
    "**Time commitment:** 15–20 minutes\n",
    "\n",
    "**Prerequisites:**\n",
    "\n",
    "- Python 3.12 or later installed on your machine\n",
    "- Basic familiarity with Python syntax (variables, strings, numbers)\n",
    "\n",
    "## Learning objectives\n",
    "\n",
    "By the end of this tutorial, you will be able to:\n",
    "\n",
    "- Write `if` statements to execute code conditionally\n",
    "- Use `elif` and `else` to handle multiple conditions\n",
    "- Use comparison operators to compare values\n",
    "- Combine conditions with `and`, `or`, and `not`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What is conditional logic?\n",
    "\n",
    "**Conditional logic** allows your program to make decisions. Instead of running every line of code in order, you can tell Python to run certain code only when a condition is met.\n",
    "\n",
    "Think of it like everyday decisions: \"If it is raining, take an umbrella. Otherwise, wear sunglasses.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Your first if statement\n",
    "\n",
    "An `if` statement checks whether a condition is true. If it is, the indented code beneath it runs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature = 35\n",
    "\n",
    "if temperature > 30:\n",
    "    print(\"It is hot today!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The condition `temperature > 30` evaluates to `True`, so the `print` statement runs. Try changing the temperature to 20 and running the cell again — you will see that nothing is printed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding an else clause\n",
    "\n",
    "Use `else` to specify what should happen when the condition is `False`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature = 15\n",
    "\n",
    "if temperature > 30:\n",
    "    print(\"It is hot today!\")\n",
    "else:\n",
    "    print(\"It is not too hot today.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the program always prints something — one message if the temperature is above 30, and a different message otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple conditions with elif\n",
    "\n",
    "When you have more than two possible outcomes, use `elif` (short for \"else if\") to check additional conditions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature = 15\n",
    "\n",
    "if temperature > 30:\n",
    "    print(\"It is hot!\")\n",
    "elif temperature > 20:\n",
    "    print(\"It is warm.\")\n",
    "elif temperature > 10:\n",
    "    print(\"It is mild.\")\n",
    "else:\n",
    "    print(\"It is cold!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python checks each condition from top to bottom. As soon as one condition is `True`, it runs that block and skips the rest. If no conditions are `True`, the `else` block runs.\n",
    "\n",
    "Try changing the temperature and running the cell again to see different results."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparison operators\n",
    "\n",
    "Conditions use **comparison operators** to compare values. Here are the main ones:\n",
    "\n",
    "| Operator | Meaning | Example |\n",
    "|----------|---------|--------|\n",
    "| `==` | Equal to | `x == 5` |\n",
    "| `!=` | Not equal to | `x != 5` |\n",
    "| `>` | Greater than | `x > 5` |\n",
    "| `<` | Less than | `x < 5` |\n",
    "| `>=` | Greater than or equal to | `x >= 5` |\n",
    "| `<=` | Less than or equal to | `x <= 5` |\n",
    "\n",
    "Let's see some in action:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = 75\n",
    "\n",
    "if score >= 70:\n",
    "    print(\"You passed with distinction!\")\n",
    "elif score >= 50:\n",
    "    print(\"You passed.\")\n",
    "else:\n",
    "    print(\"You did not pass this time.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Combining conditions\n",
    "\n",
    "You can combine multiple conditions using **boolean operators**:\n",
    "\n",
    "- `and` — both conditions must be true\n",
    "- `or` — at least one condition must be true\n",
    "- `not` — reverses a condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 20\n",
    "has_ticket = True\n",
    "\n",
    "if age >= 18 and has_ticket:\n",
    "    print(\"Welcome to the event!\")\n",
    "elif age >= 18 and not has_ticket:\n",
    "    print(\"You need a ticket to enter.\")\n",
    "else:\n",
    "    print(\"Sorry, you must be at least 18.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "day = \"Saturday\"\n",
    "\n",
    "if day == \"Saturday\" or day == \"Sunday\":\n",
    "    print(\"It is the weekend!\")\n",
    "else:\n",
    "    print(\"It is a weekday.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Checking equality with strings\n",
    "\n",
    "You can compare strings just like numbers. Remember that string comparisons are case-sensitive:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colour = \"red\"\n",
    "\n",
    "if colour == \"red\":\n",
    "    print(\"Stop!\")\n",
    "elif colour == \"amber\":\n",
    "    print(\"Get ready...\")\n",
    "elif colour == \"green\":\n",
    "    print(\"Go!\")\n",
    "else:\n",
    "    print(f\"Unknown colour: {colour}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Nested if statements\n",
    "\n",
    "You can place `if` statements inside other `if` statements. This is called **nesting**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "is_member = True\n",
    "age = 15\n",
    "\n",
    "if is_member:\n",
    "    if age >= 18:\n",
    "        print(\"Full access granted.\")\n",
    "    else:\n",
    "        print(\"Junior access granted.\")\n",
    "else:\n",
    "    print(\"Please sign up to become a member.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While nesting works, deeply nested conditions can become hard to read. In later guides, you will learn techniques for keeping your conditions simple and readable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise: classify a number\n",
    "\n",
    "Now it is your turn! Write code that takes a number and prints whether it is:\n",
    "\n",
    "- Positive and even\n",
    "- Positive and odd\n",
    "- Zero\n",
    "- Negative\n",
    "\n",
    "**Hint:** Use the modulo operator `%` to check if a number is even. A number is even if `number % 2 == 0`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 7  # Try different values\n",
    "\n",
    "# Write your code here\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "Here is one way to complete the exercise:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number = 7\n",
    "\n",
    "if number > 0:\n",
    "    if number % 2 == 0:\n",
    "        print(f\"{number} is positive and even\")\n",
    "    else:\n",
    "        print(f\"{number} is positive and odd\")\n",
    "elif number == 0:\n",
    "    print(\"The number is zero\")\n",
    "else:\n",
    "    print(f\"{number} is negative\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "In this tutorial, you learned how to:\n",
    "\n",
    "- Write `if` statements to run code only when a condition is true\n",
    "- Use `else` for a fallback when the condition is false\n",
    "- Use `elif` to check multiple conditions in sequence\n",
    "- Compare values with `==`, `!=`, `>`, `<`, `>=`, and `<=`\n",
    "- Combine conditions with `and`, `or`, and `not`\n",
    "- Nest `if` statements for more complex decisions\n",
    "\n",
    "## What is next\n",
    "\n",
    "In upcoming tutorials, you will explore:\n",
    "\n",
    "- **Boolean expressions** — deeper dive into `True`, `False`, and truthiness\n",
    "- **Truthiness and falsiness** — how Python decides what counts as true\n",
    "- **Match statements** — a powerful alternative to long `if`/`elif` chains (Python 3.10 and later)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
