{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8261678",
   "metadata": {},
   "source": [
    "# Basic Types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c60f107",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# Find project root by looking for _config.yml\n",
    "current = Path.cwd()\n",
    "for parent in [current, *current.parents]:\n",
    "    if (parent / '_config.yml').exists():\n",
    "        project_root = parent\n",
    "        break\n",
    "else:\n",
    "    project_root = Path.cwd().parent.parent\n",
    "\n",
    "# Add project root to path\n",
    "sys.path.insert(0, str(project_root))\n",
    "\n",
    "# Import shared teaching helpers and cell magics\n",
    "from shared import thinkpython, diagram, jupyturtle, structshape\n",
    "from shared.download import download\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c47e196",
   "metadata": {},
   "source": [
    "## Numbers\n",
    "\n",
    "Python has three basic number types: integers (e.g., 1), floating-point numbers (e.g., 1.0), and complex numbers. The standard mathematical order of operations is followed for basic arithmetic operations. Note that dividing two integers results in a floating-point number, and dividing by zero will generate an error. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4af3c49d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Integer: 42\n",
      "Floating-point number: 3.14\n",
      "Complex number: (3+4j)\n"
     ]
    }
   ],
   "source": [
    "integer = 42             # integer (natural number)\n",
    "floating = 3.14          # floating-point number (real number)\n",
    "complex_num = 3 + 4j     # complex number\n",
    "\n",
    "print(f\"Integer: {integer}\")\n",
    "print(f\"Floating-point number: {floating}\")\n",
    "print(f\"Complex number: {complex_num}\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad242e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 <class 'int'>\n",
      "3 <class 'int'>\n",
      "0.5 <class 'float'>\n",
      "1.0 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "num1 = 1 + 1\n",
    "num2 = 1 * 3         \n",
    "num3 = 1 / 2\n",
    "num4 = 2 / 2             ### output 1.0, not 1\n",
    "\n",
    "print(num1, type(num1))\n",
    "print(num2, type(num2))\n",
    "print(num3, type(num3))\n",
    "print(num4, type(num4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5de2f00f",
   "metadata": {},
   "source": [
    "The modulus operation (also known as the mod function) is represented by the percent sign. It returns what remains after the division:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d18ab8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "print(4 % 2)\n",
    "print(5 % 2)\n",
    "print(9 // 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08647c99",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: How many hours and minutes are there in 12500 seconds?\n",
    "### Use numeric operators\n",
    "### Your code starts here\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20564769",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 hours, 28 minutes, and 20 seconds\n"
     ]
    }
   ],
   "source": [
    "### solution\n",
    "total_seconds = 12500\n",
    "\n",
    "remaining_seconds = total_seconds % 60\n",
    "minutes = total_seconds // 60\n",
    "remaining_minutes = minutes % 60\n",
    "# hours = total_seconds // 3600  # 3600 seconds in an hour\n",
    "hours = minutes // 60  # 60 minutes in an hour\n",
    "\n",
    "print(f\"{hours} hours, {remaining_minutes} minutes, and {remaining_seconds} seconds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d65db00",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "## Strings\n",
    "\n",
    "Python represents sequences of letters, which are called **strings** because the letters are strung together like beads on a necklace. Strings are one of the most commonly used built-in types. \n",
    "\n",
    "Strings represent text (**character sequences**) and are created using quotes. Strings can be created using single or double quotes. You can also wrap a single quote in double quotes if you need to include a quote within the string.\n",
    "\n",
    "Some features about strings in Python:\n",
    "\n",
    "- A string is a sequence of characters enclosed in quotes.\n",
    "- You can use single, double, or triple quotation marks to create a string; they are all legal.\n",
    "- Double quotes make it easy to write a string that contains an apostrophe, which is treated the same as single quotes in programming."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "759292f6",
   "metadata": {},
   "source": [
    "### Creating String Variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b50fc1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice Alice Alice\n"
     ]
    }
   ],
   "source": [
    "name1 = \"Alice\"\n",
    "name2 = 'Alice'\n",
    "name3 = \"\"\"Alice\"\"\"\n",
    "print(name1, name2, name3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "416b1674",
   "metadata": {},
   "source": [
    "### Quotation Marks & Escaping\n",
    "\n",
    "An escape sequence is a combination of a backslash `\\` with a special character/symbol to treat the special characters as regular strings. \n",
    "\n",
    "Observe the following two strings. You see that we are trying to honor the single quote in a pair of double quotes. For that, you can either:\n",
    "\n",
    "- **enclose** the single quotation mark inside the double quotation marks as in the first example, or enclose the double quote with single quotes like the 2nd. \n",
    "- use an **escape sequence**. Note that in s3, we have three single quotation marks, and we are able to produce the same results as the first example.\n",
    "- Starting from s4, we have a syntax error because the quotation marks are not properly closed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "88c76c5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's a sunny day.\n",
      "It\"s a sunny day.\n",
      "It's a sunny day.\n"
     ]
    }
   ],
   "source": [
    "s1 = \"It's a sunny day.\"   ### double quote enclose single\n",
    "s2 = 'It\"s a sunny day.'   ### ... but legal\n",
    "s3 = 'It\\'s a sunny day.'  ### escape single quote\n",
    "print(s1, s2, s3, sep=\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c731a686",
   "metadata": {},
   "source": [
    "The enclosing quotations have to be symmetrical, otherwise you would have a syntax error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "561ad1d1-a9a5-4f8f-ab80-53ff0b82e69b",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%expect SyntaxError\n",
    "s4 = 'It's a sunny day.'   ### illegal: not closed\n",
    "s5 = \"It\"s a sunny day.\"   ### illegal: not closed\n",
    "print(s4, s5, sep=\"\\n\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd504d5f",
   "metadata": {},
   "source": [
    "Commonly used escape sequences are as follows.\n",
    "\n",
    "| Sequence | Meaning                                  |\n",
    "| -------- | ---------------------------------------- |\n",
    "| `\\'`     | Single quote inside single-quoted string |\n",
    "| `\\\"`     | Double quote inside double-quoted string |\n",
    "| `\\\\`     | Backslash literal                        |\n",
    "| `\\n`     | Newline                                  |\n",
    "| `\\t`     | Tab                                      |\n",
    "\n",
    "Some examples of escape sequences are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c72504ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First line \n",
      " Second line\n",
      "Name:\tDoris\n",
      "He said \"Python is great!\"\n",
      "I'm learning Python\n"
     ]
    }
   ],
   "source": [
    "print(\"First line \\n Second line\")      ### \\n: gives new line (break)\n",
    "print(\"Name:\\tDoris\")                   ### \\t: tab\n",
    "print(\"He said \\\"Python is great!\\\"\")   ### print \" inside \"\"\n",
    "print('I\\'m learning Python')           ### show ' as a character, not quotation mark"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cfa3dbf",
   "metadata": {},
   "source": [
    "Observe and see if the print output makes sense to you."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b8c270dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PS C:\\Users\\tychen>\n"
     ]
    }
   ],
   "source": [
    "print(\"PS C:\\\\Users\\\\tychen>\")          ### \\\\ to show \\"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb9cc652",
   "metadata": {},
   "source": [
    "### Common string operations\n",
    "\n",
    "1. **Concatenation**: `\"Hello\" + \" \" + \"World\"` → `\"Hello World\"`\n",
    "2. **Repetition**: `\"Ha\" * 3` → `\"HaHaHa\"`. The multiplication (`*`) operator also works with strings; it makes multiple copies of a string and concatenates them.\n",
    "3. **Length**: `len(\"Python\")` → `6`. Python provides a useful function called `len` that computes the length of a string. Notice that `len` counts the letters between the quotes, but not the quotes. In collection types, `len` counts the number of elements in the collection.\n",
    "4. **Indexing**: `\"Python\"[0]` → `\"P\"`\n",
    "5. **Slicing**: `\"Python\"[0:3]` → `\"Pyt\"`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "880b82df",
   "metadata": {},
   "source": [
    "Here we have one example for each of the string operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "2c13cb11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. Hello, Alice!\n",
      "2. Hello, Alice!Hello, Alice!Hello, Alice!\n",
      "3. Length: 5\n",
      "4. First letter: A\n",
      "5. First three letters: Ali\n"
     ]
    }
   ],
   "source": [
    "name = \"Alice\"\n",
    "greeting = \"Hello, \" + name + \"!\"              ### 1. concatenation\n",
    "\n",
    "print(\"1.\", greeting)                           \n",
    "print(\"2.\", greeting * 3)                      ### 2. repetition\n",
    "print(f\"3. Length: {len(name)}\")               ### 3. length + f-string\n",
    "print(f\"4. First letter: {name[0]}\")           ### 4. indexing + f-string\n",
    "print(f\"5. First three letters: {name[0:3]}\")  ### 5. slicing + f-string"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e1d151d",
   "metadata": {},
   "source": [
    "**Exercise: String Manipulation**\n",
    "- What will be the output of this code?  \n",
    "- Guess first and then activate Live Coding and run the code in the cell below to find out.\n",
    "\n",
    "```python\n",
    "s = \"hello\"\n",
    "t = s\n",
    "s = s.upper()\n",
    "print(t)\n",
    "```\n",
    "A) HELLO  \n",
    "B) hello  \n",
    "C) Error  \n",
    "D) None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8f472f0",
   "metadata": {
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Use this code cell to test.\n",
    "\n",
    "\n",
    "\n",
    "###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "ac242ae9",
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "### solution: comment out the print statement \n",
    "#   to see the output of t\n",
    "\n",
    "s = \"hello\"\n",
    "t = s\n",
    "s = s.upper()\n",
    "# print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "253445b9",
   "metadata": {},
   "source": [
    "### Indexing and Slicing \n",
    "\n",
    "Strings are **sequences** of characters. You can access specific elements using **square bracket notation**. Python indexing starts at zero. Negative indexing in slicing starts with -1 from the end element."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "310534e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "o\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "s = 'hello'\n",
    "print(s[0])\n",
    "print(s[4])\n",
    "print(s[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c16e13d",
   "metadata": {},
   "source": [
    "**Slice notation** allows you to grab parts of a string. Use a **`colon`** to specify the start and stop indices. The **stop index is not included**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b5b24f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "applebananacherry\n",
      "apple\n",
      "banana\n",
      "cherry\n",
      "\n",
      "cherr\n"
     ]
    }
   ],
   "source": [
    "s = 'applebananacherry'\n",
    "print(s[0:])     ### applebananacherry\n",
    "print(s[:5])     ### apple\n",
    "print(s[5:11])   ### banana\n",
    "print(s[-6:])    ### cherry\n",
    "print(s[-6:0])   ### \n",
    "print(s[-6:-1])  ### cherr (stop exclusive)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "830e97e5",
   "metadata": {},
   "source": [
    "## Boolean Type\n",
    "\n",
    "The type of a Boolean value is `bool`. The only two Boolean values are `True` and `False`. They are built-in keywords and must be capitalized.\n",
    "\n",
    "Booleans represent truth values and are used extensively in conditional comparisons and logical expressions, which involve comparison/relational operators and logical operators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc9a7c53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "3 > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84371442",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num1 is not greator than num2.\n"
     ]
    }
   ],
   "source": [
    "num1, num2, num3, num4 = 1, 2, 3, 4\n",
    "\n",
    "if (num1 > num2):                  ### comparison operator \">\"\n",
    "    print(\"num1 is greater than num2.\")\n",
    "else:\n",
    "    print(\"num1 is not greator than num2.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14c573b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Is student: True\n",
      "Is graduated: False\n"
     ]
    }
   ],
   "source": [
    "is_student = True\n",
    "is_graduated = False\n",
    "\n",
    "print(f\"Is student: {is_student}\")      \n",
    "print(f\"Is graduated: {is_graduated}\")   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8bf5829",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The person is a student.\n"
     ]
    }
   ],
   "source": [
    "if is_student:                          ### in this case, True\n",
    "    print(\"The person is a student.\")\n",
    "else:\n",
    "    print(\"The person is not a student.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "699e7526",
   "metadata": {},
   "source": [
    "### Boolean Expressions\n",
    "\n",
    "A **boolean expression** is an expression that is either `True` or `False`.\n",
    "For example, the following expressions use the equals operator, `==`, which compares two values and produces `True` if they are equal and `False` otherwise. Remember that there are six relational/comparison operators: `==` `!=` `>` `<` `>=` `<=`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f68a702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True False\n"
     ]
    }
   ],
   "source": [
    "t_f_1 = 5 == 5\n",
    "t_f_2 = 5 == 7\n",
    "print(t_f_1, t_f_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c85eb11",
   "metadata": {},
   "source": [
    "A common error is to use a **single equal sign** (`=`) instead of a **double equal sign** (`==`).\n",
    "Remember that `=` assigns a value to a variable and `==` compares two values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61547727",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 5       ### assignment\n",
    "y = 7       ### assignment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08e36cda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x == y      ### comparison/relational"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3789222f",
   "metadata": {},
   "source": [
    "`True` and `False` are special values that belong to the type `bool`;\n",
    "they are not strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bff1182",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n",
      "<class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "print(type(True))\n",
    "print(type(False))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03d12fba",
   "metadata": {},
   "source": [
    "The `==` operator is one of the **6 relational operators**; the others are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9150d1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x != y               # x is not equal to y\n",
    "x > y                # x is greater than y\n",
    "x < y                # x is less than y\n",
    "x >= y               # x is greater than or equal to y\n",
    "x <= y               # x is less than or equal to y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "333d5976",
   "metadata": {},
   "source": [
    "### Truthy and Falsy\n",
    "\n",
    "In Python, every object has a truth value in Boolean contexts like `if` and `while`. \"Truthy\" values behave like `True`, while \"falsy\" values behave like `False` - empty containers and zero are falsy; most non-empty values are truthy.\n",
    "\n",
    "- Falsy: `False`, `None`, `0`, `0.0`, `''`, `[]`, `{}`, `set()`\n",
    "- Truthy: most other values (e.g., `'0'`, `[0]`).\n",
    "- Quick check: `bool(value)`;\n",
    "- idiom: `if not items:` checks emptiness."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4330fcdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(None, None)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Minimal truthy/falsy examples\n",
    "print(bool(0)), print(bool(''))        # False\n",
    "print(bool('0')), print(bool([0]))     # True"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
