{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook 1\n", "\n", "Run each commands and try to understand why certain operations behave the way they do before looking at the answers.\n", "\n", "There are 3 parts to this exercise: Numbers, Strings, and Lists, with the answers written under each section." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers\n", "\n", "`2 + 2`\n", "\n", "`10 / 4`\n", "\n", "`50 - 5*6` \n", "\n", "`(50 - 5)*6`\n", "\n", "`5 ** 2` \n", "\n", "`7 % 2`\n", "\n", "`7 // 2`\n", "\n", "`3.14 + 2`\n", "\n", "Assign the integer 20 to the variable `width`\n", "\n", "Assign the float 5 to the variable `height`\n", "\n", "Save `width * height` to the variable `area`\n", "\n", "What is the area?\n", "\n", "What happens if you assign 30 to `width` and print `area` again? \n", "\n", "Re-assign `width * height` to `area` and see if the results changes now. \n", "\n", "Assign `area * 2` to `2timesArea`\n", "\n", "Assign `area + 2` to `return`\n", "\n", "\n", "<br><br>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers - Answers" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 2 " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 / 4 # Note that division of two integers becomes a float" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "50 - 5*6 # multiplication is evaluated before the minus" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "270" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(50 - 5)*6 # Order of operations, parenthesis first" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 ** 2 # To the power of" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 2 # Modulo yields the remainder of the division of 7 by 2 (6/2 has the remainder 1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 // 2 # Floor division returns the highest integer" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.140000000000001" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.14 + 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So why is the example above not 5.14? Python represents floats as base 2 (binary) fractions. This causes some of the numbers to having to be approximated instead of exact. If you need very precise calculations you need to be aware that python behaves this way. To read more about this, click [here](https://docs.python.org/3/tutorial/floatingpoint.html)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "width = 20\n", "height = 5.0\n", "area = width * height\n", "area # Multiplication of a float and an integer results in a float" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "width = 30\n", "area" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When assigning width * height to area, python stores the actual value assigned to area in memory, ie area = 100.0. It does not store the pointer to width * height. To update area with the new inputs we have to actively assign area a value again." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "150.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "area = width * height\n", "area" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (<ipython-input-14-4bdf2ca84b6b>, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"<ipython-input-14-4bdf2ca84b6b>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 2timesArea = area * 2\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "2timesArea = area * 2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (<ipython-input-15-da7854f8d5e4>, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"<ipython-input-15-da7854f8d5e4>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m return = area + 2\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "return = area + 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Why does the above fail? In the first case we are trying to assign a value to a variable name starting with a number, which is not allowed. In the second case we are trying to assign a value to a reserved keyword. However, both of them results in the same error message, a SyntaxError, which sometimes can be tricky to track, so be aware.\n", "\n", "<br><br><br>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings\n", "\n", "\n", "`'Hello World!'` \n", "`\"Hello World!\"`\n", "\n", "`'Oh, no, you didn\\\\'t...'` \n", "`\"Oh, no, you didn't...\"`\n", "\n", "`\"\\\\\"Yes\\\\\", I did!\"` \n", "`'\"Yes\", I did'`\n", "\n", "\n", "`'Hello' ' ' 'World' '!'`\n", "\n", "`s = 'Hello\\tWorld'` \n", "`s` \n", "`print(s)` \n", "\n", "\n", "`s = 'First line.\\nSecond line.'` \n", "`s` \n", "`print(s)` \n", "\n", "`folder = 'C:\\some\\name'` \n", "`print(folder)` \n", "\n", "`folder = r'C:\\some\\name'` \n", "`print(folder)` \n", "\n", "`'hmm ' + 3 * 'miam'` \n", "\n", "`'Py' 'thon'` \n", "\n", "`prefix = 'Py'` \n", "`prefix + 'thon'` \n", "\n", "`text = ('Put several strings within parentheses ' +` \n", "     `'to have them joined together, ' +` \n", "     `'with the use of ' +` \n", "     `'concatenation')` \n", "`text`\n", "\n", "<br><br>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings - Answers" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World!'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello World!' # \"\" and '' can be used interchangeably" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World!'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World!\" " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Oh, no, you didn't...\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Oh, no, you didn\\'t...'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the example above we have to escape the special character ' to prevent python as interpreting it as end of the string. Another way of inserting ' into a string would be to enclose the string in \" instead, as in the example below. That way the ' is interpreted as a normal character." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Oh, no, you didn't...\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Oh, no, you didn't...\"" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\"Yes\", I did!'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\\\"Yes\\\", I did!\"" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\"Yes\", I did'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'\"Yes\", I did'" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World!'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello' ' ' 'World' '!' # Ignores spaces between strings" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello\\tWorld'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'Hello\\tWorld' # Adds a tab between the words\n", "s # Notice that s only shows the content" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\tWorld\n" ] } ], "source": [ "print(s) # While print() renders the content of s" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'First line.\\nSecond line.'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'First line.\\nSecond line.' # \\n adds a newline\n", "s # Without print(), \\n is included in the output" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First line.\n", "Second line.\n" ] } ], "source": [ "print(s) # With print(), \\n produces a new line" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\n", "ame\n" ] } ], "source": [ "folder = 'C:\\some\\name' # Here the \\n will be interpreted as a newline\n", "print(folder)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\\name\n" ] } ], "source": [ "folder = r'C:\\some\\name'\n", "print(folder)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don’t want characters prefaced by \\ to be interpreted as special characters, you can use raw strings by adding an r before the first quote as above" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hmm miammiammiam'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hmm ' + 3 * 'miam'" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Python'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Py' 'thon'" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Python'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prefix = 'Py'\n", "prefix + 'thon'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Put several strings within parentheses to have them joined together, with the use of concatenation'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = ('Put several strings within parentheses ' +\n", " 'to have them joined together, ' +\n", " 'with the use of ' +\n", " 'concatenation')\n", "text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<br><br><br>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists\n", "\n", "`squares = [1, 4, 9, 16, 25]` \n", "`cubes = [1, 8, 27, 64, 125]`\n", "\n", "`squares + cubes` \n", "`squares * 2` \n", "`squares * cubes` \n", "\n", "<br><br>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists - Answers\n", "\n", "Lists are a collection of items grouped together. A list is denoted using comma-separated values between square brackets. A list might contain items of different types, but usually the items all have the same type." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25, 1, 8, 27, 64, 125]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = [1, 4, 9, 16, 25] \n", "cubes = [1, 8, 27, 64, 125]\n", "\n", "squares + cubes # Even though both contains integers and are of equal length they cannot be added together, only concatenated" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares * 2 # Multiplication with 2 will produce a new list with a duplicate of the first list concatenated to the end" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'list'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m<ipython-input-35-27f4ae718739>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msquares\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mcubes\u001b[0m \u001b[1;31m# Multiplication of lists can only be done with integers, not lists\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'list'" ] } ], "source": [ "squares * cubes # Multiplication of lists can only be done with integers, not lists" ] } ], "metadata": { "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.9.4" } }, "nbformat": 4, "nbformat_minor": 2 }