{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Day 14 Modules and Packages in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recap\n", "\n", "- Call by value\n", "- Call by reference\n", "- Variable length arguments\n", "- Modules and Packages\n", "\n", "### Day Objectives\n", "\n", "\n", "- Modules and Packages\n", " - Pre-defined/ in-built\n", " - User-Defined/ 3rd party" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5371613768087516\n" ] } ], "source": [ "print(random.random())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8227023481261443\n" ] } ], "source": [ "print(random.random())" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.74\n" ] } ], "source": [ "print(round(random.random(), 2))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 5\n" ] } ], "source": [ "a = 5.5\n", "\n", "ceil = 6\n", "floor = 5\n", "\n", "import math\n", "\n", "print(math.ceil(5.5), math.floor(5.5))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9027255381595464" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.random()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.09033985426934954" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.seed(55)\n", "\n", "random.random()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1456692551041303" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.seed(100)\n", "\n", "random.random()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9241408041681706" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.seed(1000000)\n", "\n", "random.random()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Return random integer in range [a, b], including both end points.\\n '" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint.__doc__" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(random.randint(0, 100))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "print(random.randint(0, 6))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "1\n", "1\n", "1\n", "5\n", "0\n" ] } ], "source": [ "for i in range(6):\n", " print(random.randint(0, 6))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65 90\n" ] } ], "source": [ "print(ord('A'), ord('Z'))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "M\n", "C\n", "M\n", "J\n", "X\n", "W\n", "J\n", "F\n", "Q\n", "O\n" ] } ], "source": [ "for i in range(10):\n", " print(chr(random.randint(65, 90)))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "head\n" ] } ], "source": [ "li = ['head', 'tail']\n", "\n", "print(random.choice(li))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tail\n", "tail\n", "head\n", "head\n", "tail\n", "head\n" ] } ], "source": [ "li = ['head', 'tail']\n", "for i in range(6):\n", " print(random.choice(li))" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "li = [1,2,3,4,5,6,7]\n", "random.shuffle(li)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7, 6, 2, 1, 4, 3, 5]\n" ] } ], "source": [ "random.shuffle(li)\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 5, 1, 7, 3, 6, 4]\n" ] } ], "source": [ "random.shuffle(li)\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.7774591305763382\n" ] } ], "source": [ "# random.gauss(mu, sigma) -> mu = mean, sigma = std\n", "\n", "print(random.gauss(0, 1))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.144456058349938\n", "7.670983477419868\n", "8.154743166289004\n", "8.162593217836559\n", "5.2051290853667975\n", "6.153058019018136\n", "8.528796621208931\n", "7.851741517503576\n", "9.189532854547217\n", "5.262431441942137\n" ] } ], "source": [ "for i in range(10):\n", " print(random.uniform(5, 10))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(1, 15)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "import calendar" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " June 2021\n", "Mo Tu We Th Fr Sa Su\n", " 1 2 3 4 5 6\n", " 7 8 9 10 11 12 13\n", "14 15 16 17 18 19 20\n", "21 22 23 24 25 26 27\n", "28 29 30\n", "\n" ] } ], "source": [ "print(calendar.month(2021, 6))" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " June 2035\n", "Mo Tu We Th Fr Sa Su\n", " 1 2 3\n", " 4 5 6 7 8 9 10\n", "11 12 13 14 15 16 17\n", "18 19 20 21 22 23 24\n", "25 26 27 28 29 30\n", "\n" ] } ], "source": [ "print(calendar.month(2035, 6))" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "import calendar as cal" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " June 2035\n", "Mo Tu We Th Fr Sa Su\n", " 1 2 3\n", " 4 5 6 7 8 9 10\n", "11 12 13 14 15 16 17\n", "18 19 20 21 22 23 24\n", "25 26 27 28 29 30\n", "\n" ] } ], "source": [ "print(cal.month(2035, 6))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Calendar',\n", " 'EPOCH',\n", " 'FRIDAY',\n", " 'February',\n", " 'HTMLCalendar',\n", " 'IllegalMonthError',\n", " 'IllegalWeekdayError',\n", " 'January',\n", " 'LocaleHTMLCalendar',\n", " 'LocaleTextCalendar',\n", " 'MONDAY',\n", " 'SATURDAY',\n", " 'SUNDAY',\n", " 'THURSDAY',\n", " 'TUESDAY',\n", " 'TextCalendar',\n", " 'WEDNESDAY',\n", " '_EPOCH_ORD',\n", " '__all__',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " '_colwidth',\n", " '_locale',\n", " '_localized_day',\n", " '_localized_month',\n", " '_monthlen',\n", " '_nextmonth',\n", " '_prevmonth',\n", " '_spacing',\n", " 'c',\n", " 'calendar',\n", " 'datetime',\n", " 'day_abbr',\n", " 'day_name',\n", " 'different_locale',\n", " 'error',\n", " 'firstweekday',\n", " 'format',\n", " 'formatstring',\n", " 'isleap',\n", " 'leapdays',\n", " 'main',\n", " 'mdays',\n", " 'month',\n", " 'month_abbr',\n", " 'month_name',\n", " 'monthcalendar',\n", " 'monthrange',\n", " 'prcal',\n", " 'prmonth',\n", " 'prweek',\n", " 'repeat',\n", " 'setfirstweekday',\n", " 'sys',\n", " 'timegm',\n", " 'week',\n", " 'weekday',\n", " 'weekheader']" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(cal)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.weekday.__doc__" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function weekday in module calendar:\n", "\n", "weekday(year, month, day)\n", " Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31).\n", "\n" ] } ], "source": [ "help(cal.weekday)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "print(cal.weekday(2021, 6, 6))" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method formatweek in module calendar:\n", "\n", "formatweek(theweek, width) method of calendar.TextCalendar instance\n", " Returns a single week in a string (no newline).\n", "\n" ] } ], "source": [ "help(cal.week)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2021\n", "\n", " January February March\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7\n", " 4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14\n", "11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21\n", "18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28\n", "25 26 27 28 29 30 31 29 30 31\n", "\n", " April May June\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 4 1 2 1 2 3 4 5 6\n", " 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13\n", "12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20\n", "19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27\n", "26 27 28 29 30 24 25 26 27 28 29 30 28 29 30\n", " 31\n", "\n", " July August September\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 4 1 1 2 3 4 5\n", " 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12\n", "12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19\n", "19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26\n", "26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30\n", " 30 31\n", "\n", " October November December\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5\n", " 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12\n", "11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19\n", "18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26\n", "25 26 27 28 29 30 31 29 30 27 28 29 30 31\n", "\n" ] } ], "source": [ "print(cal.calendar(2021))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2021, 6, 8)" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.datetime.date(2021, 6, 8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- from package import module \n", "- from package import module as alias\n", "- import package.module as alias" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Volume in drive C has no label.\n", " Volume Serial Number is 7866-1790\n", "\n", " Directory of C:\\Users\\Jesus\\Desktop\\Data Science Internship May-2021\n", "\n", "08-Jun-21 05:26 PM .\n", "08-Jun-21 05:26 PM ..\n", "08-Jun-21 04:29 PM .ipynb_checkpoints\n", "08-Jun-21 05:26 PM 17,063 Day 14 Modules and Packages in Python.ipynb\n", "26-May-21 06:59 PM 5,605 Day03_Markdown_Syntax.ipynb\n", "26-May-21 07:09 PM 286,289 Day03_Markdown_Syntax.slides.html\n", "26-May-21 07:09 PM 286,289 Day03_Markdown_Syntax.slidesa.html\n", "29-May-21 09:54 PM 17,953 Day06_Python_Basics.ipynb\n", "31-May-21 07:46 PM 29,748 Day07_Conditional_Statements_Loops_Strings.ipynb\n", "01-Jun-21 06:46 PM 39,036 Day08_Strings_and_StringMethods.ipynb\n", "02-Jun-21 08:00 PM 52,775 Day09_Lists_and_Tuples_in_Python.ipynb\n", "04-Jun-21 04:35 PM 32,065 Day10_Dictionary_in_Python.ipynb\n", "07-Jun-21 04:07 PM 66,708 Day11_Sets_and_Functions_in_Python.ipynb\n", "05-Jun-21 10:30 PM Day12_File_Handling_in_Python\n", "07-Jun-21 09:09 PM Day13_Files_and_Functions_Contd\n", "28-May-21 04:26 PM dummy\n", "05-Jun-21 06:34 PM 78,620 File Handing in Python.ipynb\n", "05-Jun-21 06:32 PM 32 file.txt\n", "05-Jun-21 06:27 PM 38,944 file1.txt\n", "05-Jun-21 05:56 PM 0 file10.txt\n", "05-Jun-21 06:27 PM 38,944 file2.txt\n", "05-Jun-21 06:27 PM 38,944 file3.txt\n", "05-Jun-21 06:27 PM 38,944 file4.txt\n", "05-Jun-21 06:27 PM 38,944 file5.txt\n", "05-Jun-21 06:27 PM 38,944 file6.txt\n", "05-Jun-21 06:27 PM 38,944 file7.txt\n", "05-Jun-21 06:27 PM 38,944 file8.txt\n", "05-Jun-21 06:27 PM 38,944 file9.txt\n", "26-May-21 06:37 PM 11,155 python-logo-master-v3-TM-flattened.png\n", "31-May-21 03:50 PM 17,803 Sample_Projects.ipynb\n", "31-May-21 09:33 PM 389,737 Untitled.ipynb\n", " 25 File(s) 1,681,374 bytes\n", " 6 Dir(s) 65,374,728,192 bytes free\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\Jesus\\\\Desktop\\\\Data Science Internship May-2021'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pwd" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "mkdir newFolder" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Volume in drive C has no label.\n", " Volume Serial Number is 7866-1790\n", "\n", " Directory of C:\\Users\\Jesus\\Desktop\\Data Science Internship May-2021\n", "\n", "08-Jun-21 05:29 PM .\n", "08-Jun-21 05:29 PM ..\n", "08-Jun-21 04:29 PM .ipynb_checkpoints\n", "08-Jun-21 05:29 PM 19,945 Day 14 Modules and Packages in Python.ipynb\n", "26-May-21 06:59 PM 5,605 Day03_Markdown_Syntax.ipynb\n", "26-May-21 07:09 PM 286,289 Day03_Markdown_Syntax.slides.html\n", "26-May-21 07:09 PM 286,289 Day03_Markdown_Syntax.slidesa.html\n", "29-May-21 09:54 PM 17,953 Day06_Python_Basics.ipynb\n", "31-May-21 07:46 PM 29,748 Day07_Conditional_Statements_Loops_Strings.ipynb\n", "01-Jun-21 06:46 PM 39,036 Day08_Strings_and_StringMethods.ipynb\n", "02-Jun-21 08:00 PM 52,775 Day09_Lists_and_Tuples_in_Python.ipynb\n", "04-Jun-21 04:35 PM 32,065 Day10_Dictionary_in_Python.ipynb\n", "07-Jun-21 04:07 PM 66,708 Day11_Sets_and_Functions_in_Python.ipynb\n", "05-Jun-21 10:30 PM Day12_File_Handling_in_Python\n", "07-Jun-21 09:09 PM Day13_Files_and_Functions_Contd\n", "28-May-21 04:26 PM dummy\n", "05-Jun-21 06:34 PM 78,620 File Handing in Python.ipynb\n", "05-Jun-21 06:32 PM 32 file.txt\n", "05-Jun-21 06:27 PM 38,944 file1.txt\n", "05-Jun-21 05:56 PM 0 file10.txt\n", "05-Jun-21 06:27 PM 38,944 file2.txt\n", "05-Jun-21 06:27 PM 38,944 file3.txt\n", "05-Jun-21 06:27 PM 38,944 file4.txt\n", "05-Jun-21 06:27 PM 38,944 file5.txt\n", "05-Jun-21 06:27 PM 38,944 file6.txt\n", "05-Jun-21 06:27 PM 38,944 file7.txt\n", "05-Jun-21 06:27 PM 38,944 file8.txt\n", "05-Jun-21 06:27 PM 38,944 file9.txt\n", "08-Jun-21 05:29 PM newFolder\n", "26-May-21 06:37 PM 11,155 python-logo-master-v3-TM-flattened.png\n", "31-May-21 03:50 PM 17,803 Sample_Projects.ipynb\n", "31-May-21 09:33 PM 389,737 Untitled.ipynb\n", " 25 File(s) 1,684,256 bytes\n", " 7 Dir(s) 65,378,168,832 bytes free\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "os.mkdir('folderFromPython')" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "os.remove('file9.txt')" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.ipynb_checkpoints',\n", " 'Day 14 Modules and Packages in Python.ipynb',\n", " 'Day03_Markdown_Syntax.ipynb',\n", " 'Day03_Markdown_Syntax.slides.html',\n", " 'Day03_Markdown_Syntax.slidesa.html',\n", " 'Day06_Python_Basics.ipynb',\n", " 'Day07_Conditional_Statements_Loops_Strings.ipynb',\n", " 'Day08_Strings_and_StringMethods.ipynb',\n", " 'Day09_Lists_and_Tuples_in_Python.ipynb',\n", " 'Day10_Dictionary_in_Python.ipynb',\n", " 'Day11_Sets_and_Functions_in_Python.ipynb',\n", " 'Day12_File_Handling_in_Python',\n", " 'Day13_Files_and_Functions_Contd',\n", " 'dummy',\n", " 'File Handing in Python.ipynb',\n", " 'file.txt',\n", " 'file1.txt',\n", " 'file10.txt',\n", " 'file2.txt',\n", " 'file3.txt',\n", " 'file4.txt',\n", " 'file5.txt',\n", " 'file6.txt',\n", " 'file7.txt',\n", " 'file8.txt',\n", " 'folderFromPython',\n", " 'newFolder',\n", " 'python-logo-master-v3-TM-flattened.png',\n", " 'Sample_Projects.ipynb',\n", " 'Untitled.ipynb']" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir()" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\Jesus\\\\Desktop\\\\Data Science Internship May-2021'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\Jesus\\\\Desktop\\\\Data Science Internship May-2021\\\\newFolder'" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.chdir('newFolder')\n", "\n", "\n", "os.getcwd()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- import module/package -> all the functions/classes/attr will be import\n", "- from package import module -> all the functions/classes/attr which are avaliable in that module\n", "- from package.module import method -> only functions/class/attri will be imported from package/module\n", "- from module import method -> module" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['__doc__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " 'acos',\n", " 'acosh',\n", " 'asin',\n", " 'asinh',\n", " 'atan',\n", " 'atan2',\n", " 'atanh',\n", " 'ceil',\n", " 'comb',\n", " 'copysign',\n", " 'cos',\n", " 'cosh',\n", " 'degrees',\n", " 'dist',\n", " 'e',\n", " 'erf',\n", " 'erfc',\n", " 'exp',\n", " 'expm1',\n", " 'fabs',\n", " 'factorial',\n", " 'floor',\n", " 'fmod',\n", " 'frexp',\n", " 'fsum',\n", " 'gamma',\n", " 'gcd',\n", " 'hypot',\n", " 'inf',\n", " 'isclose',\n", " 'isfinite',\n", " 'isinf',\n", " 'isnan',\n", " 'isqrt',\n", " 'ldexp',\n", " 'lgamma',\n", " 'log',\n", " 'log10',\n", " 'log1p',\n", " 'log2',\n", " 'modf',\n", " 'nan',\n", " 'perm',\n", " 'pi',\n", " 'pow',\n", " 'prod',\n", " 'radians',\n", " 'remainder',\n", " 'sin',\n", " 'sinh',\n", " 'sqrt',\n", " 'tan',\n", " 'tanh',\n", " 'tau',\n", " 'trunc']" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(math)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']\n" ] } ], "source": [ "import math as m\n", "\n", "print(dir(m))" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "from math import ceil, floor" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "56 66\n" ] } ], "source": [ "print(ceil(55.66), floor(66.66))" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "from math import *" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.3048106211022167 120\n" ] } ], "source": [ "print(sin(60), factorial(5))" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "from math import * # floor\n", "from random import * # floor" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "os.chdir('../')" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\Jesus\\\\Desktop\\\\Data Science Internship May-2021'" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "import anil" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hey welcome to my module called anil...'" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anil.__doc__" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " 'factorial',\n", " 'gravity',\n", " 'isEven',\n", " 'pi']" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(anil)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Check whether a number is even or not'" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anil.isEven.__doc__" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anil.isEven(5)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anil.factorial(5)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anil.pi" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " 'factorial',\n", " 'gravity',\n", " 'isEven',\n", " 'isOdd',\n", " 'pi']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import anil\n", "\n", "dir(anil)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'anil' has no attribute 'isodd'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0manil\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misodd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: module 'anil' has no attribute 'isodd'" ] } ], "source": [ "anil.isodd(5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'anilkumar'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0manilkumar\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'anilkumar'" ] } ], "source": [ "import anilkumar" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "anil.isOdd(5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "78.5\n" ] } ], "source": [ "from anil import pi\n", "\n", "print(pi * 5 **2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import package" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__path__',\n", " '__spec__']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(package)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import package.module1\n", "from package import module1 as m" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.isEven(5)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.isOdd(5)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "from package import module2 as m" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.isOdd(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task\n", "\n", "### Contact application `contact.py`\n", "\n", "Create a module with diff function\n", "\n", "\n", "- createContact -> for creating the contact of the user -> name, ph, email\n", "- DeleteContact -> Delete the contact given be the user -> name\n", "- editContcact -> Modify the existing contact ->ph/email/name\n", "- getContact -> show the details of the user -> name\n", "- save the contacts into a file -> contacts.txt" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def createContact(name, ph, email):\n", " f = open('contacts.txt', 'a')\n", " f.write(name + '\\t'+ ph + '\\t' + email)\n", " f.close()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\Jesus\\\\Desktop\\\\Data Science Internship May-2021'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.ipynb_checkpoints',\n", " 'anil.py',\n", " 'Day 14 Modules and Packages in Python.ipynb',\n", " 'Day03_Markdown_Syntax.ipynb',\n", " 'Day03_Markdown_Syntax.slides.html',\n", " 'Day03_Markdown_Syntax.slidesa.html',\n", " 'Day06_Python_Basics.ipynb',\n", " 'Day07_Conditional_Statements_Loops_Strings.ipynb',\n", " 'Day08_Strings_and_StringMethods.ipynb',\n", " 'Day09_Lists_and_Tuples_in_Python.ipynb',\n", " 'Day10_Dictionary_in_Python.ipynb',\n", " 'Day11_Sets_and_Functions_in_Python.ipynb',\n", " 'Day12_File_Handling_in_Python',\n", " 'Day13_Files_and_Functions_Contd',\n", " 'dummy',\n", " 'File Handing in Python.ipynb',\n", " 'file.txt',\n", " 'file1.txt',\n", " 'file10.txt',\n", " 'file2.txt',\n", " 'file3.txt',\n", " 'file4.txt',\n", " 'file5.txt',\n", " 'file6.txt',\n", " 'file7.txt',\n", " 'file8.txt',\n", " 'folderFromPython',\n", " 'newFolder',\n", " 'package',\n", " 'python-logo-master-v3-TM-flattened.png',\n", " 'Sample_Projects.ipynb',\n", " 'Untitled.ipynb',\n", " '__pycache__']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }