From 2e374094b0e652fb0626f5254d3dfaed7066307d Mon Sep 17 00:00:00 2001 From: geo is a leo Date: Mon, 26 May 2025 15:38:24 +0200 Subject: [PATCH] finally! --- Intro Python - Data types 1.ipynb | 742 +++++++++++++++++++++++++----- 1 file changed, 630 insertions(+), 112 deletions(-) diff --git a/Intro Python - Data types 1.ipynb b/Intro Python - Data types 1.ipynb index b996ab4..daebc74 100644 --- a/Intro Python - Data types 1.ipynb +++ b/Intro Python - Data types 1.ipynb @@ -50,9 +50,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ] + } + ], "source": [ "import this" ] @@ -96,20 +124,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], "source": [ "print (\"Hello World\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Second line\n" + ] + } + ], "source": [ "print (\"Second line\")" ] @@ -139,11 +183,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Now it is a code cell :)\n" + ] + } + ], "source": [ - "#### **This is a cell for coding**" + "#### **This is a cell for coding**\n", + "print(\"Now it is a code cell :)\")" ] }, { @@ -167,12 +220,10 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "scrolled": true }, - "outputs": [], "source": [ "# Header 1\n", "## Header 2\n", @@ -294,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ @@ -310,11 +361,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "whole_number" ] @@ -328,11 +390,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ "print(whole_number)" ] @@ -346,9 +416,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a = 10\n", "b = 20\n", @@ -365,11 +446,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + }, + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a = 10\n", "b = 20\n", @@ -386,9 +485,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(a)" ] @@ -429,7 +539,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -445,9 +555,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12.34\n" + ] + } + ], "source": [ "print(a)" ] @@ -461,16 +579,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(a)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -488,13 +617,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# type(b)" + "# type(b)\n", + "type(b)" ] }, { @@ -516,7 +657,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -526,11 +667,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "10 + 3" ] @@ -544,11 +696,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Sum\n", "a + b" @@ -563,9 +726,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# difference\n", "\n", @@ -581,7 +755,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ @@ -592,9 +766,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(division)" ] @@ -608,9 +793,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# division: floor division: rounded division\n", "\n", @@ -620,9 +816,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(floor_division)" ] @@ -636,9 +843,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Module: Remainder of the division\n", "\n", @@ -654,18 +872,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "b" ] @@ -679,9 +919,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El numero 1 NO es par\n", + "El numero 2 es par\n", + "El numero 3 NO es par\n", + "El numero 4 es par\n", + "El numero 5 NO es par\n", + "El numero 6 es par\n" + ] + } + ], "source": [ "# Even / odd -> modulo (remainder)\n", "# If the remainder of a division by two is zero: even\n", @@ -719,11 +972,68 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x = 88, y = 33)\n", + "Subtraction: 55 (type: )\n", + "Multiplication: 2904 (type: )\n", + "Division: 2.6666666666666665 (type: )\n", + "Floor Division: 2 (type: )\n", + "Modulo: 22\n", + "\n", + "Even/Odd Results:\n", + "1 is odd\n", + "2 is even\n", + "3 is odd\n", + "4 is even\n", + "5 is odd\n", + "6 is even\n", + "7 is odd\n", + "8 is even\n", + "9 is odd\n", + "10 is even\n" + ] + } + ], "source": [ - "# your solution here" + "# assign variables\n", + "x = 88\n", + "y = 33\n", + "# do some math!\n", + "addition = x + y\n", + "subtraction = x - y\n", + "multiplication = x * y\n", + "division = x / y\n", + "floor_division = x // y\n", + "modulo = x % y\n", + "\n", + "# check types of results\n", + "addition_type = type(addition)\n", + "subtraction_type = type(subtraction)\n", + "multiplication_type = type(multiplication)\n", + "division_type = type(division)\n", + "floor_division_type = type(floor_division)\n", + "\n", + "# check if even or odd\n", + "even_odd_results = []\n", + "for num in range(1, 11):\n", + " even_odd_results.append((num, \"even\" if num % 2 == 0 else \"odd\"))\n", + "\n", + "# print results for verification\n", + "print(f\"x = {x}, y = {y})\")\n", + "print(f\"Subtraction: {subtraction} (type: {subtraction_type})\")\n", + "print(f\"Multiplication: {multiplication} (type: {multiplication_type})\")\n", + "print(f\"Division: {division} (type: {division_type})\")\n", + "print(f\"Floor Division: {floor_division} (type: {floor_division_type})\")\n", + "print(f\"Modulo: {modulo}\")\n", + "print(\"\\nEven/Odd Results:\")\n", + "for num, result in even_odd_results:\n", + " print(f\"{num} is {result}\")\n" ] }, { @@ -745,9 +1055,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is built-in\n" + ] + } + ], "source": [ "# First, let's explore a built-in method: print\n", "# The print method allows us to print messages to the console.\n", @@ -756,9 +1074,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'THIS IS A STRING'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Another example of a built-in method is upper.\n", "# This method converts a string of text to uppercase.\n", @@ -767,7 +1096,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, "outputs": [], "source": [ @@ -778,9 +1107,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "8789" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Next, we use a function from the math library: floor.\n", "# The floor function rounds a number down to the nearest integer.\n", @@ -802,31 +1142,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a string'" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\"This is a string\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a string with simple quotes'" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "'This is a string with simple quotes'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'This is a string with simple quotes'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\"This is a string with simple quotes\" #End Of Line" ] @@ -840,9 +1213,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'4'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "this_is_also_a_string = \"4\"\n", "this_is_also_a_string" @@ -857,9 +1241,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(this_is_also_a_string)" ] @@ -876,10 +1271,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "\"This is a string\n", - "with multiple lines\"" - ] + "source": [] }, { "cell_type": "markdown", @@ -890,9 +1282,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nThis is a string\\nwith multiple liness'" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\"\"\"\n", "This is a string\n", @@ -908,9 +1311,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "This is a string\n", + "with multiple liness\n" + ] + } + ], "source": [ "print(\"\"\"\n", "This is a string\n", @@ -927,16 +1340,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'😍'" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\"😍\" #emojis -> They are strings" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, "outputs": [], "source": [ @@ -945,18 +1369,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'😍'" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "heart_face" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(heart_face)" ] @@ -991,7 +1437,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, "outputs": [], "source": [ @@ -1001,9 +1447,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.5\n" + ] + } + ], "source": [ "c = a + b # Python automatically converts a to float to perform the operation\n", "print(c) # The result, 9.5, is a float" @@ -1011,9 +1465,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, - "outputs": [], + "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[76]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# But, if we try to perform an operation between a string and an integer:\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m d = \u001b[33;43m\"\u001b[39;49m\u001b[33;43mHola\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m \u001b[38;5;66;03m# This will cause a TypeError\u001b[39;00m\n", + "\u001b[31mTypeError\u001b[39m: can only concatenate str (not \"int\") to str" + ] + } + ], "source": [ "# But, if we try to perform an operation between a string and an integer:\n", "d = \"Hola\" + a # This will cause a TypeError" @@ -1140,21 +1606,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My age is 55\n" + ] + } + ], "source": [ "# Step 1: Create a variable age with your age as an integer\n", - "age = ...\n", + "age = 55\n", "\n", "# Step 2: Convert the variable age to a string\n", - "age = ...\n", + "age_str = str(age)\n", "\n", "# Step 3: Concatenate \"My age is: \" with the variable age and store the result in a new variable called message\n", - "message = ...\n", + "message = \"My age is \" + age_str\n", "\n", "# Step 4: Print the variable message\n", - "print(...)" + "print(message)" ] }, { @@ -1178,9 +1652,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "''" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "salutation = input()\n", "salutation" @@ -1195,7 +1680,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ @@ -1479,11 +1964,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, Geo Smith! You are 43 years old.\n", + "Hello, Geo Smith! You are 43 years old.\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "name = input(\"Enter your name: \")\n", + "age = int(input(\"Enter your age: \"))\n", + "\n", + "# String formatting using f-string - hope this is right\n", + "greeting_fstring = f\"Hello, {name}! You are {age} years old.\"\n", + "\n", + "# String formatting using .format() method\n", + "greeting_format = \"Hello, {}! You are {} years old.\".format(name, age)\n", + "\n", + "# Printing both messages\n", + "print(greeting_fstring)\n", + "print(greeting_format)" ] }, { @@ -1765,9 +2271,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'sentence' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[81]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43msentence\u001b[49m.split(\u001b[33m\"\u001b[39m\u001b[33me\u001b[39m\u001b[33m\"\u001b[39m)\n", + "\u001b[31mNameError\u001b[39m: name 'sentence' is not defined" + ] + } + ], "source": [ "sentence.split(\"e\")" ] @@ -1775,7 +2293,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -1789,7 +2307,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.3" }, "nbTranslate": { "displayLangs": [