diff --git a/04_this_cohort/live-code/03_09_2026.ipynb b/04_this_cohort/live-code/03_09_2026.ipynb new file mode 100644 index 000000000..c443c4cac --- /dev/null +++ b/04_this_cohort/live-code/03_09_2026.ipynb @@ -0,0 +1,2218 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "id": "c8b8023e", + "metadata": {}, + "outputs": [], + "source": [ + "def sumTwoNumbers(a, b):\n", + " sum = a + b\n", + " return sum" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "675cd834", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumTwoNumbers(3 , 4)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "47ffc857", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abs(-10)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e6b70980", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 > 10" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "44ba56a4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a string'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# string, text data type \n", + "\n", + "'This is a string'" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "6bead73f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This is also a string'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"This is also a string\"" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "97c10af3", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 1) (1531528915.py, line 1)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m'This is not a valid string\"\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m unterminated string literal (detected at line 1)\n" + ] + } + ], + "source": [ + "'This is not a valid string\"" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "159d9cd7", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "unterminated string literal (detected at line 1) (3205553238.py, line 1)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[12]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m'Let's see if this works'\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m unterminated string literal (detected at line 1)\n" + ] + } + ], + "source": [ + "'Let's see if this works'" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f5f4aeab", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Let's see if this works\"" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Let's see if this works\"" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "e417b52d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This \\nis \\na \\nmultiline \\nstring'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''This \n", + "is \n", + "a \n", + "multiline \n", + "string'''" + ] + }, + { + "cell_type": "markdown", + "id": "4d4c2bd4", + "metadata": {}, + "source": [ + "### Escape sequences\n", + "\n", + "| Escape sequence | Description |\n", + "|-----------------|--------------------|\n", + "| \\\\' | Single quote |\n", + "| \\\\\" | Double quote |\n", + "| \\\\\\\\ | Backslash |\n", + "| \\\\t | Tab |\n", + "| \\\\n | Newline |\n", + "| \\\\r | Carriage return |" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "116e75ae", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Let's see if this works\"" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Let\\'s see if this works'" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "0c074bc6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "She was fine with apples\\oranges\n" + ] + } + ], + "source": [ + "print('She was fine with apples\\oranges')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "05670447", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a \ttab\n" + ] + } + ], + "source": [ + "print('This is a \\ttab')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3370e70", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5674\n" + ] + } + ], + "source": [ + "# \\r move the cursor back to the beginning of the current line\n", + "\n", + "print('1234\\r567')" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "b8097580", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "abcdefg\n" + ] + } + ], + "source": [ + "print('abcdefg')" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "e6eab22a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gbcdef\n" + ] + } + ], + "source": [ + "print('abcdef\\rg')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f701ecc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 + 3 # int, whole numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "fb4f3671", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.3333333333333335" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "10 / 3 " + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "1758efd6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hellomynameiskaylie'" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'hello' + 'my' + 'name' + 'is' + 'kaylie'" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "6732f945", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hahaha'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'ha' * 3" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "8a32488d", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate str (not \"int\") to str", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[40]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[33;43m'\u001b[39;49m\u001b[33;43mThe year is \u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m2020\u001b[39;49m\n", + "\u001b[31mTypeError\u001b[39m: can only concatenate str (not \"int\") to str" + ] + } + ], + "source": [ + "'The year is ' + 2020" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "c55d8678", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'The year is 2020'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'The year is ' + '2020'" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "023692c9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'apple' == 'Apple'" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "41459c7f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'apple' != 'Apple'" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "aa0da1ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "loooo\n" + ] + } + ], + "source": [ + "print('hel \\rloooo')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cd422ed", + "metadata": {}, + "outputs": [], + "source": [ + "loooo" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "8d165181", + "metadata": {}, + "outputs": [], + "source": [ + "first_name = 'Ada'\n", + "last_name = 'Lovelace'" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "fd41b5b8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Ada'" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "4678f4f4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Lovelace'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "6c1a1636", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "7fc45fcb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'vel'" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[2:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "d9d5a6b1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ove'" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[1:4]" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "7ffd54bd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'lace'" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[4:8]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "906af842", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'lace'" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[4:]" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "c9da91b1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Love'" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[:4]" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "8eea8427", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# slice the third-to-last character \n", + "last_name[-3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "796f245f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ace'" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# slice from the third-to-last character to the end\n", + "last_name[-3:]" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "e6c86848", + "metadata": {}, + "outputs": [], + "source": [ + "phone_number = '+1 555-123-4567'" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "c2bcfee9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'+1 555-123-4567'" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phone_number" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "ec771fa3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'555'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phone_number[3:6]" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "4fa3e573", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 fish 2 fish\n" + ] + } + ], + "source": [ + "print(1, \"fish\", 2, \"fish\")" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "349bbbee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "87" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len('We have a very, very, very long sentence and need to know how many characters there are')" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "be48c4f2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'banana'" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fruit = 'banana'\n", + "fruit" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "900819bd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'a', 'a', 'b', 'n', 'n']" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted(fruit)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "21a42dd9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'I AM NOT YELLING'" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# methods \n", + "# function that works only for a specific type of data\n", + "# e.g., string methods work only on strings\n", + "# methods are called differently from other functions\n", + "\n", + "'I am not yelling'.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "41384639", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'This string is unusual'.count('s')" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "ccc08fac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'file_name.csv'.endswith('.csv')" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "73faafc0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'long_file_name_with_space.csv'" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'long file name with space.csv'.replace(' ', '_')" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "0283c389", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Ada'" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "e3dd2b2b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Lovelace'" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "575f8468", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_name[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "ba4d5331", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'L'" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_name[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "7af266c4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Ada Lovelace's initials are A. L.\"" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# format() \n", + "\n", + "'Ada Lovelace\\'s initials are {}. {}.'.format(first_name[0], last_name[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "01afdb05", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Kaylie's favourite vegetable is carrot\"" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Kaylie's favourite vegetable is {}\".format('carrot')" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "feb83384", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Ada Lovelace's initials are A. L.\"" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# f-string\n", + "\n", + "f'Ada Lovelace\\'s initials are {first_name[0]}. {last_name[0]}.'" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "20c3c660", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Kaylie's favourite vegetable is carrot\"" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f\"Kaylie's favourite vegetable is {'carrot'}\"" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "dbafa96e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function round in module builtins:\n", + "\n", + "round(number, ndigits=None)\n", + " Round a number to a given precision in decimal digits.\n", + " \n", + " The return value is an integer if ndigits is omitted or None. Otherwise\n", + " the return value has the same type as the number. ndigits may be negative.\n", + "\n" + ] + } + ], + "source": [ + "help(round)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "5150b562", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on method_descriptor:\n", + "\n", + "lower(self, /) unbound builtins.str method\n", + " Return a copy of the string converted to lowercase.\n", + "\n" + ] + } + ], + "source": [ + "help(str.lower)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "33ed9c2e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'all caps'" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'ALL CAPS'.lower()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8eeafa7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# float -> int\n", + "int(17.4)" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "a93cfd8a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(15.9)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "6c8b0eca", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# string -> int\n", + "int('18')" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "3cbbfb73", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'me'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[98]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mme\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: 'me'" + ] + } + ], + "source": [ + "int('me')" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "112ecc3f", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: '1.74'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[99]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m1.74\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: '1.74'" + ] + } + ], + "source": [ + "int('1.74')" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "90467956", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "892.0" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# string -> float\n", + "\n", + "float('892') " + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "id": "bb8cf634", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "could not convert string to float: 'you'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[101]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43myou\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[31mValueError\u001b[39m: could not convert string to float: 'you'" + ] + } + ], + "source": [ + "float('you')" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "1b287393", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'37.5'" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# float -> string\n", + "\n", + "str(37.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "7e6bc672", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'20'" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# int -> string\n", + "\n", + "str(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "ebad40aa", + "metadata": {}, + "outputs": [], + "source": [ + "age = input('How old are you? ')" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "id": "65e11c3f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1000'" + ] + }, + "execution_count": 106, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "age" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "12420d82", + "metadata": {}, + "outputs": [], + "source": [ + "colour = input(\"What is your favourite colour? \")" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "880cc30a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Red'" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "53404147", + "metadata": {}, + "outputs": [], + "source": [ + "animal = input(\"What is your favourite animal? \")" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "80f35c09", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "''" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "animal" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "8d1c9bdc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1000'" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "age" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31c28e7a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1001" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "age_next_bday = int(age) + 1 # 1000 + 1\n", + "age_next_bday" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59d8fe01", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate str (not \"int\") to str", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[112]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m age_next_bday = \u001b[43mage\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m1\u001b[39;49m\n\u001b[32m 2\u001b[39m age_next_bday\n", + "\u001b[31mTypeError\u001b[39m: can only concatenate str (not \"int\") to str" + ] + } + ], + "source": [ + "age_next_bday = age + 1 # '1000' + 1\n", + "age_next_bday" + ] + }, + { + "cell_type": "markdown", + "id": "e359420c", + "metadata": {}, + "source": [ + "## `not`\n", + "\n", + "|X|`not` X|\n", + "|-|-|\n", + "|True|False|\n", + "|False|True|" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "5721d429", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 123, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# not \n", + "# negate the truth value of the statement\n", + "\n", + "not True" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "34cd8c44", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 124, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not False" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "id": "5c4e350e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 117, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3 == 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a3dbd7de", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not (3 == 3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39181142", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 127, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not (3 == 3)" + ] + }, + { + "cell_type": "markdown", + "id": "4afd93a0", + "metadata": {}, + "source": [ + "## `and`\n", + "\n", + "Evaluates to `True` if both statements are true.\n", + "\n", + "|X|Y|X `and` Y|\n", + "|-|-|-|\n", + "|True|True|True|\n", + "|False|True|False|\n", + "|True|False|False|\n", + "|False|False|False|" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "59eda2f3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# and: check if the statements on both sides are true\n", + "\n", + "True and False" + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "id": "0196893a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 131, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(7 == 7)" + ] + }, + { + "cell_type": "code", + "execution_count": 132, + "id": "99b4d635", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 132, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(9 > 32)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9cb71e64", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 134, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(7 == 7) and (9 > 32) # True and False" + ] + }, + { + "cell_type": "code", + "execution_count": 136, + "id": "76cb9b54", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 136, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(4 == 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 137, + "id": "3439af4d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 137, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(5 > 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 135, + "id": "30d638db", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 135, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(4 == 2) and (5 > 6) # False and False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9a615cf6", + "metadata": {}, + "outputs": [], + "source": [ + "# behaviour of the `and` operator is that it will return the first\n", + "# falsy value encountered or the last truthy value if all operands \n", + "# are true based on the conditions provided" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cebc717e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_winter = \"True\" # string, non-empty and is considered `True`\n", + "is_cloudy = True # boolean\n", + "\n", + "is_winter and is_cloudy # \"True\" and True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f12fccc8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'True'" + ] + }, + "execution_count": 139, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_cloudy and is_winter # True and \"True\"" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "id": "3002f55b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "''" + ] + }, + "execution_count": 140, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\" and False #empty string and boolean False" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "id": "f09120a4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "False and \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aefdd479", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0 and 1 # False and True" + ] + }, + { + "cell_type": "markdown", + "id": "5469fba2", + "metadata": {}, + "source": [ + "## To avoid confusion and ensure consistent behaviour, it's recommended to use boolean values (`True` or `False`) explicitly when working with logical operations to ensure predictable outcomes based on boolean logic." + ] + }, + { + "cell_type": "markdown", + "id": "eff29187", + "metadata": {}, + "source": [ + "## `or`\n", + "\n", + "Evaluates to `True` if just one of the statements is true.\n", + "\n", + "|X|Y|X `or` Y|\n", + "|-|-|-|\n", + "|True|True|True|\n", + "|False|True|True|\n", + "|True|False|True|\n", + "|False|False|False|" + ] + }, + { + "cell_type": "code", + "execution_count": 145, + "id": "29d6e9e1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 145, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# or: check if at least one of the statements are true\n", + "\n", + "(4 == 4) or False # True or False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "41ae10b4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 146, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "('Python' == 'python') or True # False or True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed2fd28b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not (7 % 2 == 1) or False # False or False" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "id": "f68ce0da", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 152, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# or operator return the first truthy value encountered or the last falsy\n", + "# value if all operands are `False` based on the conditions provided\n", + "\n", + "True or \"True\"" + ] + }, + { + "cell_type": "code", + "execution_count": 153, + "id": "140fd7e0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'True'" + ] + }, + "execution_count": 153, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"True\" or True" + ] + }, + { + "cell_type": "code", + "execution_count": 154, + "id": "3ee1b166", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0 or 0" + ] + }, + { + "cell_type": "code", + "execution_count": 155, + "id": "fb97159e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "''" + ] + }, + "execution_count": 155, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0 or ''" + ] + }, + { + "cell_type": "markdown", + "id": "dac0230d", + "metadata": {}, + "source": [ + "## Operator precedence\n", + "\n", + "Boolean operators are evaluated after arithmetic and comparison operators.\n", + "\n", + "| Order | Operator | Description |\n", + "|---|---|---|\n", + "| 1 | `**` | Exponentiation |\n", + "| 2 | `-`| Negation |\n", + "| 3 | `*`, `/`, `//`, `%` | Multiplication, division, integer division, and modulo |\n", + "| 4 | `+`, `-` | Addition and subtraction |\n", + "| 5 | `<`, `<=`, `>`, `>=`, `==`, `!=` | Less than, less than or equal to, greater than, greater than or equal to, equal, not equal |\n", + "| 6 | `not` | Not |\n", + "| 7 | `and` | And |\n", + "| 8 | `or` | Or|" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d4da1e1", + "metadata": {}, + "outputs": [], + "source": [ + "year = 2026\n", + "\n", + "if year >= 2000:\n", + " print(\"We are in the 21st century.\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}