{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 1: Python basics and environment setup\n",
    "**Asiman Data School** · Module 1: Foundations of Python Programming · Week 1, Tuesday\n",
    "\n",
    "This notebook follows the lesson page step by step. Read each explanation, then run the code cell under it.\n",
    "\n",
    "**How to use it**\n",
    "- Run a cell with **Shift + Enter**. Run the cells from top to bottom, in order.\n",
    "- Change the values in the examples and run them again. Breaking things is part of learning.\n",
    "- Cells that say `# Write your code here` are for you. Solutions are hidden: click **Show the solution** only after you try.\n",
    "- If something behaves strangely, use **Kernel → Restart Kernel and Run All Cells**."
   ],
   "id": "t01-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## By the end of this lesson you will be able to\n",
    "1. Create variables, print them, and follow Python's naming rules.\n",
    "2. Recognize the data types int, float, str, bool, None, list, tuple, dict and set, and use the main operations of each.\n",
    "3. Convert values from one type to another, and know when you must.\n",
    "4. Use arithmetic, assignment, comparison, logical, membership and identity operators.\n",
    "5. Install Anaconda, open Jupyter, and run code in notebook cells in the right order.\n",
    "6. Recognize and fix the most common beginner errors.\n",
    "\n",
    "## Contents\n",
    "0. Check your setup\n",
    "1. Variables\n",
    "2. Data types (numbers, strings, booleans, None, lists, tuples, dictionaries, sets, conversion)\n",
    "3. Operators\n",
    "4. Jupyter tips\n",
    "5. Common mistakes\n",
    "6. Practice exercises (warm-up, core, challenge)\n",
    "7. Check yourself\n",
    "8. Summary"
   ],
   "id": "t01-001"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Check your setup\n",
    "Run the cell below with **Shift + Enter**. If the text appears under it, everything works."
   ],
   "id": "t01-002"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello, Data Science!\")"
   ],
   "id": "t01-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Which Python version do you have? (Your number may be different from your classmates'. Any Python 3 version works for this course.)"
   ],
   "id": "t01-004"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "print(sys.version)"
   ],
   "id": "t01-005"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Variables\n",
    "### 1.1 What is a variable?\n",
    "A variable is a **labeled box** where you store a piece of information so you can use it later. You choose the label (the name) and put something inside.\n",
    "\n",
    "The `=` sign does **not** mean \"equal\" like in maths. It means: *put the value on the right into the box on the left.*"
   ],
   "id": "t01-006"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Aylin\"\n",
    "age = 20\n",
    "height = 1.75\n",
    "is_student = True\n",
    "\n",
    "print(name)\n",
    "print(age)\n",
    "print(height)\n",
    "print(is_student)"
   ],
   "id": "t01-007"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 Showing values with print() and writing comments\n",
    "- `print(a, b)` shows several values, separated by a space.\n",
    "- `sep=\"-\"` changes the separator; `end=\" \"` changes what comes at the end of the line.\n",
    "- Anything after `#` is a **comment**: Python ignores it.\n",
    "\n",
    "Tip: in Jupyter, if the last line of a cell is just a variable name, Jupyter shows its value even without `print()`."
   ],
   "id": "t01-008"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Aylin\"\n",
    "age = 20\n",
    "\n",
    "print(\"Name:\", name)            # several values, separated by a space\n",
    "print(\"Age:\", age)\n",
    "print(\"Data\", \"Science\", sep=\"-\")  # choose the separator\n",
    "print(\"Same\", end=\" \")           # do not jump to a new line\n",
    "print(\"line\")"
   ],
   "id": "t01-009"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This whole line is a comment. Python skips it.\n",
    "price = 100   # A comment can also follow code on the same line.\n",
    "# print(price * 2)   <- this line is \"commented out\", so it does not run\n",
    "print(price)"
   ],
   "id": "t01-010"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 Changing a variable\n",
    "A box holds one value at a time. A new assignment replaces the old value. In `age = age + 1`, Python first works out the right side using the old value, then stores the result."
   ],
   "id": "t01-011"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 20\n",
    "age = 21        # the box now holds 21\n",
    "age = age + 1   # take the old value, add 1, store it back\n",
    "print(age)"
   ],
   "id": "t01-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 Several variables in one line\n",
    "You can fill several boxes at once, give several boxes the same value, or swap two values."
   ],
   "id": "t01-013"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c = 1, 2, 3     # three boxes in one line\n",
    "print(a, b, c)\n",
    "\n",
    "x = y = 0             # the same value into two boxes\n",
    "print(x, y)"
   ],
   "id": "t01-014"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first = \"tea\"\n",
    "second = \"coffee\"\n",
    "first, second = second, first   # swap the two values\n",
    "print(first, second)"
   ],
   "id": "t01-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5 Rules for naming variables\n",
    "| Rule | Works | Does not work |\n",
    "|---|---|---|\n",
    "| Letters, numbers and underscores only | `score_2` | `score-2` |\n",
    "| Cannot start with a number | `score1` | `1score` |\n",
    "| No spaces, use `_` | `student_name` | `student name` |\n",
    "| Case-sensitive | `age`, `Age`, `AGE` are three different names | |\n",
    "| No reserved words | `class_name` | `if`, `for`, `class`, `True` |\n",
    "| Meaningful, lowercase_with_underscores | `price` | `p` (works, but unclear) |"
   ],
   "id": "t01-016"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 20\n",
    "Age = 30\n",
    "AGE = 40\n",
    "print(age, Age, AGE)   # three different variables"
   ],
   "id": "t01-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.6 Python figures out the type for you\n",
    "You never announce the type. Python looks at the value and decides by itself (this is called **dynamic typing**). The same name can even hold a different type later."
   ],
   "id": "t01-018"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 5\n",
    "print(type(x))\n",
    "x = \"five\"          # the same name now holds text\n",
    "print(type(x))"
   ],
   "id": "t01-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try it: variables\n",
    "\n",
    "Create a variable `city` with any city name and `temperature` with a decimal number. Print both on one line. Then increase the temperature by 2 and print it again."
   ],
   "id": "t01-020"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-021"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Use `print(city, temperature)`. To increase: `temperature = temperature + 2`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "city = \"Paris\"\n",
    "temperature = 18.5\n",
    "print(city, temperature)\n",
    "temperature = temperature + 2\n",
    "print(temperature)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Paris 18.5\n",
    "20.5\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Key points: variables**\n",
    "> - A variable is a named box: `name = value`. Read `=` as \"store\".\n",
    "> - `print(a, b)` shows values; `sep` and `end` control spacing and line endings. `#` starts a comment.\n",
    "> - A new assignment replaces the old value. `x = x + 1` uses the old value first.\n",
    "> - Names: letters, digits, `_`; not starting with a digit; no spaces; case-sensitive; no reserved words."
   ],
   "id": "t01-023"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Data types\n",
    "### 2.1 What is a data type?\n",
    "A data type tells Python what **kind** of information a value is. The type decides what you can do with it: you can multiply two numbers, but multiplying two names makes no sense. Ask for the type with `type()`, or check it with `isinstance()`."
   ],
   "id": "t01-024"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(20))\n",
    "print(type(3.5))\n",
    "print(type(\"hello\"))\n",
    "print(type(True))\n",
    "print(type(None))"
   ],
   "id": "t01-025"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 20\n",
    "print(isinstance(age, int))    # is age an int?\n",
    "print(isinstance(age, str))    # is age a str?"
   ],
   "id": "t01-026"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 The basic types\n",
    "| Type | Name | What it holds | Example |\n",
    "|---|---|---|---|\n",
    "| `int` | Integer | Whole numbers | `7`, `-3`, `2026` |\n",
    "| `float` | Floating-point number | Numbers with a decimal point | `3.14`, `-0.5`, `2.0` |\n",
    "| `str` | String | Text, always inside quotes | `\"Hello\"`, `'Python'` |\n",
    "| `bool` | Boolean | Only True or False | `True`, `False` |\n",
    "| `NoneType` | None | \"Nothing here yet\" | `None` |\n",
    "\n",
    "### 2.3 Numbers: int and float\n",
    "Mixing an int and a float gives a float, and `/` always gives a float."
   ],
   "id": "t01-027"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(3 + 2)         # int + int gives int\n",
    "print(3 + 2.0)       # int + float gives float\n",
    "print(10 / 5)        # / always gives float\n",
    "print(type(3 + 2.0))"
   ],
   "id": "t01-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Useful functions for numbers:** `abs`, `round`, `min`, `max`, `pow`, `divmod`."
   ],
   "id": "t01-029"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(abs(-7))             # distance from zero\n",
    "print(round(3.14159, 2))   # round to 2 decimals\n",
    "print(round(7.6))          # round to a whole number\n",
    "print(min(4, 9, 1))        # smallest\n",
    "print(max(4, 9, 1))        # largest\n",
    "print(pow(2, 5))           # same as 2 ** 5\n",
    "print(divmod(17, 5))       # (17 // 5, 17 % 5)"
   ],
   "id": "t01-030"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Watch out:** `round()` sends an exact half to the nearest **even** number."
   ],
   "id": "t01-031"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(round(2.5))   # rounds to the nearest EVEN number\n",
    "print(round(3.5))\n",
    "print(round(4.5))"
   ],
   "id": "t01-032"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Writing big and small numbers:** underscores for readability, and `e` for scientific notation. Python integers have no size limit."
   ],
   "id": "t01-033"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "population = 8_100_000_000   # underscores make long numbers readable\n",
    "print(population)\n",
    "\n",
    "distance = 1.5e3             # scientific notation: 1.5 x 10 to the power 3\n",
    "print(distance)\n",
    "tiny = 2e-4                  # 2 x 10 to the power -4\n",
    "print(tiny)"
   ],
   "id": "t01-034"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(2 ** 100)   # Python integers have no size limit"
   ],
   "id": "t01-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**A famous surprise with decimals.** Computers store decimals in binary, so some decimals can't be stored perfectly. It's normal, not a bug. Round when you display results, and don't compare decimals with `==`."
   ],
   "id": "t01-036"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(0.1 + 0.2)\n",
    "print(0.1 + 0.2 == 0.3)\n",
    "print(round(0.1 + 0.2, 2))"
   ],
   "id": "t01-037"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4 Strings: working with text\n",
    "**Creating strings.** Single or double quotes both work. Use the other kind when your text contains one, and three quotes for several lines."
   ],
   "id": "t01-038"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "single = 'Hello'\n",
    "double = \"Hello\"\n",
    "with_apostrophe = \"It's Python\"\n",
    "many_lines = \"\"\"Line one\n",
    "Line two\"\"\"\n",
    "\n",
    "print(single, double)\n",
    "print(with_apostrophe)\n",
    "print(many_lines)"
   ],
   "id": "t01-039"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Special characters (escape sequences):** `\\n` new line, `\\t` tab, `\\\"` a quote inside quotes, `\\\\` one backslash."
   ],
   "id": "t01-040"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Name:\\tAylin\")             # \\t is a tab\n",
    "print(\"First line\\nSecond line\")  # \\n starts a new line\n",
    "print(\"She said \\\"hi\\\"\")          # \\\" puts a quote inside quotes\n",
    "print(\"C:\\\\Users\\\\aylin\")         # \\\\ is one backslash"
   ],
   "id": "t01-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Positions and slices.** Each character has a position starting at **0**; negative positions count from the end. A slice `text[start:stop:step]` takes a part of the text, and the stop position is **not** included.\n",
    "\n",
    "```\n",
    " P   y   t   h   o   n\n",
    " 0   1   2   3   4   5\n",
    "-6  -5  -4  -3  -2  -1\n",
    "```"
   ],
   "id": "t01-042"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"Python\"\n",
    "print(word[0])     # first character\n",
    "print(word[5])     # sixth character\n",
    "print(word[-1])    # last character\n",
    "print(word[-2])    # second from the end\n",
    "print(len(word))   # number of characters"
   ],
   "id": "t01-043"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"Python\"\n",
    "print(word[0:3])   # from 0 up to (not including) 3\n",
    "print(word[2:])    # from 2 to the end\n",
    "print(word[:4])    # from the start up to 4\n",
    "print(word[-3:])   # the last three\n",
    "print(word[::2])   # every second character\n",
    "print(word[::-1])  # reversed"
   ],
   "id": "t01-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Joining and repeating** with `+` and `*`:"
   ],
   "id": "t01-045"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first = \"Data\"\n",
    "second = \"Science\"\n",
    "print(first + \" \" + second)   # join with +\n",
    "print(\"ha\" * 3)               # repeat with *\n",
    "print(\"-\" * 20)               # a quick divider line"
   ],
   "id": "t01-046"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Changing case:**"
   ],
   "id": "t01-047"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"python for Data Science\"\n",
    "print(text.upper())\n",
    "print(text.lower())\n",
    "print(text.title())\n",
    "print(text.capitalize())\n",
    "print(text.swapcase())"
   ],
   "id": "t01-048"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Removing extra spaces** (very common when cleaning data):"
   ],
   "id": "t01-049"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw = \"   Aylin   \"\n",
    "print(\"[\" + raw + \"]\")\n",
    "print(\"[\" + raw.strip() + \"]\")    # both sides\n",
    "print(\"[\" + raw.lstrip() + \"]\")   # left side only\n",
    "print(\"[\" + raw.rstrip() + \"]\")   # right side only"
   ],
   "id": "t01-050"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Replacing text** (case-sensitive; an optional number limits how many replacements):"
   ],
   "id": "t01-051"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentence = \"I like cats. Cats are great.\"\n",
    "print(sentence.replace(\"cats\", \"dogs\"))   # case-sensitive: \"Cats\" stays\n",
    "print(sentence.replace(\"a\", \"@\", 2))      # only the first 2 matches"
   ],
   "id": "t01-052"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Splitting** text into a list, and **joining** a list back into text:"
   ],
   "id": "t01-053"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_line = \"apple,banana,cherry\"\n",
    "fruits = csv_line.split(\",\")       # text -> list\n",
    "print(fruits)\n",
    "print(\" | \".join(fruits))          # list -> text\n",
    "\n",
    "words = \"Data science is fun\".split()   # split on spaces\n",
    "print(words)\n",
    "print(len(words))"
   ],
   "id": "t01-054"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Searching inside text:**"
   ],
   "id": "t01-055"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"banana\"\n",
    "print(text.find(\"an\"))         # position of the first match\n",
    "print(text.find(\"x\"))          # -1 means \"not found\"\n",
    "print(text.index(\"n\"))         # like find, but an error if missing\n",
    "print(text.count(\"a\"))         # how many times\n",
    "print(text.startswith(\"ban\"))\n",
    "print(text.endswith(\"na\"))\n",
    "print(\"nan\" in text)"
   ],
   "id": "t01-056"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Checking what a string contains:**"
   ],
   "id": "t01-057"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"2026\".isdigit())     # only digits?\n",
    "print(\"Aylin\".isalpha())    # only letters?\n",
    "print(\"abc123\".isalnum())   # letters or digits?\n",
    "print(\"   \".isspace())      # only spaces?\n",
    "print(\"HELLO\".isupper())\n",
    "print(\"hello\".islower())"
   ],
   "id": "t01-058"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**f-strings.** Put `f` before the quotes; anything in `{ }` is replaced by its value. After a colon you can format numbers: `{x:.2f}` two decimals, `{n:,}` thousands separator."
   ],
   "id": "t01-059"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Aylin\"\n",
    "score = 87.456\n",
    "students = 1250\n",
    "\n",
    "print(f\"{name} scored {score} points\")\n",
    "print(f\"Rounded: {score:.1f}\")            # 1 decimal place\n",
    "print(f\"Two decimals: {score:.2f}\")\n",
    "print(f\"Total students: {students:,}\")    # thousands separator\n",
    "print(f\"Next year: {20 + 1}\")             # any expression works"
   ],
   "id": "t01-060"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Strings cannot be changed in place** (they are *immutable*). This fails:\n",
    "\n",
    "```python\n",
    "word = \"Python\"\n",
    "word[0] = \"J\"\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "TypeError: 'str' object does not support item assignment\n",
    "```"
   ],
   "id": "t01-061"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead, build a new string:"
   ],
   "id": "t01-062"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"Python\"\n",
    "new_word = \"J\" + word[1:]   # build a new string instead\n",
    "print(new_word)\n",
    "print(word)                 # the original is unchanged"
   ],
   "id": "t01-063"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try it: strings\n",
    "\n",
    "Start with `course = \"data science\"`. Print it in Title Case, print only the first 4 characters, print its length, and print it with the space replaced by an underscore."
   ],
   "id": "t01-064"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-065"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Use `.title()`, a slice `[:4]`, `len()` and `.replace(\" \", \"_\")`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "course = \"data science\"\n",
    "print(course.title())\n",
    "print(course[:4])\n",
    "print(len(course))\n",
    "print(course.replace(\" \", \"_\"))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Data Science\n",
    "data\n",
    "12\n",
    "data_science\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-066"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.5 Booleans: True and False\n",
    "Booleans are the answers to yes/no questions, and every comparison gives one. They are the foundation of decisions in code, which you will use in the next topic."
   ],
   "id": "t01-067"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "is_raining = True\n",
    "print(is_raining)\n",
    "print(type(is_raining))\n",
    "print(5 > 3)       # comparisons give booleans\n",
    "print(10 == 9)"
   ],
   "id": "t01-068"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Behind the scenes, `True` counts as 1 and `False` as 0:"
   ],
   "id": "t01-069"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(True + True)   # True counts as 1\n",
    "print(int(False))    # False counts as 0"
   ],
   "id": "t01-070"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Truthy and falsy.** `bool()` turns any value into True or False. Zero and empty values count as **False**: `0`, `0.0`, `\"\"`, `[]`, `()`, `{}`, `set()`, `None`. Everything else counts as **True**."
   ],
   "id": "t01-071"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(bool(0), bool(42), bool(-1))\n",
    "print(bool(\"\"), bool(\"hi\"), bool(\" \"))\n",
    "print(bool([]), bool([0]))\n",
    "print(bool(None))"
   ],
   "id": "t01-072"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6 None: nothing here yet\n",
    "`None` is used when a variable exists but doesn't have a value yet. Check for it with `is None`. Some functions give back `None`, like `print()`."
   ],
   "id": "t01-073"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = None\n",
    "print(result)\n",
    "print(type(result))\n",
    "print(result is None)"
   ],
   "id": "t01-074"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer = print(\"Hello\")   # print shows text but gives back nothing\n",
    "print(answer)"
   ],
   "id": "t01-075"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.7 Collection types: many values in one box\n",
    "| | list | tuple | dict | set |\n",
    "|---|---|---|---|---|\n",
    "| Brackets | `[ ]` | `( )` | `{key: value}` | `{ }` |\n",
    "| Keeps order | Yes | Yes | Yes (order added) | No |\n",
    "| Can change | Yes | No | Yes | Yes |\n",
    "| Duplicates | Allowed | Allowed | Keys must be unique | Removed |\n",
    "| Get an item by | position | position | key | cannot (only check with `in`) |\n",
    "\n",
    "A first look at lists, dictionaries and sets:"
   ],
   "id": "t01-076"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(fruits[0])          # apple  (counting starts at 0!)\n",
    "fruits.append(\"mango\")    # adds an item to the end\n",
    "print(len(fruits))        # 4\n",
    "\n",
    "student = {\"name\": \"Aylin\", \"age\": 20}\n",
    "print(student[\"name\"])    # Aylin\n",
    "\n",
    "print({1, 2, 2, 3})       # {1, 2, 3}  (duplicates removed)"
   ],
   "id": "t01-077"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Remember** that Python counts from 0, so the first item is number 0, not 1. Lists and dictionaries are everywhere in Data Science, so you will use them constantly. Topics 4 and 5 practice these in more depth.\n",
    "\n",
    "### 2.8 Lists\n",
    "**Create** a list:"
   ],
   "id": "t01-078"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "numbers = [4, 8, 15, 16, 23, 42]\n",
    "mixed = [\"Aylin\", 20, 1.75, True]   # different types are allowed\n",
    "empty = []\n",
    "\n",
    "print(fruits)\n",
    "print(len(numbers))\n",
    "print(mixed)\n",
    "print(empty)"
   ],
   "id": "t01-079"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Read** items by position (starting at 0) and **slice** them, exactly like strings:"
   ],
   "id": "t01-080"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\", \"mango\"]\n",
    "print(fruits[0])     # first\n",
    "print(fruits[-1])    # last\n",
    "print(fruits[1:3])   # slice: positions 1 and 2\n",
    "print(fruits[:2])    # the first two"
   ],
   "id": "t01-081"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Change** an item (lists can change, strings cannot):"
   ],
   "id": "t01-082"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "fruits[1] = \"blueberry\"   # lists can change\n",
    "print(fruits)"
   ],
   "id": "t01-083"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Add** items: `append` (end), `insert` (a position), `extend` (several):"
   ],
   "id": "t01-084"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\"]\n",
    "fruits.append(\"cherry\")          # add to the end\n",
    "fruits.insert(0, \"mango\")        # add at position 0\n",
    "fruits.extend([\"kiwi\", \"lime\"])  # add several\n",
    "print(fruits)"
   ],
   "id": "t01-085"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Remove** items: `remove` (by value, first match), `pop` (by position, gives the item back), `del` (by position):"
   ],
   "id": "t01-086"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\", \"banana\", \"kiwi\"]\n",
    "fruits.remove(\"banana\")    # removes the FIRST match\n",
    "print(fruits)\n",
    "\n",
    "last = fruits.pop()        # removes and gives back the last item\n",
    "print(last, fruits)\n",
    "\n",
    "second = fruits.pop(1)     # removes and gives back position 1\n",
    "print(second, fruits)\n",
    "\n",
    "del fruits[0]              # deletes position 0\n",
    "print(fruits)"
   ],
   "id": "t01-087"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Search**: `in`, `index`, `count`:"
   ],
   "id": "t01-088"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [3, 7, 3, 9, 3]\n",
    "print(7 in nums)        # is it in the list?\n",
    "print(nums.index(9))    # where is it?\n",
    "print(nums.count(3))    # how many times?"
   ],
   "id": "t01-089"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Sort**: `sorted()` makes a new list, `.sort()` changes the list itself:"
   ],
   "id": "t01-090"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [72, 95, 58, 88]\n",
    "print(sorted(scores))     # a NEW sorted list\n",
    "print(scores)             # the original is unchanged\n",
    "\n",
    "scores.sort()             # sorts the list itself\n",
    "print(scores)\n",
    "scores.sort(reverse=True) # largest first\n",
    "print(scores)\n",
    "scores.reverse()          # flip the current order\n",
    "print(scores)"
   ],
   "id": "t01-091"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Count and calculate**: `len`, `min`, `max`, `sum`:"
   ],
   "id": "t01-092"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [72, 95, 58, 88]\n",
    "print(len(scores))\n",
    "print(min(scores), max(scores))\n",
    "print(sum(scores))\n",
    "print(sum(scores) / len(scores))   # the average"
   ],
   "id": "t01-093"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Join and repeat** lists:"
   ],
   "id": "t01-094"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print([1, 2] + [3, 4])   # join two lists\n",
    "print([0] * 5)           # repeat a list"
   ],
   "id": "t01-095"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Lists inside lists** (a table of rows):"
   ],
   "id": "t01-096"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = [[1, 2, 3],\n",
    "        [4, 5, 6]]\n",
    "print(grid[1])      # the second row\n",
    "print(grid[1][2])   # row 1, position 2"
   ],
   "id": "t01-097"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**A common trap:** `b = a` does not copy a list. Both names point to the same list. Use `.copy()`:"
   ],
   "id": "t01-098"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2, 3]\n",
    "b = a            # b is the SAME list, not a copy\n",
    "b.append(4)\n",
    "print(a)         # a changed too!\n",
    "\n",
    "c = a.copy()     # a real copy\n",
    "c.append(5)\n",
    "print(a)\n",
    "print(c)"
   ],
   "id": "t01-099"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Number lists with `range()`** (the stop number is not included):"
   ],
   "id": "t01-100"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(list(range(5)))          # 0 up to 5 (not including 5)\n",
    "print(list(range(2, 10, 2)))   # start 2, stop 10, step 2"
   ],
   "id": "t01-101"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try it: lists\n",
    "\n",
    "Start with `colors = [\"red\", \"green\"]`. Add `\"blue\"` at the end, put `\"black\"` at the start, remove `\"green\"`, then print the list and its length."
   ],
   "id": "t01-102"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-103"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Use `append`, `insert(0, ...)`, `remove` and `len`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "colors = [\"red\", \"green\"]\n",
    "colors.append(\"blue\")\n",
    "colors.insert(0, \"black\")\n",
    "colors.remove(\"green\")\n",
    "print(colors)\n",
    "print(len(colors))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "['black', 'red', 'blue']\n",
    "3\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-104"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.9 Tuples\n",
    "A tuple is like a list that **cannot be changed**. Use it for values that belong together and should stay fixed (a point, a date). A tuple with one item needs a comma."
   ],
   "id": "t01-105"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point = (3, 4)\n",
    "colors = (\"red\", \"green\", \"blue\")\n",
    "single = (5,)        # one item needs a comma\n",
    "not_a_tuple = (5)    # just the number 5 in brackets\n",
    "\n",
    "print(point, colors)\n",
    "print(type(single))\n",
    "print(type(not_a_tuple))"
   ],
   "id": "t01-106"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = (\"red\", \"green\", \"blue\", \"green\")\n",
    "print(colors[0])\n",
    "print(colors[-1])\n",
    "print(colors[1:3])\n",
    "print(len(colors))\n",
    "print(colors.count(\"green\"))\n",
    "print(colors.index(\"blue\"))"
   ],
   "id": "t01-107"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Unpacking** gives each value its own name:"
   ],
   "id": "t01-108"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point = (3, 4)\n",
    "x, y = point        # unpacking: one value per name\n",
    "print(x)\n",
    "print(y)"
   ],
   "id": "t01-109"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tuples cannot be changed. This fails:\n",
    "\n",
    "```python\n",
    "colors = (\"red\", \"green\", \"blue\")\n",
    "colors[0] = \"pink\"\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "TypeError: 'tuple' object does not support item assignment\n",
    "```"
   ],
   "id": "t01-110"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print((1, 2) + (3,))\n",
    "print((\"ha\",) * 3)"
   ],
   "id": "t01-111"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.10 Dictionaries\n",
    "A dictionary stores **key → value** pairs. You look values up by key, not by position."
   ],
   "id": "t01-112"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\"name\": \"Aylin\", \"age\": 20, \"course\": \"Data Science\"}\n",
    "print(student)\n",
    "print(len(student))   # number of key-value pairs"
   ],
   "id": "t01-113"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Read** values with `[ ]` or, more safely, with `get()`:"
   ],
   "id": "t01-114"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\"name\": \"Aylin\", \"age\": 20}\n",
    "print(student[\"name\"])                 # by key\n",
    "print(student.get(\"age\"))              # same, but safe\n",
    "print(student.get(\"email\"))            # missing key -> None\n",
    "print(student.get(\"email\", \"not given\"))   # missing key -> your default"
   ],
   "id": "t01-115"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With square brackets, a missing key is an error:\n",
    "\n",
    "```python\n",
    "student = {\"name\": \"Aylin\", \"age\": 20}\n",
    "print(student[\"email\"])\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "KeyError: 'email'\n",
    "```"
   ],
   "id": "t01-116"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Add and change** with `d[key] = value`, or several at once with `update()`:"
   ],
   "id": "t01-117"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\"name\": \"Aylin\", \"age\": 20}\n",
    "student[\"age\"] = 21                      # change a value\n",
    "student[\"email\"] = \"aylin@example.com\"   # add a new pair\n",
    "print(student)\n",
    "\n",
    "student.update({\"age\": 22, \"course\": \"AI\"})   # change or add several\n",
    "print(student)"
   ],
   "id": "t01-118"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Remove** with `pop()` (gives the value back) or `del`:"
   ],
   "id": "t01-119"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\"name\": \"Aylin\", \"age\": 20, \"email\": \"aylin@example.com\"}\n",
    "removed = student.pop(\"email\")   # remove and give back the value\n",
    "print(removed)\n",
    "del student[\"age\"]               # delete a pair\n",
    "print(student)"
   ],
   "id": "t01-120"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**All keys, values and pairs:**"
   ],
   "id": "t01-121"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\"name\": \"Aylin\", \"age\": 20}\n",
    "print(student.keys())\n",
    "print(student.values())\n",
    "print(student.items())\n",
    "print(list(student.keys()))   # as a normal list"
   ],
   "id": "t01-122"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`in` checks the **keys** of a dictionary:"
   ],
   "id": "t01-123"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\"name\": \"Aylin\", \"age\": 20}\n",
    "print(\"name\" in student)    # checks the KEYS\n",
    "print(\"Aylin\" in student)   # values are not checked\n",
    "print(\"Aylin\" in student.values())"
   ],
   "id": "t01-124"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Dictionaries can hold lists (and other dictionaries):"
   ],
   "id": "t01-125"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "course = {\n",
    "    \"title\": \"Python basics\",\n",
    "    \"topics\": [\"variables\", \"data types\", \"operators\"],\n",
    "}\n",
    "print(course[\"topics\"])\n",
    "print(course[\"topics\"][1])"
   ],
   "id": "t01-126"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try it: dictionaries\n",
    "\n",
    "Create `phone = {\"brand\": \"Nova\", \"year\": 2024}`. Add a `\"price\"` of 499, change the year to 2025, print the dictionary, then print the `\"color\"` with `get()` and `\"unknown\"` as the default."
   ],
   "id": "t01-127"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-128"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Adding and changing both use `phone[key] = value`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "phone = {\"brand\": \"Nova\", \"year\": 2024}\n",
    "phone[\"price\"] = 499\n",
    "phone[\"year\"] = 2025\n",
    "print(phone)\n",
    "print(phone.get(\"color\", \"unknown\"))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "{'brand': 'Nova', 'year': 2025, 'price': 499}\n",
    "unknown\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-129"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.11 Sets\n",
    "A set keeps each value **once** and has **no order**. Great for removing duplicates and comparing groups. Careful: `{}` is an empty **dictionary**; an empty set is `set()`."
   ],
   "id": "t01-130"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = {3, 1, 2, 3, 1}\n",
    "print(nums)            # duplicates disappear\n",
    "\n",
    "empty_set = set()      # an empty set\n",
    "empty_dict = {}        # careful: {} is an empty DICT\n",
    "print(type(empty_set))\n",
    "print(type(empty_dict))"
   ],
   "id": "t01-131"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grades = [90, 85, 90, 70, 85]\n",
    "unique = set(grades)\n",
    "print(len(unique))      # how many different grades\n",
    "print(sorted(unique))   # as a sorted list"
   ],
   "id": "t01-132"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Add and remove:** `remove` gives an error if the item is missing, `discard` does not."
   ],
   "id": "t01-133"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {1, 2, 3}\n",
    "s.add(4)        # add one item\n",
    "s.add(2)        # already there: nothing changes\n",
    "print(s)\n",
    "s.remove(1)     # remove (error if missing)\n",
    "s.discard(10)   # remove if present (no error)\n",
    "print(s)"
   ],
   "id": "t01-134"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Removing something that is not there:\n",
    "\n",
    "```python\n",
    "s = {1, 2, 3}\n",
    "s.remove(10)\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "KeyError: 10\n",
    "```"
   ],
   "id": "t01-135"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Comparing sets:**\n",
    "- `a | b` union: in a **or** b\n",
    "- `a & b` intersection: in **both**\n",
    "- `a - b` difference: in a but **not** in b\n",
    "- `a ^ b` symmetric difference: in **exactly one**"
   ],
   "id": "t01-136"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {1, 2, 3, 4}\n",
    "b = {3, 4, 5, 6}\n",
    "print(a | b)   # union: in a OR b\n",
    "print(a & b)   # intersection: in a AND b\n",
    "print(a - b)   # difference: in a but not in b\n",
    "print(b - a)\n",
    "print(a ^ b)   # symmetric difference: in exactly one of them"
   ],
   "id": "t01-137"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {1, 2, 3, 4}\n",
    "b = {3, 4, 5, 6}\n",
    "print(a.union(b))\n",
    "print(a.intersection(b))\n",
    "print(a.difference(b))\n",
    "print(3 in a)"
   ],
   "id": "t01-138"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try it: sets\n",
    "\n",
    "Monday visitors: `{\"Ali\", \"Ben\", \"Cara\"}`. Tuesday visitors: `{\"Ben\", \"Cara\", \"Dana\"}`. Print, in sorted order, who came on both days and who came at least once."
   ],
   "id": "t01-139"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-140"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Both days: `&`. At least once: `|`. Use `sorted()` for a fixed order.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "monday = {\"Ali\", \"Ben\", \"Cara\"}\n",
    "tuesday = {\"Ben\", \"Cara\", \"Dana\"}\n",
    "print(sorted(monday & tuesday))   # came both days\n",
    "print(sorted(monday | tuesday))   # came at least once\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "['Ben', 'Cara']\n",
    "['Ali', 'Ben', 'Cara', 'Dana']\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-141"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.12 Converting between types\n",
    "| Code | Result | What happened |\n",
    "|---|---|---|\n",
    "| `int(\"5\")` | `5` | text → whole number |\n",
    "| `float(3)` | `3.0` | whole number → decimal |\n",
    "| `str(10)` | `\"10\"` | number → text |\n",
    "| `int(3.9)` | `3` | cuts off the decimals, does not round |\n",
    "| `list(\"abc\")` | `['a', 'b', 'c']` | text → list of characters |\n",
    "| `set([1, 1, 2])` | `{1, 2}` | list → set, duplicates removed |"
   ],
   "id": "t01-142"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(int(\"42\"))      # text -> int\n",
    "print(float(\"3.5\"))   # text -> float\n",
    "print(str(99))        # number -> text\n",
    "print(int(7.9))       # float -> int: cuts the decimals\n",
    "print(float(7))       # int -> float"
   ],
   "id": "t01-143"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This matters because data from files and websites often arrives as **text**. The text `\"5\"` is not the number `5`, and `\"5\" + \"5\"` gives `\"55\"`, not 10:"
   ],
   "id": "t01-144"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"5\" + \"5\")             # text: glued together\n",
    "print(int(\"5\") + int(\"5\"))   # numbers: added\n",
    "print(\"5\" * 3)               # text repeated"
   ],
   "id": "t01-145"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(list(\"abc\"))                  # text -> list of characters\n",
    "print(tuple([1, 2, 3]))             # list -> tuple\n",
    "print(set([1, 1, 2]))               # list -> set (duplicates removed)\n",
    "print(list((4, 5)))                 # tuple -> list\n",
    "print(dict([(\"a\", 1), (\"b\", 2)]))   # pairs -> dict"
   ],
   "id": "t01-146"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**When conversion fails:** `int()` cannot read a decimal number from text.\n",
    "\n",
    "```python\n",
    "print(int(\"3.5\"))\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "ValueError: invalid literal for int() with base 10: '3.5'\n",
    "```"
   ],
   "id": "t01-147"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(int(float(\"3.5\")))   # text -> float -> int"
   ],
   "id": "t01-148"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Reading what the user types: `input()`.** It always gives back **text**, so convert it before doing maths. When you run the next cell, type a number in the box and press Enter."
   ],
   "id": "t01-149"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age_text = input(\"How old are you? \")   # input() ALWAYS gives text\n",
    "age = int(age_text)                      # convert before doing maths\n",
    "print(\"Next year you will be\", age + 1)"
   ],
   "id": "t01-150"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Key points: data types**\n",
    "> - `type(x)` tells you the type; `isinstance(x, int)` checks it.\n",
    "> - `/` always gives a float; decimals are not exact, so round when displaying.\n",
    "> - Strings: positions start at 0, `[-1]` is the last, `[start:stop:step]` slices. Strings cannot be changed in place.\n",
    "> - Empty and zero values are falsy: `0`, `\"\"`, `[]`, `{}`, `None`.\n",
    "> - list = ordered, changeable; tuple = ordered, fixed; dict = key → value; set = unique values, no order.\n",
    "> - `b = a` does not copy a list (use `.copy()`). `{}` is an empty dict; an empty set is `set()`.\n",
    "> - `input()` always returns text: convert with `int()` or `float()`."
   ],
   "id": "t01-151"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Operators\n",
    "Operators are symbols that do something with values. In `5 + 3`, `+` is the operator.\n",
    "\n",
    "### 3.1 Arithmetic operators\n",
    "| Operator | Meaning | Example | Result |\n",
    "|---|---|---|---|\n",
    "| `+` | Add | `7 + 2` | `9` |\n",
    "| `-` | Subtract | `7 - 2` | `5` |\n",
    "| `*` | Multiply | `7 * 2` | `14` |\n",
    "| `/` | Divide (always a float) | `7 / 2` | `3.5` |\n",
    "| `//` | Floor division | `7 // 2` | `3` |\n",
    "| `%` | Remainder (modulus) | `7 % 2` | `1` |\n",
    "| `**` | Power | `2 ** 3` | `8` |"
   ],
   "id": "t01-152"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 17\n",
    "b = 5\n",
    "print(a + b)    # add\n",
    "print(a - b)    # subtract\n",
    "print(a * b)    # multiply\n",
    "print(a / b)    # divide (always a float)\n",
    "print(a // b)   # floor division\n",
    "print(a % b)    # remainder\n",
    "print(a ** 2)   # power"
   ],
   "id": "t01-153"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`n % 2 == 0` means n is even. Together, `//` and `%` split a total into whole units and the rest:"
   ],
   "id": "t01-154"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 47\n",
    "print(n % 2)        # remainder after dividing by 2\n",
    "print(n % 2 == 0)   # True would mean \"even\""
   ],
   "id": "t01-155"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_minutes = 135\n",
    "hours = total_minutes // 60     # whole hours\n",
    "minutes = total_minutes % 60    # what is left over\n",
    "print(f\"{hours} h {minutes} min\")"
   ],
   "id": "t01-156"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 Order of operations\n",
    "Brackets first, then `**`, then a minus sign in front, then `* / // %`, then `+ -`, then comparisons, then `not`, `and`, `or`. When in doubt, add brackets."
   ],
   "id": "t01-157"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(2 + 3 * 4)      # * before +\n",
    "print((2 + 3) * 4)    # brackets first\n",
    "print(10 - 4 - 3)     # left to right\n",
    "print(-2 ** 2)        # ** before the minus sign!\n",
    "print((-2) ** 2)"
   ],
   "id": "t01-158"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 Assignment operators\n",
    "`x += 5` is short for `x = x + 5`. The same works for `-=`, `*=`, `/=`, `//=`, `%=` and `**=`."
   ],
   "id": "t01-159"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 10\n",
    "x += 5     # x = x + 5\n",
    "print(x)\n",
    "x -= 3     # x = x - 3\n",
    "print(x)\n",
    "x *= 2     # x = x * 2\n",
    "print(x)\n",
    "x /= 4     # x = x / 4  (now a float)\n",
    "print(x)"
   ],
   "id": "t01-160"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 7\n",
    "n //= 2    # n = n // 2\n",
    "print(n)\n",
    "n **= 3    # n = n ** 3\n",
    "print(n)\n",
    "n %= 5     # n = n % 5\n",
    "print(n)\n",
    "\n",
    "greeting = \"Hello\"\n",
    "greeting += \", Aylin\"   # += also works on text\n",
    "print(greeting)"
   ],
   "id": "t01-161"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.4 Comparison operators\n",
    "`==` equal, `!=` not equal, `>` `<` `>=` `<=`. They always give `True` or `False`.\n",
    "\n",
    "**Important:** `=` **stores** a value, `==` **compares** two values."
   ],
   "id": "t01-162"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(5 == 5)\n",
    "print(5 != 3)\n",
    "print(3 > 5)\n",
    "print(3 < 5)\n",
    "print(5 >= 5)\n",
    "print(4 <= 3)"
   ],
   "id": "t01-163"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(5 == 5.0)              # same value\n",
    "print(\"5\" == 5)              # text is never equal to a number\n",
    "print(\"apple\" < \"banana\")    # text is compared alphabetically\n",
    "print(\"Zebra\" < \"apple\")     # capital letters come first!\n",
    "print([1, 2] == [1, 2])      # lists compared item by item"
   ],
   "id": "t01-164"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Chained comparisons** work like in maths:"
   ],
   "id": "t01-165"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 25\n",
    "print(18 <= age < 65)   # same as: 18 <= age and age < 65"
   ],
   "id": "t01-166"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.5 Logical operators\n",
    "| Operator | Meaning |\n",
    "|---|---|\n",
    "| `and` | True only if both are true |\n",
    "| `or` | True if at least one is true |\n",
    "| `not` | flips True to False and back |"
   ],
   "id": "t01-167"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 20\n",
    "has_ticket = True\n",
    "is_vip = False\n",
    "\n",
    "print(age >= 18 and has_ticket)              # both must be True\n",
    "print(has_ticket or is_vip)                  # at least one\n",
    "print(not is_vip)                            # flips the value\n",
    "print(age >= 18 and (has_ticket or is_vip))  # brackets make it clear"
   ],
   "id": "t01-168"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.6 Membership operators\n",
    "`in` and `not in` work on strings, lists, tuples, sets and dictionaries (where they check the keys)."
   ],
   "id": "t01-169"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"py\" in \"python\")                       # inside a string\n",
    "print(3 in [1, 2, 3])                         # inside a list\n",
    "print(\"grape\" not in [\"apple\", \"banana\"])\n",
    "print(5 in {1, 3, 5})                         # inside a set\n",
    "print(\"name\" in {\"name\": \"Aylin\"})            # dict: checks keys"
   ],
   "id": "t01-170"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.7 Identity operators\n",
    "`is` checks whether two names point to the **same object**, not just equal values. Use it to check for `None`."
   ],
   "id": "t01-171"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = None\n",
    "print(result is None)      # the right way to check for None\n",
    "\n",
    "a = [1, 2, 3]\n",
    "b = [1, 2, 3]\n",
    "c = a\n",
    "print(a == b)    # same values?            True\n",
    "print(a is b)    # the very same object?   False\n",
    "print(a is c)    # c is just another name for a"
   ],
   "id": "t01-172"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Try it: operators\n",
    "\n",
    "A video is `3725` seconds long. Use `//` and `%` to print how many hours, minutes and seconds that is."
   ],
   "id": "t01-173"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-174"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "One hour is 3600 seconds. Hours: `// 3600`. Then work with what is left over: `% 3600`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "seconds = 3725\n",
    "hours = seconds // 3600\n",
    "minutes = seconds % 3600 // 60\n",
    "rest = seconds % 60\n",
    "print(hours, minutes, rest)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "1 2 5\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-175"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Key points: operators**\n",
    "> - `/` gives a float; `//` rounds down; `%` is the remainder.\n",
    "> - `x += 1` means `x = x + 1`.\n",
    "> - Comparisons give True/False. `=` stores, `==` compares. You can chain: `18 <= age < 65`.\n",
    "> - `and` needs both, `or` needs one, `not` flips. Use brackets to make combined conditions clear.\n",
    "> - `in` checks membership (keys for dictionaries). Use `is None` to check for None."
   ],
   "id": "t01-176"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Jupyter tips\n",
    "| Keys | What happens |\n",
    "|---|---|\n",
    "| **Shift + Enter** | run the cell and move to the next one |\n",
    "| **Ctrl + Enter** | run the cell and stay on it |\n",
    "| **Esc**, then **A** / **B** | insert a new cell above / below |\n",
    "| **Esc**, then **M** / **Y** | turn the cell into Markdown / back into code |\n",
    "| **Esc**, then **D D** | delete the cell |\n",
    "\n",
    "- **Edit mode**: you are typing inside a cell. **Command mode** (press Esc): single keys act on the whole cell.\n",
    "- The **kernel** remembers your variables between cells. Run cells in order. If something is strange: **Kernel → Restart Kernel and Run All Cells**.\n",
    "- Install extra libraries in Anaconda Prompt or Terminal: `conda install package_name` (try first) or `pip install package_name`."
   ],
   "id": "t01-177"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Common mistakes\n",
    "These cells are shown as text so that \"Run All\" keeps going. Copy one into a new cell if you want to see the error yourself."
   ],
   "id": "t01-178"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Forgetting that counting starts at 0**\n",
    "\n",
    "```python\n",
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(fruits[3])\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "IndexError: list index out of range\n",
    "```"
   ],
   "id": "t01-179"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Mixing text and numbers** (convert first: `int(\"5\") + 5`)\n",
    "\n",
    "```python\n",
    "print(\"5\" + 5)\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "TypeError: can only concatenate str (not \"int\") to str\n",
    "```"
   ],
   "id": "t01-180"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Misspelling a name or using the wrong capitals**\n",
    "\n",
    "```python\n",
    "age = 20\n",
    "print(Age)\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "NameError: name 'Age' is not defined. Did you mean: 'age'?\n",
    "```"
   ],
   "id": "t01-181"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Forgetting quotes around text** (write `print(\"Hello\")`)\n",
    "\n",
    "```python\n",
    "print(Hello)\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "NameError: name 'Hello' is not defined\n",
    "```"
   ],
   "id": "t01-182"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Forgetting to close a bracket or quote**\n",
    "\n",
    "```python\n",
    "print(\"Hello\"\n",
    "```\n",
    "\n",
    "Python stops with:\n",
    "\n",
    "```\n",
    "SyntaxError: '(' was never closed\n",
    "```"
   ],
   "id": "t01-183"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Using `=` when you mean `==`:** `age = 18` stores 18; `age == 18` asks whether age is 18."
   ],
   "id": "t01-184"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Practice exercises\n",
    "Try each one before opening the hint. There is usually more than one correct answer."
   ],
   "id": "t01-185"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Warm-up"
   ],
   "id": "t01-186"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1\n",
    "\n",
    "Create variables for your name, age, and favourite number, then print a sentence using an f-string."
   ],
   "id": "t01-187"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-188"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Put an `f` before the quotes and the variable names inside `{ }`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "my_name = \"Aylin\"\n",
    "my_age = 20\n",
    "favourite_number = 7\n",
    "print(f\"My name is {my_name}, I am {my_age} and my favourite number is {favourite_number}.\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "My name is Aylin, I am 20 and my favourite number is 7.\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-189"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2\n",
    "\n",
    "Calculate the area of a rectangle with width 8 and height 5, and store it in a variable called `area`."
   ],
   "id": "t01-190"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-191"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Area is width times height.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "width = 8\n",
    "height = 5\n",
    "area = width * height\n",
    "print(area)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "40\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-192"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3\n",
    "\n",
    "Use `%` to check whether the number 47 is even or odd."
   ],
   "id": "t01-193"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-194"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "A number is even when `n % 2 == 0`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "number = 47\n",
    "print(number % 2)        # remainder 1 means odd\n",
    "print(number % 2 == 0)   # is it even?\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "1\n",
    "False\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-195"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4\n",
    "\n",
    "Make a list of 5 favourite foods, then print the first and last items."
   ],
   "id": "t01-196"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-197"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "First: position `0`. Last: position `-1`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "foods = [\"pizza\", \"sushi\", \"pasta\", \"salad\", \"pancakes\"]\n",
    "print(foods[0])\n",
    "print(foods[-1])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "pizza\n",
    "pancakes\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-198"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5\n",
    "\n",
    "Create a dictionary describing a book (title, author, pages) and print the author."
   ],
   "id": "t01-199"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-200"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Use `book[\"author\"]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "book = {\"title\": \"Animal Farm\", \"author\": \"George Orwell\", \"pages\": 112}\n",
    "print(book[\"author\"])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "George Orwell\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-201"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 6\n",
    "\n",
    "Write a comparison that checks whether a person aged 17 can vote (age 18 or over) and print the result."
   ],
   "id": "t01-202"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-203"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`>=` gives True or False.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "age = 17\n",
    "can_vote = age >= 18\n",
    "print(can_vote)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "False\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-204"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Core practice"
   ],
   "id": "t01-205"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 7\n",
    "\n",
    "Convert `25` degrees Celsius to Fahrenheit with `F = C * 9 / 5 + 32` and print a sentence with both values."
   ],
   "id": "t01-206"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-207"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Store celsius, calculate fahrenheit, use an f-string.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "celsius = 25\n",
    "fahrenheit = celsius * 9 / 5 + 32\n",
    "print(f\"{celsius} C is {fahrenheit} F\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "25 C is 77.0 F\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-208"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 8\n",
    "\n",
    "Clean the text `\"  python FOR data science  \"`: remove the outer spaces, make it Title Case, print it, then print how many words it has."
   ],
   "id": "t01-209"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-210"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`raw.strip().title()`; count words with `len(text.split())`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "raw = \"  python FOR data science  \"\n",
    "clean = raw.strip().title()\n",
    "print(clean)\n",
    "print(len(clean.split()))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Python For Data Science\n",
    "4\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-211"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 9\n",
    "\n",
    "Turn 200 minutes into hours and minutes, and print it like `3 h 20 min`."
   ],
   "id": "t01-212"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-213"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Hours: `// 60`. Minutes left: `% 60`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "total_minutes = 200\n",
    "hours = total_minutes // 60\n",
    "minutes = total_minutes % 60\n",
    "print(f\"{hours} h {minutes} min\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3 h 20 min\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-214"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 10\n",
    "\n",
    "Start with `shopping = [\"milk\", \"bread\"]`. Add \"eggs\" at the end, put \"apples\" first, remove \"bread\", then print the list and its length."
   ],
   "id": "t01-215"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-216"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`append`, `insert(0, ...)`, `remove`, `len`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "shopping = [\"milk\", \"bread\"]\n",
    "shopping.append(\"eggs\")\n",
    "shopping.insert(0, \"apples\")\n",
    "shopping.remove(\"bread\")\n",
    "print(shopping)\n",
    "print(len(shopping))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "['apples', 'milk', 'eggs']\n",
    "3\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-217"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 11\n",
    "\n",
    "Store grades in `{\"Aylin\": 88, \"Omar\": 92, \"Lea\": 75}`. Add Sam with 81, then print the dictionary, the average grade, and the highest grade."
   ],
   "id": "t01-218"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-219"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Average: `sum(grades.values()) / len(grades)`. Highest: `max(grades.values())`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "grades = {\"Aylin\": 88, \"Omar\": 92, \"Lea\": 75}\n",
    "grades[\"Sam\"] = 81\n",
    "average = sum(grades.values()) / len(grades)\n",
    "print(grades)\n",
    "print(average)\n",
    "print(max(grades.values()))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "{'Aylin': 88, 'Omar': 92, 'Lea': 75, 'Sam': 81}\n",
    "84.0\n",
    "92\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-220"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 12\n",
    "\n",
    "Count the different names in `[\"anna\", \"ben\", \"anna\", \"cara\", \"ben\"]`. Then find who visited on both days: Monday `{\"anna\", \"ben\", \"cara\"}` and Tuesday `{\"ben\", \"dan\"}`."
   ],
   "id": "t01-221"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-222"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`len(set(...))` counts different values; both days: `&`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "visitors = [\"anna\", \"ben\", \"anna\", \"cara\", \"ben\"]\n",
    "print(len(set(visitors)))\n",
    "\n",
    "monday = {\"anna\", \"ben\", \"cara\"}\n",
    "tuesday = {\"ben\", \"dan\"}\n",
    "print(sorted(monday & tuesday))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3\n",
    "['ben']\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-223"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Challenge"
   ],
   "id": "t01-224"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 13\n",
    "\n",
    "A shop sells 3 items at 4.50 each with 18% tax. Print the subtotal, the tax and the total, each with 2 decimal places."
   ],
   "id": "t01-225"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-226"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Subtotal = price * quantity; tax = subtotal * 0.18; format with `:.2f`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "price = 4.5\n",
    "quantity = 3\n",
    "tax_rate = 0.18\n",
    "\n",
    "subtotal = price * quantity\n",
    "tax = subtotal * tax_rate\n",
    "total = subtotal + tax\n",
    "print(f\"Subtotal: {subtotal:.2f}\")\n",
    "print(f\"Tax:      {tax:.2f}\")\n",
    "print(f\"Total:    {total:.2f}\")\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "Subtotal: 13.50\n",
    "Tax:      2.43\n",
    "Total:    15.93\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-227"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 14\n",
    "\n",
    "A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. Check 2024 and 2026."
   ],
   "id": "t01-228"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-229"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Combine `%`, `==`, `!=`, `and`, `or` and brackets.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "year = 2024\n",
    "is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "print(is_leap)\n",
    "\n",
    "year = 2026\n",
    "is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "print(is_leap)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "True\n",
    "False\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-230"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 15\n",
    "\n",
    "Build a username from `first = \"Aylin\"`, `last = \"Demir\"`, `birth_year = 2005`: first letter of the first name + last name, all small letters, then the last two digits of the year."
   ],
   "id": "t01-231"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here"
   ],
   "id": "t01-232"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`first[0]`, `.lower()`, `str(birth_year)[-2:]`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "first = \"Aylin\"\n",
    "last = \"Demir\"\n",
    "birth_year = 2005\n",
    "username = (first[0] + last).lower() + str(birth_year)[-2:]\n",
    "print(username)\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "ademir05\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t01-233"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Check yourself\n",
    "Answer in your head first, then open the answer."
   ],
   "id": "t01-234"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1.** What does `print(7 // 2)` show?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`3`. `//` divides and drops the decimals.\n",
    "\n",
    "</details>\n",
    "\n",
    "**2.** What does `print(10 / 2)` show?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`5.0`. `/` always gives a float.\n",
    "\n",
    "</details>\n",
    "\n",
    "**3.** What is `type(\"20\")`?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`str`, because of the quotes.\n",
    "\n",
    "</details>\n",
    "\n",
    "**4.** What does `print(\"5\" + \"5\")` show?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`55`. Adding strings joins them.\n",
    "\n",
    "</details>\n",
    "\n",
    "**5.** `word = \"Python\"`. What is `word[-2]`?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`'o'`. `-1` is the last character, `-2` the one before it.\n",
    "\n",
    "</details>\n",
    "\n",
    "**6.** What is `len(\"Data Science\")`?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`12`. The space counts too.\n",
    "\n",
    "</details>\n",
    "\n",
    "**7.** `student = {\"name\": \"Aylin\"}`. What is `\"Aylin\" in student`?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`False`. `in` checks dictionary keys, not values.\n",
    "\n",
    "</details>\n",
    "\n",
    "**8.** What does `print({1, 2, 2, 3})` show?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`{1, 2, 3}`. A set keeps each value once.\n",
    "\n",
    "</details>\n",
    "\n",
    "**9.** What is `bool(\"\")`?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`False`. Empty strings are falsy.\n",
    "\n",
    "</details>\n",
    "\n",
    "**10.** Which line stores 18 in age: `age == 18` or `age = 18`?\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the answer</b></summary>\n",
    "\n",
    "`age = 18`. One `=` stores, two `==` compare.\n",
    "\n",
    "</details>"
   ],
   "id": "t01-235"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Summary\n",
    "| Concept | Meaning | Example |\n",
    "|---|---|---|\n",
    "| Variable | A named box that stores a value | `age = 20` |\n",
    "| `int` / `float` | Whole numbers / decimal numbers | `7` / `3.14` |\n",
    "| `str` | Text in quotes | `\"Hello\"` |\n",
    "| `bool` | True or False | `True` |\n",
    "| `None` | No value yet | `result = None` |\n",
    "| `list` / `dict` | Ordered group / key-value pairs | `[1, 2, 3]` / `{\"a\": 1}` |\n",
    "| `tuple` / `set` | Fixed ordered group / unique values | `(1, 2)` / `{1, 2}` |\n",
    "| Type conversion | Change a value's type | `int(\"5\")` |\n",
    "| Arithmetic operators | Do maths | `+ - * / // % **` |\n",
    "| Assignment operators | Update a variable | `+= -= *= /=` |\n",
    "| Comparison operators | Compare, give True/False | `== != > < >= <=` |\n",
    "| Logical operators | Combine conditions | `and`, `or`, `not` |\n",
    "| Membership / identity | Inside? / same object? | `in`, `is` |\n",
    "| Jupyter | Run code in cells | Shift + Enter |\n",
    "\n",
    "**Next topic:** control flow with `if` and `else`, where booleans and comparisons start making decisions."
   ],
   "id": "t01-236"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
