{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assigning variables" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print('Hello World')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "pi = 3.14159265359 # decimal\n", "name = 'Philipp' # text\n", "age = 31 # integer\n", "sky_is_blue = True # boolean" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14159265359\n" ] } ], "source": [ "print(pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Packing multiple assignments on one line" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x, y = 10, 5\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Re-assigning (Updating the variable)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Philipp\n" ] } ], "source": [ "pi = 'Philipp'\n", "print(pi)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "x, y = y, x" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using non-defined variables will raise a NameError" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'non_existent_variable' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnon_existent_variable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'non_existent_variable' is not defined" ] } ], "source": [ "print(non_existent_variable)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tips - other ways to define value" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ten_millions = 10_000_000\n", "ten_millions" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.25" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "small = .25\n", "small" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sci_thousand = 10e2\n", "sci_thousand" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Naming the variable \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. No keywords and operator symbols (class, def, for, ..., +, -, @, etc)\n", "2. No whitespace\n", "2. Cannot start with number" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "counter = 0\n", "pricey_car = 'Mercedes'\n", "income = 120_000" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m class = 'Mercedes'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "class = 'Mercedes'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m pricey@@car\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "pricey@@car" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1y_income = 120_000\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "1y_income = 120_000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Data types" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi = 3.14159265359 # reassing it back\n", "type(pi)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(name)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(age)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sky_is_blue)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Floats and Integers" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "A = 6\n", "B = 5" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A + B" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A - B" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.2" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A / B" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exponent**" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Integer division**" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 / 2" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 // 4 # way faster" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10.0 // 4\n", "2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Reminer**" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Self-assignment" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "count = 0" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count +=1\n", "count" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count -=1\n", "count" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count +=1\n", "count *=2\n", "count" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count **= 2\n", "count" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count /=2\n", "count" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count //= 2\n", "count" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10.0 // 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Order of operations" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.0" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 + 10) / 2" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 / (1 + 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "text1 = 'This is a so-called “speakeasy”'" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "text2 = \"This is Sam’s Tavern\"" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "text3 = ''' This is Sam’s Tavern.\n", "\"Welcome everyone!\" - is written on the door.\n", "'''" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "World!\n" ] } ], "source": [ "print('Hello\\nWorld!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello ' + 'World'" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HelloHelloHello'" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello' * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Methods" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello World'.upper()" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello World'.lower()" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello world'.title()" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello planet'" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello world'.replace('world', 'planet')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Formatting" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' hello'" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello'.rjust(10, ' ')" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello '" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello'.ljust(10, ' ')" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0000000999'" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'999'.zfill(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Format method**" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello beautiful world and our blue planet'" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {} world and our blue {}'.format('beautiful','planet')" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello Beautiful World and our Beautiful Planet!'" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{0} {2} {1} and our {2} {3}!'.format('Hello','World','Beautiful', 'Planet')" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello beautiful world!'" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {adj} world!'.format(adj='beautiful')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**F-strings**" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello beautiful world!'" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adj = 'beautiful'\n", "f'Hello {adj} world!'" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello mr Philipp'" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = 'pHILIPP'\n", "f'Hello mr {name.title()}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Legacy formatting (do not use)**" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello mr. David\n" ] } ], "source": [ "name = 'David'\n", "print('Hello mr. %s' % name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Formatting mini-language**" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Price grew by 34.5% or 45,500'" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pct = .345\n", "value = 45500\n", "f'Price grew by {pct:.1%} or {value:,}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Formatting mini-language link](https://docs.python.org/3/library/string.html#formatspec)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String as iterable" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\"[0]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\"[0:5]" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"World\" in \"Hello World!\"" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Planet\" in \"Hello World!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Boolean" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'World' == 'World'" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi == pi" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"World\" in \"Hello World!\"" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi != pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logical operators\n" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not (5 > 4)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 > 4) | (6 < 5)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 > 4) & (6 < 5)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 > 4) ^ (5 < 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conversion" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"2.5\")" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(\"45\")" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(4.521)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(5)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(True)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(False)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'True'" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(True)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('Hello')" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(115.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Can't convert**" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '2.5'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"2.5\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '2.5'" ] } ], "source": [ "int(\"2.5\")" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'Hello'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Hello'" ] } ], "source": [ "int(\"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Practice" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [], "source": [ "people = 2\n", "days = 7" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "plane_ticket_pp = 180\n", "hotel_price_day = 85 \n", "meal_pp = 13" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [], "source": [ "total_budget = 1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate main costs" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "total_travel = people * plane_ticket_pp\n", "total_hotel = hotel_price_day * days\n", "total_meal = 3 * days * people\n", "total = total_travel + total_hotel + total_meal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check if they exceeded our budget" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exceeded = total > total_budget\n", "exceeded" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate report" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Estimated travel price is $997 and represents 99.700000% of existing travel budget.\n", "Estimate exceeds travel budget: False.\n" ] } ], "source": [ "report = f'Estimated travel price is ${total:,} and represents {(total / total_budget):%} of existing travel budget.\\nEstimate exceeds travel budget: {exceeded}.'\n", "print(report)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.3" }, "toc-autonumbering": true }, "nbformat": 4, "nbformat_minor": 2 }