{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.7 \u2014 Lists & Strings as Sequences\n",
    "\n",
    "Every variable so far has held exactly one value. A **list** holds many values in order, under one name \u2014 perfect for an inventory, a party roster, or anything else that grows over the course of a game. Strings turn out to behave a lot like lists too, once you know what to look for."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Idea\n",
    "\n",
    "A list is written with square brackets, values separated by commas:\n",
    "\n",
    "```python\n",
    "inventory = [\"sword\", \"shield\", \"potion\"]\n",
    "```\n",
    "\n",
    "Access one item by its **index** \u2014 counting starts at `0`:\n",
    "\n",
    "```python\n",
    "inventory[0]   # \"sword\"\n",
    "```\n",
    "\n",
    "Loop through every item with `for`:\n",
    "\n",
    "```python\n",
    "for item in inventory:\n",
    "    print(item)\n",
    "```\n",
    "\n",
    "Add to a list with `.append()`, and check how many items it holds with `len()`. Strings support indexing and slicing the same way lists do \u2014 `\"dragon\"[0]` is `\"d\"`, and `\"dragon\"[0:3]` is `\"dra\"` \u2014 but unlike lists, you can't change a string in place; you can only build a new one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A worked example \u2014 Zara's starting inventory\n",
    "\n",
    "inventory = [\"sword\", \"shield\", \"potion\"]\n",
    "\n",
    "print(\"Starting inventory:\")\n",
    "for item in inventory:\n",
    "    print(\"-\", item)\n",
    "\n",
    "inventory.append(\"torch\")\n",
    "print(f\"\\nAfter finding a torch, you're carrying {len(inventory)} items:\")\n",
    "for item in inventory:\n",
    "    print(\"-\", item)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Watch out for:* indexing starts at `0`, not `1` \u2014 `inventory[3]` on a 3-item list is an `IndexError`, since the valid indexes are `0`, `1`, and `2`. And strings are **immutable**: `name[0] = \"Z\"` will error, even though the equivalent works fine on a list."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Guided Practice 1\n",
    "\n",
    "Zara's party grows. Run the cell as-is, then add another party member with `.append()` and re-run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add another name to the party list below and re-run\n",
    "\n",
    "party = [\"Zara\", \"Old Finn\"]\n",
    "\n",
    "print(\"Current party:\")\n",
    "for member in party:\n",
    "    print(\"-\", member)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Guided Practice 2\n",
    "\n",
    "Add a healing potion to the inventory list, then print how many items it holds. Fill in the two blanks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fill in the two blanks\n",
    "\n",
    "inventory = [\"sword\", \"shield\"]\n",
    "inventory.____(\"potion\")          # fill in: the method that adds an item to the end\n",
    "\n",
    "print(f\"You're carrying {____(inventory)} items.\")   # fill in: the function that counts items\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Independent Practice 1\n",
    "\n",
    "Given a list of room names, print each one numbered starting from 1 (not 0) \u2014 e.g. `Room 1: Entrance`, `Room 2: Corridor`, and so on. Use a `for` loop with `range(len(...))`, or `enumerate()` if you've seen it before.\n",
    "\n",
    "**Hint (only if stuck):** `for i in range(len(rooms)): print(f\"Room {i + 1}: {rooms[i]}\")` prints the index alongside the value \u2014 the `+ 1` is what turns 0-indexing into human-friendly numbering."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here \u2014 print a numbered list of rooms, starting from 1\n",
    "\n",
    "rooms = [\"Entrance\", \"Corridor\", \"Treasure Room\"]\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Independent Practice 2\n",
    "\n",
    "No hint this time. Given a monster's name as a string, print its first letter, its last letter, and the whole name reversed (slicing with a step of `-1` reverses a string or list: `name[::-1]`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here \u2014 print the first letter, last letter, and reversed version of a monster's name\n",
    "\n",
    "monster_name = \"Basilisk\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Want More? (Optional)\n",
    "\n",
    "Using slicing, print just the first two items of your inventory list, and separately, just the last two."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Optional stretch \u2014 slice the first two and last two items of a list\n",
    "\n",
    "inventory = [\"sword\", \"shield\", \"potion\", \"torch\"]\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Check Your Understanding\n",
    "\n",
    "In one sentence: what's one thing lists and strings have in common, and one thing that's different between them?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Your answer:** *(double-click to edit this cell)*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
