{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Collections\n", "\n", "- Lists, Tuples, Dictionaries\n", "- Indexing\n", "- Mutating\n", "- `chr` and `ord`" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Collections: Lists " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "A list is a mutable collection of ordered items, that can be of mixed type. Lists are created using square brackets.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### List examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Define a list\n", "lst = [1, 'a', True]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Print out the contents of a list\n", "print(lst)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check the type of a list\n", "type(lst)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "slide" } }, "source": [ "### Indexing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "Indexing refers to selecting an item from within a collection. Indexing is done with square brackets.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Define a list \n", "my_lst = ['Julian', 'Amal', 'Richard', 'Juan', 'Xuan']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Indexing: Count forward, starting at 0, with positive numbers\n", "print(my_lst[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Indexing: Count backward, starting at -1, with negative numbers\n", "print(my_lst[-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Indexing: Grab a group of adjacent items using `start:stop`, called a slice\n", "print(my_lst[2:4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# indexing to end of list\n", "print(my_lst[2:])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Indexing from beginning of list\n", "print(my_lst[:4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# slicing by skipping a value [start:stop:step]\n", "print(my_lst[0:4:2])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Reminders" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Python is zero-based (The first index is '0')\n", "- Negative indices index backwards through a collection\n", "- A sequence of indices (called a slice) can be accessed using `start:stop`\n", " - In this contstruction, `start` is included then every element until `stop`, not including `stop` itself\n", " - To skip values in a sequence use `start:stop:step`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "
\n", "Starting at zero is a convention (some) languages use that comes from how variables are stored in memory\n", ", and 'pointers' to those locations.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #1\n", "\n", "What would be the appropriate line of code to return `['butter', '&', 'jelly']`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "q3_lst = ['peanut', 'butter', '&','jelly']" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- A) `q3_lst[2:4]`\n", "- B) `q3_lst[1:3]`\n", "- C) `q3_lst[:-2]`\n", "- D) `q3_lst[-3:]`\n", "- E) `q3_lst[1:4:2]`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "Note: The following has been added to the notes due to student questions in previous iterations. This and the following two cells are *not* someting you'll be tested on. Including as an FYI for those curious.\n", "\n", "You *can* return ['jelly', '&', 'butter'] but it combines two different concepts.\n", "\n", "1. the `start:stop` now refers to indices in the reverse.\n", "2. `-1` is used as the step to reverse the output.\n", "\n", "More details about `step`: \n", "`step`: the amount by which the index increases, defaults to 1. If it's negative, you're slicing over the iterable in reverse." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "# slice in reverse\n", "q3_lst[-1:-4:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "# you can use forward indexing\n", "# makes this a little clearer\n", "q3_lst[3:0:-1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Mutating a List" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "Lists are mutable, meaning after definition, you can update and change things about the list.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# reminder what's in my_lst \n", "my_lst" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Redefine a particular element of the list\n", "my_lst[2] = 'Rich'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check the contents of the list\n", "print(my_lst)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #2\n", "\n", "What would the following code accommplish?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "lst_update = [1, 2, 3, 0, 5]\n", "lst_update[3] = 4 " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- A) replace 0 with 4 in `lst_update`\n", "- B) replace 4 with 0 in `lst_update`\n", "- C) no change to `lst_update`\n", "- D) produce an error\n", "- E) I'm not sure" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Collections: Tuples " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "A tuple is an immutable collection of ordered items, that can be of mixed type. Tuples are created using parentheses. Tuples are used when you don't want to be able to update the items in your tuple.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Tuple Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Define a tuple\n", "tup = (2, 'b', False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Print out the contents of a tuple\n", "print(tup)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check the type of a tuple\n", "type(tup)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Index into a tuple\n", "tup[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Get the length of a tuple\n", "len(tup)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Tuples are Immutable" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Tuples are immutable - meaning after they defined, you can't change them\n", "# This code will produce an error.\n", "tup[2] = 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #3\n", "\n", "Which of the following specifies a tuple of 2 items?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "item_A = ['100-11-2233', '200-22-3344']\n", "item_B = ('100-11-2233', '200-22-3344')\n", "item_C = ['100-11-2233', '200-22-3344', 1234, 0]\n", "item_D = ('100-11-2233', '200-22-3344', 1234, 0)\n", "item_E = (12)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- A) item_A\n", "- B) item_B\n", "- C) item_C\n", "- D) item_D\n", "- E) item_E" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Dictionaries\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "A dictionary is mutable collection of items, that can be of mixed-type, that are stored as key-value pairs.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dictionaries as Key-Value Collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Create a dictionary\n", "dictionary = {'key_1' : 'value_1', 'key_2' : 'value_2'}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check the contents of the dictionary\n", "print(dictionary)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check the type of the dictionary\n", "type(dictionary)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Dictionaries also have a length\n", "# length refers to how many pairs there are\n", "len(dictionary)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "source": [ "### Dictionaries: Indexing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# Dictionaries are indexed using their keys\n", "dictionary['key_1']" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dictionaries are mutable\n", "\n", "This means that dictionaries, once created, values *can* be updated." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "completed_assignment = {\n", " 'A1234' : True,\n", " 'A5678' : False,\n", " 'A9123' : True\n", "}\n", "\n", "completed_assignment" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# change value of specified key\n", "completed_assignment['A5678'] = True\n", "completed_assignment" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Because dictionaries are mutable, key-value pairs can also be removed from the dictionary using `del`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "print(completed_assignment)\n", "len(completed_assignment)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "## remove key-value pair using del\n", "del completed_assignment['A5678']\n", "\n", "print(completed_assignment)\n", "len(completed_assignment)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Additional Dictionary Properties\n", "\n", "- Only one value per key. No duplicate keys allowed. \n", " - If duplicate keys specified during assignment, the last assignment wins." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Last duplicate key assigned wins\n", "{'Student' : 97, 'Student': 88, 'Student' : 91}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- **keys** must be of an immutable type (string, tuple, integer, float, etc)\n", "- Note: **values** can be of any type" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# lists are not allowed as key types\n", "# this code will produce an error\n", "{['Student'] : 97}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Dictionary keys are case sensitive.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "{'Student' : 97, 'student': 88, 'STUDENT' : 91}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #4\n", "\n", "Fill in the '---' in the code below to return the value stored in the second key." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "height_dict = {'height_1' : 60, 'height_2': 68, 'height_3' : 65, 'height_4' : 72}\n", "height_dict[---]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- A) I did it\n", "- B) I think I did it...\n", "- C) I tried and am stuck\n", "- D) No clue where to start..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #5\n", "\n", "Write the code that would create a dictionary `car` that stores values about your dream car's `make`, `model`, and `year`.\n", "\n", "- A) I did it\n", "- B) I think I did it...\n", "- C) I tried and am stuck\n", "- D) No clue where to start..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Revisiting membership: `in` operator" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "The in operator asks whether an element is present inside a collection, and returns a boolean answer. \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "# Define a new list and dictionary to work with\n", "lst_again = [True, 13, None, 'apples']\n", "dict_again = {'Shannon': 33, 'Josh': 41}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Check if a particular element is present in the list\n", "True in lst_again" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# The `in` operator can also be combined with the `not` operator\n", "'19' not in lst_again" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# In a dictionary, checks if value is a key\n", "'Shannon' in dict_again" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# does not check for values in dictionary\n", "33 in dict_again" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #6\n", "\n", "After executing the following code, what will be the value of `output`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "ex2_lst = [0, False, 'ten', None]\n", "\n", "bool_1 = False in ex2_lst\n", "bool_2 = 10 not in ex2_lst\n", "\n", "output = bool_1 and bool_2\n", "\n", "print(output)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "-" } }, "source": [ "- a) True\n", "- b) False\n", "- c) This code will fail\n", "- d) I don't know" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Unicode" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "Unicode is a system of systematically and consistently representing characters. \n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "source": [ "Every character has a unicode `code point` - an integer that can be used to represent that character. \n", "\n", "If a computer is using unicode, it displays a requested character by following the unicode encodings of which `code point` refers to which character. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### ORD & CHR" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "ord returns the unicode code point for a one-character string.\n", "
\n", "\n", "
\n", "chr returns the character encoding of a code point. \n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### ord & chr examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(ord('a'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(chr(97))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Inverses\n", "\n", "`ord` and `chr` are inverses of one another. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "inp = 'b'\n", "out = chr(ord(inp))\n", "\n", "assert inp == out\n", "print('Input: \\t', inp, '\\nOutput: ', out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Clicker Question #7\n", "\n", "Write a function `convert_with_offset` that will take a single input parameter`character` (a single character in as input).\n", "\n", "Inside the function, the code will convert the input `character` to its unicode code point and then add an `offset` value of 50, returning that value as the output from the function.\n", "\n", "A) I did it!  B) I think I did it.  C) I tried but I am stuck.  D) Super duper lost." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "## YOUR CODE HERE " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# TEST YOUR FUNCTION HERE" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Aside: Aliases\n", "\n", "Note: This was introduced in the Variables lecture." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# Make a variable, and an alias\n", "a = 1\n", "b = a\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Here, the value 1 is assigned to the variable `a`. \n", "\n", "We then make an **alias** of `a` and store that in the variable `b`. \n", "\n", "Now, the same value (1) is stored in both `a` (the original) and `b` (the alias)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "What if we change the value of the original variable (`a`) - what happens to `b`?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #8\n", "\n", "After executing the following code, what will the values stored in `a` and `b` be?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# Make a variable & an alias\n", "# change value of original variable\n", "a = 1\n", "b = a\n", "a = 2\n", "\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- A) `a` and `b` both store 1\n", "- B) `a` and `b` both store 2\n", "- C) `a` stores 2 `b` stores 1\n", "- D) `a` stores 1 `b` stores 2\n", "- E) No clue" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Reminder: integers are **immutable**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Alias: mutable types\n", "\n", "What happens if we make an alias of a **mutable** variable, like a list?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "first_list = [1, 2, 3, 4]\n", "alias_list = first_list\n", "alias_list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "#change second value of first_list\n", "first_list[1] = 29\n", "first_list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# check alias_list\n", "alias_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For *mutable* type variables, when you change one, both change." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Clicker Question #9\n", "\n", "After executing the following code, what will the second value stored in `second_tuple`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# Make a variable & an alias\n", "# change value of original variable\n", "my_tuple = (1, 2, 3, 4)\n", "second_tuple = my_tuple\n", "my_tuple[1] = 29 " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- A) 1\n", "- B) 2\n", "- C) 29\n", "- D) This will Error\n", "- E) I'm lost." ] }, { "cell_type": "markdown", "metadata": { "jp-MarkdownHeadingCollapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### Why allow aliasing? \n", "\n", "Aliasing can get confusing and be difficult to track, so why does Python allow it?\n", "\n", "Well, it's more efficient to point to an alias than to make an entirely new copy of a a very large variable storing a lot of data. \n", "\n", "Python allows for the confusion, in favor of being more efficient." ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.8" }, "rise": { "scroll": true } }, "nbformat": 4, "nbformat_minor": 4 }