{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with lists\n",
    "\n",
    "In this tutorial, you will learn how to create, modify, and iterate over lists in Python.\n",
    "\n",
    "**Time commitment:** 15–20 minutes\n",
    "\n",
    "**Prerequisites:**\n",
    "\n",
    "- Python 3.12 or later installed on your machine\n",
    "- Basic familiarity with Python syntax (variables, strings, numbers)\n",
    "\n",
    "## Learning objectives\n",
    "\n",
    "By the end of this tutorial, you will be able to:\n",
    "\n",
    "- Create lists and access items by index\n",
    "- Add, remove, and modify items in a list\n",
    "- Iterate over lists using `for` loops\n",
    "- Use slicing to extract parts of a list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What is a list?\n",
    "\n",
    "A **list** is an ordered, mutable collection of items. Lists are one of the most commonly used data structures in Python. You can store any type of item in a list — numbers, strings, or even other lists.\n",
    "\n",
    "Lists are created using square brackets `[]`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing items by index\n",
    "\n",
    "Each item in a list has a position called an **index**. Python uses zero-based indexing, which means the first item is at index 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "print(fruits[0])  # First item\n",
    "print(fruits[1])  # Second item\n",
    "print(fruits[2])  # Third item"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also use negative indices to count from the end of the list. The index `-1` refers to the last item:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "print(fruits[-1])  # Last item\n",
    "print(fruits[-2])  # Second to last"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding the length of a list\n",
    "\n",
    "Use the built-in `len()` function to find out how many items are in a list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "print(len(fruits))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding items\n",
    "\n",
    "Lists are **mutable**, which means you can change them after they are created. Use `append()` to add an item to the end of a list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\"]\n",
    "fruits.append(\"cherry\")\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use `insert()` to add an item at a specific position:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"cherry\"]\n",
    "fruits.insert(1, \"banana\")  # Insert at index 1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Removing items\n",
    "\n",
    "Use `remove()` to remove a specific item by its value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "fruits.remove(\"banana\")\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use `pop()` to remove an item by its index (and get it back):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "removed = fruits.pop(1)  # Remove item at index 1\n",
    "print(f\"Removed: {removed}\")\n",
    "print(f\"Remaining: {fruits}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Iterating over a list\n",
    "\n",
    "Use a `for` loop to go through each item in a list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "for fruit in fruits:\n",
    "    print(f\"I like {fruit}!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you need the index as well as the value, use `enumerate()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "for index, fruit in enumerate(fruits):\n",
    "    print(f\"{index}: {fruit}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Slicing\n",
    "\n",
    "**Slicing** lets you extract a portion of a list. The syntax is `list[start:stop]`, where `start` is included and `stop` is excluded:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = [0, 1, 2, 3, 4, 5]\n",
    "\n",
    "print(numbers[1:4])   # Items at index 1, 2, 3\n",
    "print(numbers[:3])    # First three items\n",
    "print(numbers[3:])    # Everything from index 3 onwards\n",
    "print(numbers[-2:])   # Last two items"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Checking membership\n",
    "\n",
    "Use the `in` keyword to check whether an item exists in a list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "\n",
    "print(\"banana\" in fruits)\n",
    "print(\"grape\" in fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise: build a to-do list\n",
    "\n",
    "Now it is your turn! Create a list of three tasks you need to do today. Then:\n",
    "\n",
    "1. Add a fourth task\n",
    "2. Remove the second task (you have already done it)\n",
    "3. Print the remaining tasks with their positions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "Here is one way to complete the exercise:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tasks = [\"Buy groceries\", \"Reply to emails\", \"Go for a walk\"]\n",
    "\n",
    "# Add a fourth task\n",
    "tasks.append(\"Read a chapter\")\n",
    "\n",
    "# Remove the second task\n",
    "tasks.pop(1)\n",
    "\n",
    "# Print remaining tasks with positions\n",
    "for index, task in enumerate(tasks, start=1):\n",
    "    print(f\"{index}. {task}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "In this tutorial, you learned how to:\n",
    "\n",
    "- Create lists using square brackets `[]`\n",
    "- Access items using positive and negative indices\n",
    "- Add items with `append()` and `insert()`\n",
    "- Remove items with `remove()` and `pop()`\n",
    "- Iterate over lists with `for` loops and `enumerate()`\n",
    "- Extract portions of a list using slicing\n",
    "- Check whether an item exists using `in`\n",
    "\n",
    "## What is next\n",
    "\n",
    "In upcoming tutorials, you will explore:\n",
    "\n",
    "- **Tuples** — immutable sequences for data that should not change\n",
    "- **Dictionaries** — key-value pairs for fast lookups\n",
    "- **Sets** — collections of unique items\n",
    "- **Comprehensions** — concise ways to build data structures"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
