diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f3bde01..618ee41 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -28,7 +28,14 @@ "id": "630d7e4d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import random as rd\n", + "import math as m\n", + "import subprocess\n", + "import sys" + ] }, { "cell_type": "markdown", @@ -46,6 +53,20 @@ "metadata": {}, "source": [] }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc0aa254", + "metadata": {}, + "outputs": [], + "source": [ + "square = []\n", + "\n", + "square = [i**2 for i in range(1, 21)]\n", + "\n", + "print(square)" + ] + }, { "cell_type": "markdown", "id": "b421e0cb", @@ -63,7 +84,13 @@ "id": "c047115a", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "square = []\n", + "\n", + "square = [i**2 for i in range(1, 101)]\n", + "\n", + "print(square)" + ] }, { "cell_type": "markdown", @@ -81,7 +108,12 @@ "id": "e23d87ea", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "my_list = []\n", + "\n", + "my_list = [i for i in range(-10, 1)]\n", + "print(my_list)\n" + ] }, { "cell_type": "markdown", @@ -99,7 +131,12 @@ "id": "d8d1c865", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "odds = []\n", + "\n", + "odds = [i for i in range(1, 101, 2)]\n", + "print(odds)" + ] }, { "cell_type": "markdown", @@ -116,7 +153,11 @@ "id": "6a9cfb8c", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "divisible_by_seven = []\n", + "odds = [i for i in range(7, 1000, 7)]\n", + "print(odds)\n" + ] }, { "cell_type": "markdown", @@ -134,19 +175,14 @@ "metadata": {}, "outputs": [], "source": [ - "# You can use the following test string but feel free to modify at your convenience\n", - "\n", - "teststring = 'Find all of the words in a string that are monosyllabic'" + "non_vowels = ['B', 'b', 'C', 'c', 'D', 'd', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z','z']\n", + "teststring = 'Find all of the words in a string that are monosyllabic'\n", + "non_vowel_list = [char for char in teststring if char in non_vowels]\n", + "new_string = ''.join(non_vowel_list)\n", + "print(new_string)\n", + "\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "d5fc7426", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "277d491e", @@ -164,7 +200,13 @@ "id": "3a43399c", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "capital_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']\n", + "teststring_1 = 'The Quick Brown Fox Jumped Over The Lazy Dog'\n", + "capital_letters = [char for char in teststring_1 if char in capital_letters]\n", + "new_string_1 = ''.join(capital_letters)\n", + "print(new_string_1)" + ] }, { "cell_type": "markdown", @@ -183,7 +225,13 @@ "id": "3ddb68df", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "consonants = ['B', 'b', 'C', 'c', 'D', 'd', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z','z''b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']\n", + "teststring_2 = 'The Quick Brown Fox Jumped Over The Lazy Dog'\n", + "consonants = [char for char in teststring_1 if char in consonants]\n", + "new_string_2 = ''.join(consonants)\n", + "print(new_string_2)" + ] }, { "cell_type": "markdown", @@ -205,7 +253,21 @@ "id": "22bb13eb", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1a7c5d0", + "metadata": {}, + "outputs": [], + "source": [ + "directory_path = 'C:/Users/marti/OneDrive/Escritorio/ironhack/lectures'\n", + "lectures_folders = [f for f in os.listdir(directory_path) if os.path.isdir(os.path.join(directory_path, f))]\n", + "lectures_folders" + ] }, { "cell_type": "markdown", @@ -218,13 +280,32 @@ "Remember to use list comprehensions and to print your results" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4622e62", + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, { "cell_type": "code", "execution_count": null, "id": "120b29bd", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "random_list_1 = random.sample(range(1, 101), 10)\n", + "print(\"Random_list_1 = \", random_list_1)\n", + "random_list_2 = random.sample(range(1, 101), 10)\n", + "print(\"Random_list_2 = \",random_list_2)\n", + "random_list_3 = random.sample(range(1, 101), 10)\n", + "print(\"Random_list_3 = \",random_list_3)\n", + "random_list_4 = random.sample(range(1, 101), 10)\n", + "print(\"Random_list_4 = \",random_list_4)" + ] }, { "cell_type": "markdown", @@ -252,7 +333,10 @@ "id": "b6bcef37", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "flatten_list = [item for sublist in list_of_lists for item in sublist]\n", + "flatten_list" + ] }, { "cell_type": "markdown", @@ -283,7 +367,10 @@ "id": "a932aff5", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "floats = [int(item) for sublist in list_of_lists for item in sublist]\n", + "floats" + ] }, { "cell_type": "markdown", @@ -300,18 +387,13 @@ "metadata": {}, "outputs": [], "source": [ - "for i in ['a','b','c']:\n", - " print i**2" + "try:\n", + " for i in ['a','b','c']:\n", + " print (i**2)\n", + "except:\n", + " print(\"TypeError\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "3605fc78", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "bda76246", @@ -333,18 +415,13 @@ "source": [ "x = 5\n", "y = 0\n", - "\n", - "z = x/y" + "try:\n", + " z = x/y\n", + " print(z)\n", + "except:\n", + " print(\"ZeroDivisionError\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "976fec9f", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "85ea78c5", @@ -362,18 +439,13 @@ "metadata": {}, "outputs": [], "source": [ - "abc=[10,20,20]\n", - "print(abc[3])" + "try:\n", + " abc=[10,20,20]\n", + " print(abc[2])\n", + "except:\n", + " print(\"IndexError\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "cfcf15f0", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "10223640", @@ -392,7 +464,20 @@ "id": "3c1f141b", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "a = 5\n", + "b = 10\n", + "\n", + "try: \n", + " result = a / b\n", + " print(result)\n", + "except ZeroDivisionError:\n", + " print(\"ZeroDivisionError\")\n", + "except TypeError:\n", + " print(\"Type Error\")\n", + "except Exception as e:\n", + " print(f\"An unexpected error occurred: {e}\")" + ] }, { "cell_type": "markdown", @@ -406,35 +491,18 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "b73f824c", "metadata": {}, - "outputs": [ - { - "ename": "FileNotFoundError", - "evalue": "[Errno 2] No such file or directory: 'testfile'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mFileNotFoundError\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[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'testfile'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Test write this'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'testfile'" - ] - } - ], + "outputs": [], "source": [ - "f = open('testfile','r')\n", - "f.write('Test write this')" + "try:\n", + " f = open('testfile','r')\n", + " f.write('Test write this')\n", + "except: \n", + " print(\"FileNotFoundError\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "5a1cb361", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "a028ccb8", @@ -447,23 +515,20 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "5f639ab1", "metadata": {}, - "outputs": [ - { - "ename": "IndentationError", - "evalue": "unexpected indent (, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m line = f.readline()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" - ] - } - ], + "outputs": [], "source": [ - "fp = open('myfile.txt')\n", - " line = f.readline()\n", - " i = int(s.strip())\n" + "try: \n", + " try: \n", + " fp = open('myfile.txt')\n", + " line = f.readline()\n", + " i = int(s.strip())\n", + " except FileNotFoundError:\n", + " print(\"FileNotFoundError\")\n", + "except TypeError:\n", + " print(\"TypeError\")" ] }, { @@ -481,23 +546,20 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, + "id": "1d959aa0", + "metadata": {}, + "outputs": [], + "source": [ + "import sys" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "3f043ef6", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'sys' 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[1;32m 2\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'linux'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplatform\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Function can only run on Linux systems.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Doing something.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mlinux_interaction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mlinux_interaction\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mlinux_interaction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'linux'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplatform\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Function can only run on Linux systems.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Doing something.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mlinux_interaction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name 'sys' is not defined" - ] - } - ], + "outputs": [], "source": [ "def linux_interaction():\n", " assert ('linux' in sys.platform), \"Function can only run on Linux systems.\"\n", @@ -511,7 +573,23 @@ "id": "1643bbed", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def linux_interaction():\n", + " try: \n", + " assert ('linux' in sys.platform)\n", + " except AssertionError:\n", + " return \"AssertionError: Function can only run on Linux systems.\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6a1abae", + "metadata": {}, + "outputs": [], + "source": [ + "linux_interaction()" + ] }, { "cell_type": "markdown", @@ -548,7 +626,28 @@ "id": "94c2e76e", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def square_number(x):\n", + " while True:\n", + " try:\n", + " x = int(x)\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + " else:\n", + " result = x**2\n", + " print(f\"The square of {x} is:\", result)\n", + " break " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "80d0cc9f", + "metadata": {}, + "outputs": [], + "source": [ + "square_number(5)" + ] }, { "cell_type": "markdown", @@ -567,7 +666,12 @@ "id": "90d3aa37", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "results = []\n", + "\n", + "res_1000 = [i for i in range(1, 1001) if any(i % digit == 0 for digit in range(2, 10))]\n", + "print(res_1000)" + ] }, { "cell_type": "markdown", @@ -583,7 +687,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 13, "id": "86fa19bf", "metadata": {}, "outputs": [ @@ -591,33 +695,55 @@ "name": "stdout", "output_type": "stream", "text": [ - "Enter Total Marks Scored: ass\n" - ] - }, - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: 'ass'", - "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[0mTotal_Marks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter Total Marks Scored: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mNum_of_Sections\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter Num of Sections: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'ass'" + "Enter Total Marks Scored: 5\n", + "Enter Num of Sections: l\n" ] } ], "source": [ - "Total_Marks = int(input(\"Enter Total Marks Scored: \")) \n", - "Num_of_Sections = int(input(\"Enter Num of Sections: \"))" + "Total_Marks = input(\"Enter Total Marks Scored: \")\n", + "Num_of_Sections = input(\"Enter Num of Sections: \")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "f80bb764", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "class custom_error(Exception):\n", + " def __init__(self, message=\"A custom error occurred.\"):\n", + " self.message = message\n", + " super().__init__(self.message)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "c43beaf0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid number.\n" + ] + } + ], + "source": [ + "try:\n", + " Num_of_Sections = int(Num_of_Sections)\n", + " if Num_of_Sections < 2:\n", + " raise custom_error()\n", + "except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "except custom_error as e:\n", + " print(f\"Error: {e}\")\n", + "else:\n", + " print(f\"You entered Num of Sections: {Num_of_Sections}\")" + ] } ], "metadata": { @@ -636,7 +762,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1,