From bb3483bb7d49fa1081ec9f2fe1116fcc5c11f93b Mon Sep 17 00:00:00 2001 From: Eylin Date: Fri, 30 Jan 2026 08:46:50 +0100 Subject: [PATCH] Add lab --- __pycache__/functions.cpython-313.pyc | Bin 0 -> 546 bytes functions.py | 32 ++ lab-python-functions.ipynb | 489 +++++++++++++++++++++++++- myfunctions.ipynb | 45 +++ 4 files changed, 550 insertions(+), 16 deletions(-) create mode 100644 __pycache__/functions.cpython-313.pyc create mode 100644 functions.py create mode 100644 myfunctions.ipynb diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c7d50822841638c5d60fdb22bf5f709400dba0be GIT binary patch literal 546 zcmYk2OH0E*5XWbe8XvV{TdfZedA|Wr-DMUpb`-bls>i^Xts5dfW=FX>P0Vt zU%~IAP~y>3Z)(4QGrs5W+y5*x^Pk;bA~6IMhsPIgm(CYXenS5iy?GLMunGsTClC(A zG@x+AJUFl>zG_T3(Jb9cbvZ|k&}ATx=rSYPFY`eiuSa#~A za<8_Lhh^8icEZ@$uOMadHx6hq~ng|f&_#Tq8bgMIuZLK`~qL_7DK z8ox9;@#Ir{rZYTxlfTN}8;`@Yo%FUQA?GM}v}3+ literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..b9d1bb2 --- /dev/null +++ b/functions.py @@ -0,0 +1,32 @@ +lst = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9] + +def get_unique_list_f(lst): + list = [] + for number in lst: + if number not in list: + list.append(number) + return list + +get_unique_list_f(lst) + +string = "Hello Ironhack" + +def count_case_f(string): + upper_count = 0 + lower_count = 0 + for char in string: + if char.isupper(): + upper_count += 1 + elif char.islower(): + lower_count += 1 + + return upper_count, lower_count + +count_case_f(string) + +import string +sentence = "Some say, life is but a dream, Some say it's a test?" +def remove_punctuation_f(sentence): + return sentence.translate(str.maketrans('', '' , string.punctuation)) + +remove_punctuation_f(sentence) \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..0610210 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -31,9 +31,17 @@ "execution_count": null, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block after function definition on line 12 (1398976231.py, line 13)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[1], line 13\u001b[1;36m\u001b[0m\n\u001b[1;33m list = []\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block after function definition on line 12\n" + ] + } + ], "source": [ - "def get_unique_list(lst):\n", " \"\"\"\n", " Takes a list as an argument and returns a new list with unique elements from the first list.\n", "\n", @@ -42,8 +50,46 @@ "\n", " Returns:\n", " list: A new list with unique elements from the input list.\n", - " \"\"\"\n", - " # your code goes here\n" + " \"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "eff12e76", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "lst = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9]\n", + "\n", + "def get_unique_list(lst):\n", + " list = []\n", + " for number in lst:\n", + " if number not in list:\n", + " list.append(number)\n", + " return list" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "954bc4a8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_unique_list(lst)" ] }, { @@ -78,6 +124,47 @@ " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d25833b7", + "metadata": {}, + "outputs": [], + "source": [ + "string = \"Hello Ironhack\"\n", + "def count_case(string):\n", + " upper_count = 0\n", + " lower_count = 0 \n", + " for char in string:\n", + " if char.isupper():\n", + " upper_count += 1\n", + " elif char.islower():\n", + " lower_count += 1\n", + "\n", + " return upper_count, lower_count\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8e737dbe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 11)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_case(string)" + ] + }, { "cell_type": "markdown", "id": "2c902b2e-369c-4a40-a166-ca5bc554cab3", @@ -125,6 +212,74 @@ " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "280b1b2a", + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "sentence = \"Some say, life is but a dream, Some say it's a test?\"\n", + "def remove_punctuation(sentence):\n", + " return sentence.translate(str.maketrans('', '' , string.punctuation))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7753dc50", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Some say life is but a dream Some say its a test'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "remove_punctuation(sentence)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1d977833", + "metadata": {}, + "outputs": [], + "source": [ + "new_sentence = 'Some say life is but a dream Some say its a test'\n", + "def word_count(sentence):\n", + " words = sentence.split()\n", + " return len(words)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "d8529dbd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_count(sentence)" + ] + }, { "cell_type": "markdown", "id": "52814e4d-7631-4c80-80b6-2b206f7e7419", @@ -168,12 +323,198 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def add_1 ( x , y):\n", + " a = x + y\n", + " return a" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c9707153", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add_1(10, 20)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "aeb48c4c", + "metadata": {}, + "outputs": [], + "source": [ + "def sub_1 (x , y):\n", + " s = x - y\n", + " return s" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "068fed35", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sub_1 (20, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "a2a84160", + "metadata": {}, + "outputs": [], + "source": [ + "def mul_1 (x , y):\n", + " m = x*y\n", + " return m" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e39d9c0f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "200" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mul_1 (10 , 20)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0610147b", + "metadata": {}, + "outputs": [], + "source": [ + "def div_1 (x , y):\n", + " d = x/y\n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "b00543b6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "div_1 (20 , 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6a46d9bd", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate (x, y, operator):\n", + " if operator == \"+\":\n", + " a = x + y\n", + " return a\n", + " elif operator == \"-\": \n", + " s = x - y\n", + " return s\n", + " elif operator == \"*\":\n", + " m = x*y\n", + " return m\n", + " else:\n", + " d = x/y\n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3e655d96", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "200" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate(10, 20, \"*\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "8e201846", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate(10, 20, \"+\")" ] }, { @@ -192,12 +533,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def operation_args (operator, *args):\n", + " if operator == \"+\":\n", + " result = 0\n", + " for num in args:\n", + " result += num\n", + " elif operator == \"-\": \n", + " result = 0\n", + " for num in args:\n", + " result -= num\n", + " elif operator == \"*\":\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " elif operator == \"/\":\n", + " result = 1\n", + " for num in args:\n", + " result /= num\n", + " else:\n", + " print(\"Operation not successful\")\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "4ffe47a9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "operation_args(\"*\", *(2, 3, 4))" ] }, { @@ -255,6 +636,38 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 8, + "id": "224838e5", + "metadata": {}, + "outputs": [], + "source": [ + "lst = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "be3554a1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import functions\n", + "functions.get_unique_list_f(lst)\n" + ] + }, { "cell_type": "markdown", "id": "14f222c5-e7bb-4626-b45c-bd735001f768", @@ -273,16 +686,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "import functions\n", + "\n" ] }, { @@ -315,18 +738,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "1\n", + "2\n", + "3\n", + "5\n", + "8\n", + "13\n", + "21\n", + "34\n", + "55\n", + "89\n", + "144\n", + "233\n" + ] + } + ], "source": [ - "# your code goes here" + "n = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n", + "\n", + "def fibonacci(n):\n", + " if n == 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + " else:\n", + " return fibonacci(n -2) + fibonacci (n - 1)\n", + "for x in range (14):\n", + " print(fibonacci(x))\n", + " \n", + "\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +797,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, diff --git a/myfunctions.ipynb b/myfunctions.ipynb new file mode 100644 index 0000000..a8c5d2b --- /dev/null +++ b/myfunctions.ipynb @@ -0,0 +1,45 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "67fbc090", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "lst = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9]\n", + "\n", + "def get_unique_list(lst):\n", + " list = []\n", + " for number in lst:\n", + " if number not in list:\n", + " list.append(number)\n", + " return list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "38b58f20", + "metadata": {}, + "outputs": [], + "source": [ + "get_unique_list(lst)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}