From 0e82d2097b8dd54279c8f68b6705e8b2f6aaf1a7 Mon Sep 17 00:00:00 2001 From: Kanak2208 Date: Thu, 29 Jan 2026 21:18:25 +0100 Subject: [PATCH] Functions_Extra_done --- lab-python-functions.ipynb | 269 ++++++++++++++++++++++++++++++++++++- 1 file changed, 265 insertions(+), 4 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..f085103 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -46,6 +46,40 @@ " # your code goes here\n" ] }, + { + "cell_type": "code", + "execution_count": 14, + "id": "41ecb192", + "metadata": {}, + "outputs": [], + "source": [ + "def get_unique_list(lst):\n", + " list = set(lst)\n", + " return list" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "bed3ffac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst = [1,2,3,3,3,4,5,5]\n", + "get_unique_list(lst)" + ] + }, { "cell_type": "markdown", "id": "214c2c27-c4fa-46cf-bfbf-26fb25b25882", @@ -78,6 +112,38 @@ " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": 21, + "id": "6822573a", + "metadata": {}, + "outputs": [], + "source": [ + "def count_case(str):\n", + " upper_count = (sum for word in str if word.isupper())\n", + " lower_count = (sum for word in str if word.lower())\n", + " return upper_count, lower_count" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "b6d2c7e4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(. at 0x10d3b82b0>, . at 0x10d2c0e10>)\n" + ] + } + ], + "source": [ + "str = 'Hello World'\n", + "print(count_case(str))" + ] + }, { "cell_type": "markdown", "id": "2c902b2e-369c-4a40-a166-ca5bc554cab3", @@ -125,6 +191,42 @@ " # your code goes here" ] }, + { + "cell_type": "code", + "execution_count": 31, + "id": "33f26b39", + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "\n", + "def remove_punctuation(sentence):\n", + " ans = sentence.strip(\":\")\n", + " return ans" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "ef7ddde6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Note : this is an example !!! Good day : )'" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sentence = \"Note : this is an example !!! Good day : )\"\n", + "remove_punctuation(sentence)" + ] + }, { "cell_type": "markdown", "id": "52814e4d-7631-4c80-80b6-2b206f7e7419", @@ -168,12 +270,171 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "def add(num1, num2):\n", + " sum = num1 + num2\n", + " return sum\n", + "\n", + "def subt(num1 , num2):\n", + " sub = num1 - num2\n", + " return sub\n", + "\n", + "def mult(num1, num2):\n", + " mul = num1*num2\n", + " return mul\n", + "\n", + "def divide(num1, num2):\n", + " divi = num1/num2\n", + " return divi\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "41201882", + "metadata": {}, + "outputs": [], + "source": [ + "num1 = 10, \n", + "num2 = 5\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "bd1e26c3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(10,5)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "9a3c2abd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "subt(10,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "9fd84ee6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mult(10,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "465a71f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divide(10,5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b22159a3", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate(operand1, operand2, operator):\n", + " if operator == '+':\n", + " return add(operand1, operand2)\n", + " if operator == '-':\n", + " return subt(operand1, operand2)\n", + " if operator == '*':\n", + " return mult(operand1, operand2)\n", + " if operator == '/':\n", + " return divide(operand1, operand2)\n", + " if operand2 == 0:\n", + " print('Error: can not divide by zero')\n", + " else:\n", + " return 'Invalid operator'" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "0228aabc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n", + "Invalid operator\n" + ] + } + ], + "source": [ + "print(calculate(10, 5, '+')) \n", + "print(calculate(10, 5, '-')) \n", + "print(calculate(10, 5, '*')) \n", + "print(calculate(10, 5, '/')) \n", + "#print(calculate(10, 0, '/')) \n", + "print(calculate(10, 5, '%')) \n" ] }, { @@ -326,7 +587,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +601,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.2" } }, "nbformat": 4,