{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Python\n",
    "\n",
    "A short, hands-on tour of the Python language. Run each cell with **Shift+Enter** and try editing the code — you learn more by breaking things than by reading.\n",
    "\n",
    "**Sections**\n",
    "1. Printing and variables\n",
    "2. Data types\n",
    "3. Arithmetic and comparison\n",
    "4. Strings\n",
    "5. Lists\n",
    "6. Dictionaries\n",
    "7. `if` / `elif` / `else`\n",
    "8. Loops\n",
    "9. Functions\n",
    "10. Mini exercises\n",
    "\n",
    "**Every section ends with an exercise and a worked solution.** Try the exercise before you open the solution — that is where the learning happens.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Printing and variables\n",
    "\n",
    "`print(...)` shows a value. The `=` sign assigns a value to a name (a *variable*)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello, Python!\")\n",
    "\n",
    "name = \"Aroon\"\n",
    "age = 28\n",
    "print(name, \"is\", age, \"years old\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An **f-string** lets you embed variables directly inside a string by prefixing it with `f`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"{name} is {age} years old\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 1 — printing and variables\n",
    "\n",
    "Make two variables, `city` and `country`, and set them to your own city and country.\n",
    "Print one sentence using an f-string, like `I live in Chiang Mai, Thailand.`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 1** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "city = \"Chiang Mai\"\n",
    "country = \"Thailand\"\n",
    "print(f\"I live in {city}, {country}.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Data types\n",
    "\n",
    "The most common types you'll meet first:\n",
    "\n",
    "| Type    | Example          | Meaning              |\n",
    "|---------|------------------|----------------------|\n",
    "| `int`   | `42`             | whole number         |\n",
    "| `float` | `3.14`           | decimal number       |\n",
    "| `str`   | `\"hello\"`        | text                 |\n",
    "| `bool`  | `True` / `False` | yes/no value         |\n",
    "| `None`  | `None`           | \"nothing\" / absent   |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(42))\n",
    "print(type(3.14))\n",
    "print(type(\"hello\"))\n",
    "print(type(True))\n",
    "print(type(None))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 2 — data types\n",
    "\n",
    "Before you run anything, write down what type you think each of these is:\n",
    "\n",
    "`19`  ·  `19.0`  ·  `\"19\"`  ·  `False`  ·  `None`\n",
    "\n",
    "Then print the real type of each one and check your answers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 2** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(19))       # int   — a whole number\n",
    "print(type(19.0))     # float — the .0 makes it a decimal\n",
    "print(type(\"19\"))     # str   — the quotes make it text\n",
    "print(type(False))    # bool\n",
    "print(type(None))     # NoneType\n",
    "\n",
    "# \"19\" is text, not a number. This proves it:\n",
    "print(\"19\" + \"19\")    # 1919  (joined, not added)\n",
    "print(19 + 19)        # 38"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Arithmetic and comparison\n",
    "\n",
    "Standard operators: `+ - * / // % **`\n",
    "- `/`  divides and gives a float\n",
    "- `//` divides and throws away the remainder (integer division)\n",
    "- `%`  is the remainder\n",
    "- `**` is power"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(7 + 2)    # 9\n",
    "print(7 / 2)    # 3.5\n",
    "print(7 // 2)   # 3\n",
    "print(7 % 2)    # 1\n",
    "print(2 ** 10)  # 1024"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparisons return a `bool`: `==  !=  <  >  <=  >=`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(3 == 3)\n",
    "print(3 != 4)\n",
    "print(10 > 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 3 — arithmetic\n",
    "\n",
    "A lesson is `total_minutes = 205` minutes long.\n",
    "\n",
    "Print how many whole **hours** that is, and how many **minutes** are left over.\n",
    "Use `//` and `%`. The answer is 3 hours and 25 minutes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 3** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_minutes = 205\n",
    "\n",
    "hours = total_minutes // 60     # whole hours, remainder thrown away\n",
    "minutes = total_minutes % 60    # what is left over\n",
    "\n",
    "print(f\"{total_minutes} minutes = {hours} hours and {minutes} minutes\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Strings\n",
    "\n",
    "Strings are text. You can join them with `+`, repeat them with `*`, and they have many built-in methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "greeting = \"sawadee\"\n",
    "city = \"Chiang Mai\"\n",
    "\n",
    "print(greeting + \" \" + city)\n",
    "print(greeting * 3)\n",
    "print(len(city))           # number of characters\n",
    "print(city.upper())\n",
    "print(city.lower())\n",
    "print(city.replace(\"Mai\", \"Rai\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Index into a string with `[ ]`. Python counts from **0**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"python\"\n",
    "print(word[0])      # 'p'\n",
    "print(word[-1])     # 'n'  (last character)\n",
    "print(word[0:3])    # 'pyt' (a slice)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 4 — strings\n",
    "\n",
    "Start with `phrase = \"sawadee chiang mai\"`.\n",
    "\n",
    "Print:\n",
    "1. the whole phrase in CAPITAL letters\n",
    "2. how many characters it has\n",
    "3. the first character\n",
    "4. the last three characters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 4** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"sawadee chiang mai\"\n",
    "\n",
    "print(phrase.upper())     # SAWADEE CHIANG MAI\n",
    "print(len(phrase))        # 18\n",
    "print(phrase[0])          # s\n",
    "print(phrase[-3:])        # mai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Lists\n",
    "\n",
    "A **list** is an ordered collection. Items can be any type and the list can grow or shrink."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"mango\", \"banana\", \"longan\"]\n",
    "\n",
    "print(fruits[0])         # first item\n",
    "print(len(fruits))       # how many\n",
    "\n",
    "fruits.append(\"durian\")  # add to end\n",
    "fruits.remove(\"banana\")  # remove by value\n",
    "\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 5 — lists\n",
    "\n",
    "Start with `market = [\"mango\", \"rice\", \"chilli\"]`.\n",
    "\n",
    "1. Add `\"lime\"` to the end.\n",
    "2. Remove `\"rice\"`.\n",
    "3. Print how many items are left.\n",
    "4. Print the first and the last item."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 5** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "market = [\"mango\", \"rice\", \"chilli\"]\n",
    "\n",
    "market.append(\"lime\")\n",
    "market.remove(\"rice\")\n",
    "\n",
    "print(market)          # ['mango', 'chilli', 'lime']\n",
    "print(len(market))     # 3\n",
    "print(market[0])       # mango\n",
    "print(market[-1])      # lime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Dictionaries\n",
    "\n",
    "A **dictionary** maps *keys* to *values*. Use `{ }` and look things up with `[key]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student = {\n",
    "    \"name\": \"Ploy\",\n",
    "    \"age\": 12,\n",
    "    \"level\": \"P6\",\n",
    "}\n",
    "\n",
    "print(student[\"name\"])\n",
    "\n",
    "student[\"score\"] = 87   # add a new key\n",
    "print(student)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 6 — dictionaries\n",
    "\n",
    "Build a dictionary called `teacher` with the keys `name`, `subject` and `years`.\n",
    "\n",
    "Then add a new key `school`, and print just the teacher's name and school."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 6** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "teacher = {\n",
    "    \"name\": \"Kru Naree\",\n",
    "    \"subject\": \"English\",\n",
    "    \"years\": 8,\n",
    "}\n",
    "\n",
    "teacher[\"school\"] = \"Wat Don Chan\"\n",
    "\n",
    "print(teacher[\"name\"], \"teaches at\", teacher[\"school\"])\n",
    "print(teacher)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. `if` / `elif` / `else`\n",
    "\n",
    "Decisions. Indentation (4 spaces) is how Python knows what's inside each branch — it's part of the syntax, not just style."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "score = 72\n",
    "\n",
    "if score >= 80:\n",
    "    grade = \"A\"\n",
    "elif score >= 70:\n",
    "    grade = \"B\"\n",
    "elif score >= 60:\n",
    "    grade = \"C\"\n",
    "else:\n",
    "    grade = \"F\"\n",
    "\n",
    "print(f\"Score {score} -> grade {grade}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 7 — if / elif / else\n",
    "\n",
    "Set `temp = 34`.\n",
    "\n",
    "Print `\"Very hot\"` if the temperature is 35 or more, `\"Hot\"` if it is 30 to 34,\n",
    "`\"Warm\"` if it is 20 to 29, and `\"Cool\"` for anything below 20.\n",
    "\n",
    "Change the number and run it again to test every branch."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 7** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp = 34\n",
    "\n",
    "if temp >= 35:\n",
    "    advice = \"Very hot\"\n",
    "elif temp >= 30:\n",
    "    advice = \"Hot\"\n",
    "elif temp >= 20:\n",
    "    advice = \"Warm\"\n",
    "else:\n",
    "    advice = \"Cool\"\n",
    "\n",
    "print(f\"{temp} degrees -> {advice}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Loops\n",
    "\n",
    "A `for` loop walks through items in a collection. `range(n)` gives the numbers `0` to `n-1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for fruit in [\"mango\", \"banana\", \"longan\"]:\n",
    "    print(\"I like\", fruit)\n",
    "\n",
    "print(\"---\")\n",
    "\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A `while` loop runs as long as a condition is true."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 1\n",
    "while n < 100:\n",
    "    n = n * 2\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 8 — loops\n",
    "\n",
    "1. Use a `for` loop to print the 7 times table from 7 x 1 to 7 x 12.\n",
    "2. Use a `while` loop to count down from 5 to 1, then print `\"Go!\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 8** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1 — for loop\n",
    "for i in range(1, 13):\n",
    "    print(f\"7 x {i} = {7 * i}\")\n",
    "\n",
    "print(\"---\")\n",
    "\n",
    "# 2 — while loop\n",
    "n = 5\n",
    "while n > 0:\n",
    "    print(n)\n",
    "    n = n - 1\n",
    "print(\"Go!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Functions\n",
    "\n",
    "A function packages up code so you can reuse it. Define with `def`, call with `name(...)`. `return` sends a value back."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet(name):\n",
    "    return f\"Sawadee, {name}!\"\n",
    "\n",
    "print(greet(\"Ploy\"))\n",
    "print(greet(\"Aroon\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def average(numbers):\n",
    "    return sum(numbers) / len(numbers)\n",
    "\n",
    "print(average([10, 20, 30, 40]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Functions can have **default values** for their parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet(name, greeting=\"Sawadee\"):\n",
    "    return f\"{greeting}, {name}!\"\n",
    "\n",
    "print(greet(\"Ploy\"))\n",
    "print(greet(\"Ploy\", greeting=\"Hello\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ✏️ Exercise 9 — functions\n",
    "\n",
    "Write a function `bmi(weight_kg, height_m)` that returns the body mass index:\n",
    "weight divided by height squared.\n",
    "\n",
    "Give `height_m` a default value of `1.70`. Test it with and without the height."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Solution 9** — try it yourself first, then run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bmi(weight_kg, height_m=1.70):\n",
    "    \"\"\"Return body mass index: weight / height squared.\"\"\"\n",
    "    return weight_kg / (height_m ** 2)\n",
    "\n",
    "print(round(bmi(65), 1))          # uses the default 1.70\n",
    "print(round(bmi(65, 1.55), 1))    # gives its own height"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10. Mini exercises\n",
    "\n",
    "Try these in the cells below. Solutions are at the bottom — peek only after you've tried.\n",
    "\n",
    "1. Print the even numbers from 1 to 20.\n",
    "2. Given `scores = [88, 72, 95, 60, 41]`, count how many are 70 or above.\n",
    "3. Write a function `is_long_word(word)` that returns `True` if a word has more than 5 letters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 1: your code here\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 2: your code here\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 3: your code here\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### Solutions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1\n",
    "for n in range(1, 21):\n",
    "    if n % 2 == 0:\n",
    "        print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2\n",
    "scores = [88, 72, 95, 60, 41]\n",
    "count = 0\n",
    "for s in scores:\n",
    "    if s >= 70:\n",
    "        count += 1\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3\n",
    "def is_long_word(word):\n",
    "    return len(word) > 5\n",
    "\n",
    "print(is_long_word(\"mango\"))    # False\n",
    "print(is_long_word(\"durian\"))   # True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Next steps\n",
    "\n",
    "When you're comfortable with the above, look into:\n",
    "- **modules**: `import math`, `import random`\n",
    "- **file I/O**: `with open(\"file.txt\") as f:`\n",
    "- **error handling**: `try` / `except`\n",
    "- **list comprehensions**: `[n*2 for n in range(10)]`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
