{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 1 exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.1\n", "\n", "Describe in words the difference between a mutable and immutable object.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.2\n", "\n", "**a)** Describe in words the difference between the `==` operator and the `is` operator.\n", "\n", "**b)** Why should you not use the `==` operator to compare two `float`s?\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.3\n", "\n", "*Do this before Exercise 1.4.* Without running the code, write what will be printed for the following Python codes.\n", "\n", "**a)**\n", "```python\n", "my_list = [1, 2, 3]\n", "my_list[2] = \"four\"\n", "\n", "print(my_list)\n", "```\n", "\n", "
\n", "
\n", "\n", "**b)**\n", "```python\n", "a = 5\n", "b = 2\n", "a = b\n", "\n", "print(a, b)\n", "```\n", "\n", "
\n", "
\n", "\n", "\n", "**c)**\n", "```python\n", "def first_element_103(x):\n", " x[0] = 103\n", " \n", "x = [1, 2, 3]\n", "\n", "first_element_103(x)\n", "\n", "print(x)\n", "```\n", "\n", "
\n", "
\n", "\n", "\n", "\n", "**d)**\n", "```python\n", "def add_103(x):\n", " x += 103\n", " \n", "x = 4\n", "\n", "add_103(x)\n", "\n", "print(x)\n", "```\n", "\n", "
\n", "
\n", "\n", "\n", "**e)**\n", "```python\n", "def add_103(x):\n", " return x + 103\n", " \n", "x = 4\n", "\n", "x = add_103(x)\n", "\n", "print(x)\n", "```\n", "\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "**f)**\n", "```python\n", "def append_103(x):\n", " x.append(103)\n", " \n", "a = [1, 2, 3]\n", "b = a\n", "\n", "append_103(b)\n", "\n", "print(a)\n", "```\n", "\n", "
\n", "
\n", "\n", "\n", "**g)**\n", "```python\n", "add_three_numbers = lambda x, y, z: x + y + z\n", "\n", "print(add_three_numbers(*(5, 6, 7)))\n", "```\n", "\n", "
\n", "
\n", "\n", "\n", "**h)**\n", "```python\n", "print([i for i in range(1, 10) if i % 2])\n", "```\n", "\n", "\n", "
\n", "
\n", "\n", "\n", "**i)** As we will learn when we learn about style, you should not write code like this example; it's overly tricky to read. But it will help see if you understand how list comprehensions work.\n", "```python\n", "print([i if i % 3 == 0 else i**2 for i in range(1, 10) if i % 2])\n", "```\n", "\n", "\n", "
\n", "
\n", "\n", "**j)** This one is tricky. Don't worry if you have trouble with this.\n", "\n", "```python\n", "def append_103(x=[]):\n", " x.append(103)\n", "\n", " return x\n", "\n", "append_103()\n", "append_103()\n", "append_103()\n", "\n", "print(append_103())\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.4\n", "\n", "Check you work from Exercise 1.3 by running the codes in code cells. *Do not* change your answers to Exercise 1.3 after doing this.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.5 (optional)\n", "\n", "Write down any questions or points of confusion that you have." ] } ], "metadata": { "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }