From bcc5caf663e3bc16302bc23782b440916029b760 Mon Sep 17 00:00:00 2001 From: Olga Galanina Date: Thu, 29 Jan 2026 21:34:58 +0100 Subject: [PATCH] Week1Lab_Functions_Extended_Done --- __pycache__/functions.cpython-313.pyc | Bin 0 -> 1760 bytes functions.py | 56 +++++ lab-python-functions.ipynb | 287 +++++++++++++++++++++++--- 3 files changed, 315 insertions(+), 28 deletions(-) create mode 100644 __pycache__/functions.cpython-313.pyc create mode 100644 functions.py diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..44da3c0debb18bb859757322c74bbeecc10b1e3e GIT binary patch literal 1760 zcmaJ>TW=dh82$F*^<~$s?WA$ihOlldl?4R^Y70t3o=RI9Vjz}9q^@dh>`Ah)-Zitk zX#+2dKq6@$f)J7N$ZLNEe<6_@nr@$}^5Qoq`2&2jj-3k;Bky?ToAb@(oHKKnPNx9N z$X`F2RT1C~>-0+OsByTBjZG-SWuWi@l)0}t%IRTJ-V7>~6NpUdDMa<7RE|*c zF={EMC2L1}k4}JR2LZpsFRzWV>mg|MGZ=2uyq-vSi5lxRYT(-EXs-aPv)GIEbuD)cM zwx@q-liSo>A^L5n<~NA#x%%hSv6qe7cdA8P*RfswuIVlN3%X@2Of*-$WyhXa^zE8w z;@AXv>`_DS_up@>hS??JRZr}ox=53_Iy;m*fmHx|xxrxORc2sK+>p>^3q4ZFUlxK2^0Dy=~^FQ=~IwMEdX8K!b5M zhaVpie+!8hD#CeG-#|1g?{xpbh%-^j`~)s)bF*(_Pncu#o+)SZB0+C5>5g*w*-yK| z&F8N3MbLy&n@vGwtTaB1o34*CQr57XM}%r(D7hXr?Il!{jZ|xnZ=<}?=utr)%k>(eO1)`vUF=#!bf zbaq2u*Eb$MeRv$)PeL}|QJ60B__jDs+136*xkoI^QNMAhRWDGsPU(CRzQUod2$2Bj zVU&V@fYEN5W9ptM{d+M2FrPaXpyz&#FNK_{@pKRqL%GRp=^M%hVtx4QGW^Ph=uvV~8s?b%yv0>x8!(4om9PL(^c_j*eZE!G@2Gs3x$Gu*Kz7<-B?A0c0O01;NqK5 zf&a6o%{}Owu+o1ii$GZv?G$sHmg(VxKx`yv6oX;xkwfdQ%ZTn*H2(Jdj>PELi*{{Ssn7)z@zRY(0m811ZH7JHwC|3dEPmOTX1y z$t!_`2nIZqfV*%YN?fj!fy8x0qziu*Tj^^-iXpyz4%vf*zzyxEATzpkzjg6ips)dJ J7Y3q@{tLt9b&UW3 literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..65b9a41 --- /dev/null +++ b/functions.py @@ -0,0 +1,56 @@ +def get_unique_list_f(lst): + unique_list = [] + for item in lst: + if item not in unique_list: + unique_list.append(item) + return unique_list + + +numbers = [1, 2, 3, 3, 3, 3, 4, 5] + +result = get_unique_list_f(numbers) + +print(result) +print(type(result)) + + +def count_case_f(string): + upper_count = 0 + lower_count = 0 + + for char in string: + if char.isupper(): + upper_count += 1 #upper_count = upper_count + 1 + elif char.islower(): + lower_count += 1 + + return upper_count, lower_count + +text = "Hello World!" +result = count_case_f(text) +print(result) + + +import string + +def remove_punctuation_f(sentence): + result = " " + for char in sentence: + if char not in string.punctuation: + result += char + + return result + +text = "Note : this is an example !!! Good day : )" +clean_text = remove_punctuation_f(text) +print(clean_text) + + + +def word_count_f(sentence): + words = sentence.split() + + return len(words) + +words_number = word_count_f(clean_text) +print(words_number) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..cc5a727 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,22 +28,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "\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", + " unique_list = []\n", + " for item in lst:\n", + " if item not in unique_list:\n", + " unique_list.append(item)\n", + " return unique_list\n", "\n", - " Parameters:\n", - " lst (list): The input list.\n", "\n", - " Returns:\n", - " list: A new list with unique elements from the input list.\n", - " \"\"\"\n", - " # your code goes here\n" + "numbers = [1, 2, 3, 3, 3, 3, 4, 5]\n", + "\n", + "result = get_unique_list(numbers)\n", + "\n", + "print(result)\n", + "print(type(result))\n", + "\n" ] }, { @@ -65,7 +78,8 @@ "metadata": {}, "outputs": [], "source": [ - "def count_case(string):\n", + "\n", + " \"\"\"def count_case(string):\n", " \"\"\"\n", " Returns the number of uppercase and lowercase letters in the given string.\n", "\n", @@ -74,10 +88,41 @@ "\n", " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", - " \"\"\"\n", " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": 20, + "id": "ed6f446a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 8)\n" + ] + } + ], + "source": [ + "def count_case(string):\n", + " upper_count = 0\n", + " lower_count = 0\n", + "\n", + " for char in string:\n", + " if char.isupper():\n", + " upper_count += 1 #upper_count = upper_count + 1\n", + " elif char.islower():\n", + " lower_count += 1\n", + "\n", + " return upper_count, lower_count\n", + "\n", + "text = \"Hello World!\"\n", + "result = count_case(text)\n", + "print(result)\n" + ] + }, { "cell_type": "markdown", "id": "2c902b2e-369c-4a40-a166-ca5bc554cab3", @@ -125,6 +170,61 @@ " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": 25, + "id": "7fcf553a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Note this is an example Good day \n" + ] + } + ], + "source": [ + "import string\n", + "\n", + "def remove_punctuation(sentence):\n", + " result = \" \"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " result += char\n", + "\n", + " return result \n", + "\n", + "text = \"Note : this is an example !!! Good day : )\"\n", + "clean_text = remove_punctuation(text)\n", + "print(clean_text)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "6e09ff45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], + "source": [ + "def word_count(sentence):\n", + " \n", + " words = sentence.split()\n", + " \n", + " return len(words)\n", + "\n", + "words_number = word_count(clean_text)\n", + "print(words_number)\n" + ] + }, { "cell_type": "markdown", "id": "52814e4d-7631-4c80-80b6-2b206f7e7419", @@ -138,16 +238,6 @@ "*would give you as expected output: 7*" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, { "cell_type": "markdown", "id": "fb01ccba-2ecc-4eea-b9a9-2bf9d29e8804", @@ -173,9 +263,79 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def subtraction(x, y):\n", + " result = x - y\n", + " return result\n", + "\n", + "print(subtraction(10, 3)) # Output: 7\n", + "\n", + "\n", + "def addition(x, y):\n", + " result = x + y\n", + " return result\n", + "print(addition(10, 3))\n", + "\n", + "\n", + "def multiplication(x, y):\n", + " result = x * y \n", + " return result\n", + "print(multiplication(12, 23))\n", + "\n", + "def division(x, y):\n", + " result = x / y\n", + " return result \n", + "print(division(12, 6))\n" ] }, + { + "cell_type": "code", + "execution_count": 50, + "id": "6734cc3c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Result: 87\n" + ] + } + ], + "source": [ + "# Define another function called \"calculate\" that takes three arguments: two operands and an operator.\n", + "#- The \"calculate\" function should use a conditional statement to determine which arithmetic function to call based on the operator argument.\n", + "#- The \"calculate\" function should then call the appropriate arithmetic function and return the result.\n", + "\n", + "def calculate(x, y):\n", + " operation = input(\"Enter operation (addition, subtraction, multiplication, division): \").strip().lower()\n", + "\n", + " if operation == \"subtraction\":\n", + " result = x - y\n", + " elif operation == \"addition\":\n", + " result = x + y\n", + " elif operation == \"division\":\n", + " result = x / y\n", + " elif operation == \"multiplication\":\n", + " result = x * y\n", + " else:\n", + " return \"Invalid operation entered!\"\n", + "\n", + " return result\n", + "\n", + "x = int(input(\"Enter the number1\"))\n", + "y = int(input(\"Enter the number2\"))\n", + "result = calculate(x, y)\n", + "print(\"Result:\", result)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "734f3e23", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "d1784c63-208c-4553-9686-acac6c0a9dda", @@ -192,12 +352,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Result: 32\n" + ] + } + ], "source": [ - "# your code goes here" + "def calculate(*args, **kwargs):\n", + "\n", + " if not args:\n", + " return \"No numbers provided!\"\n", + "\n", + " operation = input(\"Enter operation (addition, subtraction, multiplication, division): \").strip().lower()\n", + "\n", + " if operation == \"addition\":\n", + " result = sum(args)\n", + "\n", + " elif operation == \"subtraction\":\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + "\n", + " elif operation == \"multiplication\":\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + "\n", + " elif operation == \"division\":\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " if num == 0:\n", + " return \"Error: Division by zero!\"\n", + " result /= num\n", + "\n", + " else:\n", + " return \"Invalid operation entered!\"\n", + "\n", + " if \"round_result\" in kwargs:\n", + " result = round(result, kwargs[\"round_result\"])\n", + "\n", + " return result\n", + "\n", + "num1 = int(input(\"Enter num1: \"))\n", + "num2 = int(input(\"Enter num2: \"))\n", + "\n", + "print(\"Result:\", calculate(num1, num2))\n", + "\n" ] }, { @@ -271,6 +478,30 @@ "You can read more about this here: https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html" ] }, + { + "cell_type": "code", + "execution_count": 71, + "id": "6183d32b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "87\n", + "\n" + ] + } + ], + "source": [ + "from functions import get_unique_list_f\n", + "\n", + "numbers = [1, 2, 3, 3, 3, 3, 4, 5]\n", + "\n", + "print(result)\n", + "print(type(result))\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -326,7 +557,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +571,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,