{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assigning variables" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print('Hello World')" ] }, { "cell_type": "code", "execution_count": 2, "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": 3, "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": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x, y = 10, 5\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 5, "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": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Philipp\n" ] } ], "source": [ "pi = 'Philipp'\n", "print(pi)\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "x, y = y, x" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 8, "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": 9, "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[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": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ten_millions = 10_000_000\n", "ten_millions" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.25" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "small = .25\n", "small" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000.0" ] }, "execution_count": 12, "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": 13, "metadata": {}, "outputs": [], "source": [ "counter = 0\n", "pricey_car = 'Mercedes'\n", "income = 120_000" ] }, { "cell_type": "code", "execution_count": 14, "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": 15, "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": 16, "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": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(age)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 20, "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": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 // 4 # way faster" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10.0 // 4\n", "2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Remainder**" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Self-assignment" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "count = 0" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count +=1\n", "count" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count -=1\n", "count" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count +=1\n", "count *=2\n", "count" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count **= 2\n", "count" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count /=2\n", "count" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count //= 2\n", "count" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10.0 // 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Order of operations" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.0" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 + 10) / 2" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 / (1 + 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "text1 = 'This is a so-called “speakeasy”'" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "text2 = \"This is Sam’s Tavern\"" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "text3 = ''' This is Sam’s Tavern.\n", "\"Welcome everyone!\" - is written on the door.\n", "'''" ] }, { "cell_type": "code", "execution_count": 48, "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": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello ' + 'World'" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HelloHelloHello'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello' * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Methods" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello World'.upper()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello World'.lower()" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello world'.title()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello planet'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello world'.replace('world', 'planet')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Formatting" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' hello'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello'.rjust(10, ' ')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello '" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello'.ljust(10, ' ')" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0000000999'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'999'.zfill(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Format method**" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello beautiful world and our blue planet'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {} world and our blue {}'.format('beautiful','planet')" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello Beautiful World and our Beautiful Planet!'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{0} {2} {1} and our {2} {3}!'.format('Hello','World','Beautiful', 'Planet')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello beautiful world!'" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello {adj} world!'.format(adj='beautiful')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**F-strings**" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello beautiful world!'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adj = 'beautiful'\n", "f'Hello {adj} world!'" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello, mr. Philipp'" ] }, "execution_count": 62, "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": 63, "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": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Price grew by 34.5% or 45,500'" ] }, "execution_count": 64, "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": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\"[0]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\"[0:5]" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"World\" in \"Hello World!\"" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Planet\" in \"Hello World!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Boolean" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'World' == 'World'" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi == pi" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"World\" in \"Hello World!\"" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pi != pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logical operators\n" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not (5 > 4)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 > 4) | (6 < 5)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 > 4) & (6 < 5)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(5 > 4) ^ (5 < 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conversion" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(\"2.5\")" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(\"45\")" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(4.521)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(5)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(True)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(False)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'True'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(True)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('Hello')" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(115.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Can't convert**" ] }, { "cell_type": "code", "execution_count": 87, "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[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": 88, "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[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": 89, "metadata": {}, "outputs": [], "source": [ "CONST = 32\n", "RATIO = 5/9\n", "\n", "T_f = 100" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "37.77777777777778" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T_c = (T_f - CONST) * RATIO\n", "T_c" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100.0" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T_f2 = (T_c / RATIO) + CONST\n", "T_f2" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21.11111111111111" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T_f = 70\n", "T_c = (T_f - CONST) * RATIO\n", "T_c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "file_extension": ".py", "kernelspec": { "display_name": "Python [conda env:packt] *", "language": "python", "name": "conda-env-packt-py" }, "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.3" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 }, "nbformat": 4, "nbformat_minor": 4 }