{
 "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"
   ]
  },
  {
   "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": [
    "## 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": [
    "## 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": [
    "## 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": [
    "## 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": [
    "## 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": [
    "## 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": [
    "## 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": [
    "## 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": [
    "## 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
}
