{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f0f1b9ee",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# More on Strings and Lists, Slicing, Mutability\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9768d1a2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Group Exercise\n",
    "\n",
    "Run the following lines of code one-by-one in your interactive shell, and discuss what each of them is doing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ae49002f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20, 30, 40, 50, 60]\n",
      "[20, 30, 40, 50, 60, 70, 80, 90]\n",
      "[0, 10, 20, 30, 40, 50, 60]\n",
      "own fox jumps\n",
      "3\n",
      "35\n",
      "lazy\n"
     ]
    }
   ],
   "source": [
    "my_list = [0,10,20,30,40,50,60,70,80,90]\n",
    "print( my_list[2:7] )\n",
    "print( my_list[2:] )\n",
    "print( my_list[:7] )\n",
    "my_string = \"The quick brown fox jumps over the lazy dog\"\n",
    "print( my_string[12:25] )\n",
    "print( my_list.index(30)  )\n",
    "print( my_string.index(\"lazy\")  )\n",
    "lazy_index = my_string.index(\"lazy\") \n",
    "print(my_string[lazy_index:(lazy_index+4)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f890d7d",
   "metadata": {},
   "source": [
    "This is called __slicing__."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24896741",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Notice that `my_list[start:end]` gives you the slice of the list starting at index `start` and ending _just before_ index `end`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1db0c773",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Slicing to make a copy\n",
    "\n",
    "Recall that lists only reference their data, so when you assign, it copies the reference to the same list - you end up with two names for the same list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d3026d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 9999, 4, 5]\n",
      "[1, 2, 9999, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "x = [1,2,3,4,5]\n",
    "y = x\n",
    "y[2] = 9999\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba87ffea",
   "metadata": {},
   "source": [
    "But, a slice will always be a copy, so you can make a real copy of a whole list using a slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "10c1b6ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 9999, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "x = [1,2,3,4,5]\n",
    "y = x[:]\n",
    "y[2] = 9999\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "348562e3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Strings vs. Lists\n",
    "\n",
    "sequences, can use `[]` notation, can loop through them both, can check their length with `len()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1805be16",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "my_list = [0.0, 1.1, 42, 3.14]\n",
    "my_string = \"The quick brown fox jumps over the lazy dog\"\n",
    "\n",
    "print( my_list[2] )\n",
    "print( my_string[2] )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a31ec224",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "work with __concatenation__ `+` and __repition__ `*` operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f2f28eb3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 1.1, 42, 3.14, 1, 2, 3]\n",
      "The quick brown fox jumps over the lazy dog, and the dog doesn't care.\n",
      "[0.0, 1.1, 42, 3.14, 0.0, 1.1, 42, 3.14, 0.0, 1.1, 42, 3.14]\n",
      "The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog\n"
     ]
    }
   ],
   "source": [
    "print( my_list + [1,2,3] )\n",
    "print( my_string + \", and the dog doesn't care.\" )\n",
    "print( my_list * 3 )\n",
    "print( my_string * 3 ) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8fa560e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "can process them with `in`, `count()`, and `index()`\n",
    "* for lists, this works with _items_\n",
    "* for strings, this works with any _substring_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a298c7e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print( 1.1 in my_list )\n",
    "print( \"fox\" in my_string )\n",
    "print( my_list.count(42) )\n",
    "print( my_string.count(\"ox\") )\n",
    "print( my_list.index(42) )\n",
    "print( my_string.index(\"ox\") )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28e07ca8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Group Exercise: \n",
    "\n",
    "Before running these, discuss in your groups what you think will happen. Then, try it and see if you were right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "31484b37",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0, 1.1, 5, 3.14]\n",
      "['T', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']\n",
      "['T', 'h', 'y', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']\n",
      "Thy quick brown fox jumps over the lazy dog\n"
     ]
    }
   ],
   "source": [
    "my_list = [0.0, 1.1, 42, 3.14]\n",
    "my_list[2] = 5\n",
    "print(my_list)\n",
    "\n",
    "my_string = \"The quick brown fox jumps over the lazy dog\"\n",
    "#my_string[2] = \"y\"\n",
    "#print(my_string)\n",
    "my_string_list = list(my_string)\n",
    "print(my_string_list)\n",
    "my_string_list[2] = \"y\"\n",
    "print(my_string_list)\n",
    "my_string =  \"\".join(my_string_list) \n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61209324",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## How are strings and lists different?\n",
    "\n",
    "You might think that a string is just a list of characters, and that's pretty close. However, they differ in one important property:\n",
    "\n",
    "Lists are __mutable__, meaning they can be _changed_\n",
    "\n",
    "Strings are __immutable__, meaning they can't be _changed_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f368bda",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "This means that with a _string_, you can't use any list method that changes the list\n",
    "* `append()`\n",
    "* `insert()`\n",
    "* `remove()`\n",
    "* `pop()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1e667f1",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "We will see other examples of both _mutable_ and _immutable_ objects in Python."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
