{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## [The Python Standard Library](https://docs.python.org/3/library/index.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python’s standard library is very extensive, offering a wide range of facilities as indicated by the long table of contents listed below. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```string```" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "this is (0,0) integer\n", "this is (0,1) integer\n", "this is (1,0) integer\n", "this is (1,1) integer\n", "this is (2,0) integer\n", "this is (2,1) integer\n", "this is (3,0) integer\n", "this is (3,1) integer\n", "this is (4,0) integer\n", "this is (4,1) integer\n" ] } ], "source": [ "for i in range(5):\n", " for j in range(2):\n", " print(\"this is ({},{}) integer\".format(i,j))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['shubhamg199630@gmail.com', 'priya@yahoo.com', 'abc@example.com.', 'shubhamg@gmail.com', 'priya2@yahoo.com', 'abcd@example.com.', 'shubhamg19@gmail.com', 'priya5@yahoo.com', 'abct@example.com.']\n" ] } ], "source": [ "import re \n", " \n", "s = 'Hello from shubhamg199630@gmail.com to priya@yahoo.com about\\\n", " the meeting @2PM. Can we meet today for an important decision?\\\n", " Based on previous meeting minute we had decided to write an emailto\\\n", " abc@example.com. In case we do not get reply.\\\n", " Hello from shubhamg@gmail.com to priya2@yahoo.com about\\\n", " the meeting @2PM. Can we meet today for an important decision?\\\n", " Based on previous meeting minute we had decided to write an emailto\\\n", " abcd@example.com. In case we do not get reply\\\n", " Hello from shubhamg19@gmail.com to priya5@yahoo.com about\\\n", " the meeting @2PM. Can we meet today for an important decision?\\\n", " Based on previous meeting minute we had decided to write an emailto\\\n", " abct@example.com. In case we do not get reply'\n", " \n", "lst = re.findall('\\S+@\\S+', s) \n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```collections```" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "from collections import namedtuple" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "point = namedtuple('Point', ['x', 'y'])" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Point(x=11, y=12)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point(11,12)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Point(x=2, y=3)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point(2,3)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "from collections import Counter" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({1: 1, 3: 2, 5: 4, 6: 3, 8: 1, 4: 2, 2: 1})" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [ 1,3,5,6,8,6,5,4,5,6,5,4,3,2]\n", "Counter(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```itertools```" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('a', 'b')\n", "('a', 'c')\n", "('a', 'd')\n", "('b', 'a')\n", "('b', 'c')\n", "('b', 'd')\n", "('c', 'a')\n", "('c', 'b')\n", "('c', 'd')\n", "('d', 'a')\n", "('d', 'b')\n", "('d', 'c')\n" ] } ], "source": [ "from itertools import permutations\n", "L = [\"a\",\"b\",\"c\",\"d\",\"e\"]\n", "for item in permutations(L,2):\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('a', 'b', 'c')\n", "('a', 'b', 'd')\n", "('a', 'c', 'b')\n", "('a', 'c', 'd')\n", "('a', 'd', 'b')\n", "('a', 'd', 'c')\n", "('b', 'a', 'c')\n", "('b', 'a', 'd')\n", "('b', 'c', 'a')\n", "('b', 'c', 'd')\n", "('b', 'd', 'a')\n", "('b', 'd', 'c')\n", "('c', 'a', 'b')\n", "('c', 'a', 'd')\n", "('c', 'b', 'a')\n", "('c', 'b', 'd')\n", "('c', 'd', 'a')\n", "('c', 'd', 'b')\n", "('d', 'a', 'b')\n", "('d', 'a', 'c')\n", "('d', 'b', 'a')\n", "('d', 'b', 'c')\n", "('d', 'c', 'a')\n", "('d', 'c', 'b')\n" ] } ], "source": [ "for item in permutations(L,3):\n", " print(item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```os```" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "path = \"plot\"\n", "os.path.isdir(path)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path = \"1. DataStructure.ipynb\"\n", "os.path.isfile(path)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.git',\n", " '.gitignore',\n", " '.ipynb_checkpoints',\n", " '1. DataStructure.ipynb',\n", " '1.1-MiniAssignment-DNAcount.ipynb',\n", " '2. LoopAndCondition.ipynb',\n", " '3. Input and Output.ipynb',\n", " '4. FunctionAndClass.ipynb',\n", " '5. Learning from Errors.ipynb',\n", " '6. The Python Standard Library.ipynb',\n", " 'data',\n", " 'Learning to read error.ipynb',\n", " 'plot',\n", " 'Project1-Fern-I.ipynb',\n", " 'Project2-Rwalk.ipynb',\n", " 'Project3-Ncharges.ipynb',\n", " 'Project4-Diffusion.ipynb',\n", " 'Project5-Fern-II.ipynb',\n", " 'README.md']" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir()" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Description ProcessId \n", "\n", "System Idle Process 0 \n", "\n", "System 4 \n", "\n", "Registry 96 \n", "\n", "smss.exe 416 \n", "\n", "csrss.exe 640 \n", "\n", "csrss.exe 744 \n", "\n", "wininit.exe 772 \n", "\n", "services.exe 820 \n", "\n", "lsass.exe 868 \n", "\n", "winlogon.exe 940 \n", "\n", "svchost.exe 392 \n", "\n", "svchost.exe 576 \n", "\n", "fontdrvhost.exe 592 \n", "\n", "fontdrvhost.exe 588 \n", "\n", "WUDFHost.exe 564 \n", "\n", "svchost.exe 1068 \n", "\n", "svchost.exe 1120 \n", "\n", "dwm.exe 1208 \n", "\n", "svchost.exe 1344 \n", "\n", "svchost.exe 1352 \n", "\n", "svchost.exe \n" ] } ], "source": [ "import os \n", "\n", "# Running the aforementioned command and saving its output \n", "output = os.popen('wmic process get description, processid').read() \n", "\n", "# Displaying the output \n", "print(output[0:1000]) \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```glob```" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1. DataStructure.ipynb',\n", " '1.1-MiniAssignment-DNAcount.ipynb',\n", " '2. LoopAndCondition.ipynb',\n", " '3. Input and Output.ipynb',\n", " '4. FunctionAndClass.ipynb',\n", " '5. Learning from Errors.ipynb',\n", " '6. The Python Standard Library.ipynb',\n", " 'Learning to read error.ipynb',\n", " 'Project1-Fern-I.ipynb',\n", " 'Project2-Rwalk.ipynb',\n", " 'Project3-Ncharges.ipynb',\n", " 'Project4-Diffusion.ipynb',\n", " 'Project5-Fern-II.ipynb']" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import glob\n", "glob.glob('*.ipynb')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```time```" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "done!\n" ] }, { "data": { "text/plain": [ "0.09277701377868652" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "\n", "t1 = time.time()\n", "for i in range(1000001):\n", " if i==1000000:\n", " print(\"done!\")\n", "\n", "t2 = time.time()\n", "t2-t1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```urllib```" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "ff = open('data/web.txt','w')" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import urllib.request\n", "import urllib.parse\n", "params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})\n", "url = \"http://www.python.org\"\n", "with urllib.request.urlopen(url) as f:\n", " ff.write(f.read().decode('utf-8'))\n", " \n", "ff.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```tqdm```" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 100/100 [00:50<00:00, 1.99it/s]\n" ] } ], "source": [ "import time\n", "from tqdm import tqdm\n", "\n", "mylist = [i for i in range(100)]\n", "\n", "for i in tqdm(mylist):\n", " time.sleep(0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }