diff --git a/Project/builtin_functions.ipynb b/Project/builtin_functions.ipynb new file mode 100644 index 00000000..78090a12 --- /dev/null +++ b/Project/builtin_functions.ipynb @@ -0,0 +1,328 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# builtin methods [doc]\n", + "- statistics\n", + " - len()\n", + " - max()\n", + " - min()\n", + " - sum()\n", + "- utility\n", + " - sorted()\n", + " - reversed()\n", + " - eval()\n", + " - exec()\n", + "- operate on iterable\n", + " - all()\n", + " - any()\n", + " - enumerate()\n", + " - zip()\n", + " - filter()\n", + " - map()\n", + "- type conversion\n", + " - int()\n", + " - float()\n", + " - str()\n", + " - list()\n", + " - tuple()\n", + " - dict()\n", + " - set()\n", + " - bool()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = ['A','N','B','A','C','Z','D','E']\n", + "y = \"this is an example string\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sorted(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(sorted(y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sorted(x, reverse=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sorted(y.split(), reverse=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "z = 'we are working on an Encyclopedia in which there will be stuff'\n", + "sorted(z.split(), key=len)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reversed(z) # lazy object" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\".join(list(reversed(z))) # not the best way to reverse a string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "z[::-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = [1,1,1,11,1]\n", + "print(all(x))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "z =['this','is', '', 'example', 'string']\n", + "print(all(z))\n", + "print(any(z))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = [0,0,1,0,0]\n", + "print(any(x))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- math functions\n", + "\n", + "`f(x) = x**2 + x + 10`\n", + "\n", + "`f(5)`\n", + "\n", + "`g(x, y) = x**2 + y**2 + x*y + 10`\n", + "\n", + "`g(2,3)`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "f = lambda i : i**2\n", + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "f(25)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "f = lambda x: x**2 + x + 10\n", + "print(f(10))\n", + "print(f(15))\n", + "print(f(25))\n", + "print(f(11))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = [12,45,5,17,89,124,32,15,16]\n", + "for val in data:\n", + " print(f(val))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# map(function, iterable)\n", + "list(map(f, data)) # lazy object" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "nums =['1', '100', '300', '599']\n", + "clean_list = map(lambda i: int(i), nums)\n", + "print(list(clean_list))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "clean_list = map(int, nums)\n", + "print(list(clean_list))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "nums = [1,2,0,1,0,2,2,2,2,0,3,3,0,3,1,0,0,2,2,3,3]\n", + "list(filter(lambda i: i != 0, nums))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "isinstance(x, list)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "isinstance(x, (int, float))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "isinstance(x, (list, tuple))" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 14, 24, 36, 50]" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1,2,3,4,5]\n", + "y = [6,7,8,9,10]\n", + "list(map(lambda i, j: i*j, x, y))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Project/user_defined_func.ipynb b/Project/user_defined_func.ipynb new file mode 100644 index 00000000..46aa3809 --- /dev/null +++ b/Project/user_defined_func.ipynb @@ -0,0 +1,680 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "non-parameterized functions" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# define\n", + "def message():\n", + " print(\"Good Morning\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good Morning\n" + ] + } + ], + "source": [ + "# call (use)\n", + "message()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def si():\n", + " p = int(input(\"Enter Principal Amount:\"))\n", + " r = float(input(\"Enter Rate of Interest:\"))\n", + " t = int(input(\"Enter Time in Years:\"))\n", + " si = p*r*t/100\n", + " print(f'Simple Interest is {si}')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Simple Interest is 6600.0\n" + ] + } + ], + "source": [ + "si()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def hypotenuse(p, b):\n", + " h = ( p**2 + b**2 ) **.5\n", + " print(f'hyp: {h}')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hyp: 50.0\n" + ] + } + ], + "source": [ + "hypotenuse(30, 40)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# a more professional way to define a function\n", + "def hypotenuse(p:int, b:int):\n", + " '''\n", + " This function take perpendicular and base to calculate hypotenuse\n", + " '''\n", + " h = ( p**2 + b**2 ) **.5\n", + " print(f'hyp: {h}')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hyp: 17.69180601295413\n" + ] + } + ], + "source": [ + "hypotenuse(12, 13)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "requirement parameters" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "# hypotenuse() # error" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "# hypotenuse(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "# hypotenuse(2, 5, 10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "default parameters\n", + "- always start giving defaults from the right side (end)\n", + "- default values can be of any datatypes" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "def summer(a, b, c = 0, d = 0):\n", + " print(a + b + c + d)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "103\n" + ] + } + ], + "source": [ + "summer(12,34,12,45)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "150\n" + ] + } + ], + "source": [ + "summer(120, 30)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60\n" + ] + } + ], + "source": [ + "summer(10,20,30)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "def area_circle(radius = 1):\n", + " area = 22/7 * radius ** 2\n", + " print(area)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "314.2857142857143\n" + ] + } + ], + "source": [ + "area_circle(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "keyword arguments (named parameters)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "10\n", + "33\n", + "33\n", + "13\n", + "29\n", + "314.2857142857143\n" + ] + } + ], + "source": [ + "summer(1,2,3,4)\n", + "summer(a=1, b=2, c=3, d=4)\n", + "summer(1, 2, c=10, d=20)\n", + "summer(1, 2, d=20, c=10)\n", + "summer(d=5, c=5, a=1, b=2)\n", + "summer(12,12,d=5)\n", + "area_circle(radius=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "variable arguments" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "def mean(*data:int):\n", + " ans = sum(data)/len(data)\n", + " print(f\"average:{ans}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average:3.4\n" + ] + } + ], + "source": [ + "mean(1,2,3,5,6)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average:2.526315789473684\n" + ] + } + ], + "source": [ + "mean(1,2,3,5,6,2,2,2,1,1,1,2,4,2,2,2,4,3,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def task (**details ): #has items as a method to find groups\n", + " for name,value in details.items():\n", + " print(f'{name:10} {value}')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "title learn datatypes\n", + "duration 1day\n", + "credit 1\n" + ] + } + ], + "source": [ + "task (title=\"learn datatypes\",duration=\"1day\",credit=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "name bring food\n", + "details brinjal tomato potato\n", + "budget 200\n", + "date 26-sept-2023\n" + ] + } + ], + "source": [ + "task (name = 'bring food',\n", + " details='brinjal tomato potato',\n", + " budget=200,\n", + " date='26-sept-2023')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def hypotenuse(p:int , b:int) ->float:\n", + " '''This function take perpendicular and base to calculate hypotenuse'''\n", + " h=(p**2+b**2) **.5\n", + " return h" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5.0\n" + ] + } + ], + "source": [ + "ans=hypotenuse(3,4)\n", + "print(ans)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def cube_area(side):\n", + " return side * 6 **2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "360" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cube_area(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def cube_vol(side):\n", + " return side**3" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1000 360\n" + ] + } + ], + "source": [ + "cv=cube_vol(10)\n", + "ca=cube_area(10)\n", + "print(cv,ca)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1020\n" + ] + } + ], + "source": [ + "x=cube_vol(10)+20\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "#returns number of words in a strings\n", + "def count_word(sentence):\n", + " \n", + " return len(sentence.split())" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "c=count_word(\"hello hi hello\")\n", + "print(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hll h hll \n" + ] + } + ], + "source": [ + "#remove all vowels from string\n", + "def remove_vowel(string):\n", + " for s in 'aeiouAEIOU':\n", + " string = string.replace(s, '')\n", + " return string\n", + "\n", + "print(remove_vowel(\"hello hi hello \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[7]\n" + ] + } + ], + "source": [ + "#find all indexes of a substring in a given string using a function\n", + "def find_indexes(substr,content):\n", + " startpos=0\n", + " idx_list = []\n", + " for i in range(content.count(substr)):\n", + " idx=content.find(substr,startpos)#find function returns index values\n", + " # if idx ==-1:\n", + " # break not needed\n", + " idx_list.append(idx)\n", + " startpos=idx+1\n", + " return idx_list\n", + "\n", + "print(find_indexes( \"hi\",\"helllo hi vaibhav \" ))" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "one two|0 1\n", + "two three|1 2\n", + "three four|2 3\n" + ] + } + ], + "source": [ + "### function extract all bi-grams from a string using a function?\n", + "def bigrams(msg=\"one two three four\"):\n", + " words=msg.split()\n", + " f,s=0,1\n", + " for i in range(len(words)):\n", + " if s==len(words):\n", + " break\n", + " print(words[f],words[s],end= '|')\n", + " print(f,s)\n", + " s+=1\n", + " f+=1\n", + "\n", + "bigrams()" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "here drink the coffee\n" + ] + } + ], + "source": [ + "#remove all punctuations from a string\n", + "def remove_punctuation(msg):\n", + " from string import punctuation\n", + " for p in punctuation:\n", + " msg=msg.replace(p,'')\n", + " return msg\n", + "\n", + "print(remove_punctuation(\"here, drink ! the coffee.\"))\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}