├── NLP ├── abc.txt ├── NLP_Assignment_04.ipynb ├── NLP_Assignment_02.ipynb ├── NLP_Assignment_07.ipynb └── NLP_Assignment_06.ipynb ├── Python ├── Basic Python │ ├── index.txt │ ├── Python_Basics_Assignment_14.ipynb │ ├── Python_Basics_Assignment_10.ipynb │ ├── Python_Basics_Assignment_8.ipynb │ ├── Python_Basics_Assignment_25.ipynb │ ├── Python_Basics_Assignment_18.ipynb │ └── Python_Basics_Assignment_23.ipynb ├── Advance Python │ ├── Advance_Assignment_10.ipynb │ ├── Advance_Assignment_25.ipynb │ ├── Advance_Assignment_24.ipynb │ ├── Advance_Assignment_11.ipynb │ ├── Advance_Assignment_20.ipynb │ ├── Advance_Assignment_08.ipynb │ ├── Advance_Assignment_21.ipynb │ ├── Advance_Assignment_01.ipynb │ ├── Advance_Assignment_07.ipynb │ ├── Advance_Assignment_06.ipynb │ ├── Advance_Assignment_18.ipynb │ └── Advance_Assignment_16.ipynb └── Basic_Programming │ ├── Basic_Programming_Assignment_07.ipynb │ ├── Basic_Programming_Assignment_06.ipynb │ ├── Basic_Programming_Assignment_15.ipynb │ ├── Basic_Programming_Assignment_01.ipynb │ ├── Basic_Programming_Assignment_20.ipynb │ ├── Basic_Programming_Assignment_23.ipynb │ ├── Basic_Programming_Assignment_19.ipynb │ └── Basic_Programming_Assignment_08.ipynb ├── Deep Learning └── Assignment_1.docx └── README.md /NLP/abc.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Python/Basic Python/index.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Deep Learning/Assignment_1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ratang94/iNeuron-Data-Science-Assignments/HEAD/Deep Learning/Assignment_1.docx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 👨‍💻 iNeuron Full Stack Data Science Assignments 3 | 4 | 5 | This repository contains solutions of iNeuron Full Stack Data Science Dashboard Assignments. 6 | 7 | 8 | 9 | **Current Status**: Done in Total **94 Assignments**. Any feedback on improvement in code or answers is welcome and much appreciated. Thanks in Advance 10 | 11 | 12 | 13 | ## Leaderboard 14 | 15 | ![App Screenshot](https://i.imgur.com/2N8YlVX.jpg) 16 | 17 | -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b8afdc36", 6 | "metadata": { 7 | "id": "b8afdc36" 8 | }, 9 | "source": [ 10 | "# Assignment 10 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "era16qek82qF" 20 | }, 21 | "id": "era16qek82qF" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "54257650", 26 | "metadata": { 27 | "id": "54257650" 28 | }, 29 | "source": [ 30 | "#### Q1. What is the difference between `__getattr__` and `__getattribute__`?\n", 31 | "**Ans:** **`__getattribute__`** is used to find an attribute of a class. It raises an **AttributeError** if it fails to find an attribute of a class. **`__getattr__`** is implemented latter if AttributeError is generated by **`__getattribute__`**, but for this **`__getattribute__`** and **`__getattr__`** both has to be defined in same class. If no attribute is found, **`__getattr__`** returns a default value. So key difference is that **`__getattr__`** is called for attributes that don't actually exist on a class. " 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "51f7319c", 37 | "metadata": { 38 | "id": "51f7319c" 39 | }, 40 | "source": [ 41 | "#### Q2. What is the difference between properties and descriptors?\n", 42 | "**Ans:** The differences between Properties and Descriptors is:\n", 43 | "\n", 44 | "**Properties:** With Properties we can bind getter, setter and delete functions together with an attribute name, using the built-in property function or `@property` decorator. When we do this, each reference to an attribute looks like simple, direct access, but involes the appropriate function of the object.\n", 45 | "\n", 46 | "**Descriptor:** With Descriptor we can bind getter, setter and delete functions into a seperate class. we then assign an object of this class to the attribute name in our main class. When we do this, each reference to an attribute looks like simple, direct access but invokes an appropriate function of descriptor object." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "3303787c", 52 | "metadata": { 53 | "id": "3303787c" 54 | }, 55 | "source": [ 56 | "#### Q3. What are the key differences in functionality between __getattr__ and __getattribute__, as well as properties and descriptors?\n", 57 | "**Ans:** The Key Differences between `__getattr__`, ` __getattribute__`, Properties and Descriptors are:\n", 58 | "\n", 59 | "**`__getattr__`**: Python will call this method whenever you request an attribute that hasn't already been defined\n", 60 | "\n", 61 | "**`__getattribute__`** : This method will invoked before looking at the actual attributes on the object. This means that if we have `__getattribute__` method in our class, python invokes this method for every attribute regardless whether it exists or not. \n", 62 | "\n", 63 | "**Properties:** With Properties we can bind getter, setter and delete functions together with an attribute name, using the built-in property function or @property decorator. When we do this, each reference to an attribute looks like simple, direct access, but involes the appropriate function of the object.\n", 64 | "\n", 65 | "**Descriptor:** With Descriptor we can bind getter, setter and delete functions into a seperate class. we then assign an object of this class to the attribute name in our main class. When we do this, each reference to an attribute looks like simple, direct access but invokes an appropriate function of descriptor object." 66 | ] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.6.13" 86 | }, 87 | "colab": { 88 | "name": "Advance_Assignment_10.ipynb", 89 | "provenance": [] 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 5 94 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_25.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ae0f9c40", 6 | "metadata": { 7 | "id": "ae0f9c40" 8 | }, 9 | "source": [ 10 | "# Assignment 25 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "OA9llRJOeoS6" 20 | }, 21 | "id": "OA9llRJOeoS6" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "bb43a46e", 26 | "metadata": { 27 | "id": "bb43a46e" 28 | }, 29 | "source": [ 30 | "#### Q1. What is the distinction between a numpy array and a pandas data frame? Is there a way to convert between the two if there is ?\n", 31 | "**Ans:** Numpy Ndarray provides a lot of convenient and optimized methods for performing several mathematical operations on vectors.\n", 32 | "\n", 33 | "Pandas Dataframe is an in-memory 2-dimensional tabular representation of data. In simpler words, it can be seen as a spreadsheet having rows and columns.\n", 34 | "\n", 35 | "Conversion : `Dataframe=pandas.DataFrame(array)`" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "24146fcb", 41 | "metadata": { 42 | "id": "24146fcb" 43 | }, 44 | "source": [ 45 | "#### Q2. Identify some of the plotting techniques that are used to produce a stock-market chart ?\n", 46 | "**Ans:** Bar chart, Line Chart, Candlestick chart (using mplfinance module) are used for plotting stock market chart." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "f0fdf95b", 52 | "metadata": { 53 | "id": "f0fdf95b" 54 | }, 55 | "source": [ 56 | "#### Q3. Why is it essential to print a legend on a stock market chart ?\n", 57 | "**Ans:** Legend will help us in comparing different stocks in a stock market chart. Each plot of a Stock chart has a legend, its items representing the series on the plot. In addition, the legend displays information about the points that are currently hovered over or, if none are hovered over, about the last points shown on the plot. The text of a legend item includes the name of a series and, depending on the series type, the value or values of the current or last point. The legend title, enabled by default, shows the current or last date (X-value)." 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "id": "186d0d51", 63 | "metadata": { 64 | "id": "186d0d51" 65 | }, 66 | "source": [ 67 | "#### Q4. What is the best way to limit the length of a pandas data frame to less than a year ?\n", 68 | "**Ans:** : We can use **start** and **end** parameters for that. In start, we write the date from where we are starting and at the end, we write the end date. SO within this span we can restrict the duration. Also, we can use the parameters like periods i.e for how much times we need the duration and we can also use the frequency parameter for that." 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "5805fa40", 74 | "metadata": { 75 | "id": "5805fa40" 76 | }, 77 | "source": [ 78 | "#### Q5. What is the definition of a 180-day moving average ?\n", 79 | "**Ans:** : The 180-day moving average is represented as a line on charts and represents the average price over the past 180 days. The moving average can give traders a sense regarding whether the trend is up or down, while also identifying potential support or resistance areas. A moving average (MA) is a stock indicator that is commonly used in technical analysis. The reason for calculating the moving average of a stock is to help smooth out the price data over a specified period of time by creating a constantly updated average price.\n", 80 | "A simple moving average (SMA) is a calculation that takes the arithmetic mean of a given set of prices over the specific number of days in the past; for example, in this case over 180 days." 81 | ] 82 | } 83 | ], 84 | "metadata": { 85 | "kernelspec": { 86 | "display_name": "Python 3", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.8.8" 101 | }, 102 | "colab": { 103 | "name": "Advance_Assignment_25.ipynb", 104 | "provenance": [], 105 | "collapsed_sections": [] 106 | } 107 | }, 108 | "nbformat": 4, 109 | "nbformat_minor": 5 110 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_24.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d8ed8d0b", 6 | "metadata": { 7 | "id": "d8ed8d0b" 8 | }, 9 | "source": [ 10 | "# Assignment 24 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "yXAHq70nb7a2" 20 | }, 21 | "id": "yXAHq70nb7a2" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "d91cebf0", 26 | "metadata": { 27 | "id": "d91cebf0" 28 | }, 29 | "source": [ 30 | "#### Q1. Is it permissible to use several import statements to import the same module? What would the goal be? Can you think of a situation where it would be beneficial ?\n", 31 | "**Ans:** Yes, it is permissible to use several import statements to import the same module. It is used in case when we have to import multiple functions from same module. " 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "4cca993e", 37 | "metadata": { 38 | "id": "4cca993e" 39 | }, 40 | "source": [ 41 | "#### Q2. What are some of a module's characteristics? (Name at least one.)\n", 42 | "**Ans:** The following are some of a module's characteristics:\n", 43 | "- **`__name__`** : It returns the name of the module\n", 44 | "- **`__doc__`** : It denotes the documentation string line written in a module code.\n", 45 | "- **`__file__`** : It holds the name and path of the module file from which it is loaded\n", 46 | "- **`__dict__`** : It returns a dictionary object of module attributes, functions and other definitions and their respective values" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "748f017b", 52 | "metadata": { 53 | "id": "748f017b" 54 | }, 55 | "source": [ 56 | "#### Q3. Circular importing, such as when two modules import each other, can lead to dependencies and bugs that aren't visible. How can you go about creating a program that avoids mutual importing?\n", 57 | "**Ans:** Circular importing means importing the two modules in each other. If suppose we are wokring in `MOD1.py` file and it is importing some function say F2() from some other module say\n", 58 | "`MOD2.PY` file or we can do vice-versa. What will happen is: This will give an `import error`.\n", 59 | "\n", 60 | "This is because when we import F2() function from module `MOD2.py`, then this will execute `MOD2.py` file and in `MOD2.py` file, there is another statement of importing `MOD1.py` module.\n", 61 | "\n", 62 | "This will result in endless loop. To avoid this error, we can use `if __name__ == '__main__'`\n", 63 | "\n", 64 | "In this function, we can't directly refer to the function in the program. The addition of this sentence avoids the endless loop of the program . We can use an if __name__ == \"__main__\" block to allow or prevent parts of code from being run when the modules are imported. When the Python interpreter reads a file, the __name__ variable is set as __main__ if the module being run, or as the module's name if it is imported" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "74cfdc5e", 70 | "metadata": { 71 | "id": "74cfdc5e" 72 | }, 73 | "source": [ 74 | "#### Q4. Why is **`__all__`** in Python ?\n", 75 | "**Ans:** It provides the list of all modules present in a library. " 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "0d818cbd", 81 | "metadata": { 82 | "id": "0d818cbd" 83 | }, 84 | "source": [ 85 | "#### Q5. In what situation is it useful to refer to the `__name__` attribute or the string `__main_ _` ?\n", 86 | "**Ans:** During the time of execution of the code, if we want to refer the module in which we are working on, then we use `__name__` attribute. In that case it will return the module in which we are working on. Suppose if the module is being imported from some other module, then name will have the name of that module from where the current module has been imported. The current module in which we are working is refer to the string `__main __`.\n", 87 | "\n", 88 | "This built-in attributes prints the name of the class, type, function, method, descriptor, or generator instance.\n", 89 | "\n", 90 | "For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value \"__main__\". If this file is being imported from another module, __name__ will be set to the module's name." 91 | ] 92 | } 93 | ], 94 | "metadata": { 95 | "kernelspec": { 96 | "display_name": "Python 3", 97 | "language": "python", 98 | "name": "python3" 99 | }, 100 | "language_info": { 101 | "codemirror_mode": { 102 | "name": "ipython", 103 | "version": 3 104 | }, 105 | "file_extension": ".py", 106 | "mimetype": "text/x-python", 107 | "name": "python", 108 | "nbconvert_exporter": "python", 109 | "pygments_lexer": "ipython3", 110 | "version": "3.8.8" 111 | }, 112 | "colab": { 113 | "name": "Advance_Assignment_24.ipynb", 114 | "provenance": [], 115 | "collapsed_sections": [] 116 | } 117 | }, 118 | "nbformat": 4, 119 | "nbformat_minor": 5 120 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_11.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "344e8e27", 6 | "metadata": { 7 | "id": "344e8e27" 8 | }, 9 | "source": [ 10 | "# Assignment 11 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "L5iiBdv7_jvX" 20 | }, 21 | "id": "L5iiBdv7_jvX" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "11489fdc", 26 | "metadata": { 27 | "id": "11489fdc" 28 | }, 29 | "source": [ 30 | "#### Q1. What is the concept of a metaclass?\n", 31 | "**Ans:** Metaclass in Python is a class of a class that defines how a class behaves. A class is itself a instance of Metaclass, and any Instance of Class in Python is an Instance of type metaclass. E.g. Type of of `int`, `str`, `float`, `list`, `tuple` and many more is of metaclass type. " 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "source": [ 37 | "# our metaclass\n", 38 | "class MultiBases(type):\n", 39 | "\t# overriding __new__ method\n", 40 | "\tdef __new__(cls, clsname, bases, clsdict):\n", 41 | "\t\t# if no of base classes is greater than 1\n", 42 | "\t\t# raise error\n", 43 | "\t\tif len(bases)>1:\n", 44 | "\t\t\traise TypeError(\"Inherited multiple base classes!!!\")\n", 45 | "\t\t\n", 46 | "\t\t# else execute __new__ method of super class, ie.\n", 47 | "\t\t# call __init__ of type class\n", 48 | "\t\treturn super().__new__(cls, clsname, bases, clsdict)\n", 49 | "\n", 50 | "# metaclass can be specified by 'metaclass' keyword argument\n", 51 | "# now MultiBase class is used for creating classes\n", 52 | "# this will be propagated to all subclasses of Base\n", 53 | "class Base(metaclass=MultiBases):\n", 54 | "\tpass\n", 55 | "\n", 56 | "# no error is raised\n", 57 | "class A(Base):\n", 58 | "\tpass\n", 59 | "\n", 60 | "# no error is raised\n", 61 | "class B(Base):\n", 62 | "\tpass\n", 63 | "\n", 64 | "# This will raise an error!\n", 65 | "class C(A, B):\n", 66 | "\tpass\n" 67 | ], 68 | "metadata": { 69 | "id": "JmOSZB0t_5WS" 70 | }, 71 | "id": "JmOSZB0t_5WS", 72 | "execution_count": null, 73 | "outputs": [] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "id": "a7c66d6f", 78 | "metadata": { 79 | "id": "a7c66d6f" 80 | }, 81 | "source": [ 82 | "#### Q2. What is the best way to declare a class's metaclass?\n", 83 | "**Ans:** A way to declare a class’ metaclass is by using **`metaclass`** keyword in class definition." 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "id": "e1a13053", 90 | "metadata": { 91 | "id": "e1a13053", 92 | "outputId": "418dd8d1-196f-4ff2-f5b1-e563b5dc2eb3" 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "\n", 100 | "\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "class meta(type):\n", 106 | " pass\n", 107 | "class class_meta(metaclass=meta):\n", 108 | " pass\n", 109 | "print(type(meta))\n", 110 | "print(type(class_meta))" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "id": "938e3147", 116 | "metadata": { 117 | "id": "938e3147" 118 | }, 119 | "source": [ 120 | "#### Q3. How do class decorators overlap with metaclasses for handling classes ?\n", 121 | "**Ans:** Anything you can do with a class decorator, you can of course do with a custom metaclasses (just apply the functionality of the \"decorator function\", i.e., the one that takes a class object and modifies it, in the course of the metaclass's **`__new__`** or **`__init__`** that make the class object!)." 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "id": "3f57ffac", 127 | "metadata": { 128 | "id": "3f57ffac" 129 | }, 130 | "source": [ 131 | "#### Q4. How do class decorators overlap with metaclasses for handling instances?\n", 132 | "**Ans:** Anything you can do with a class decorator, you can of course do with a custom metaclass (just apply the functionality of the \"decorator function\", i.e., the one that takes a class object and modifies it, in the course of the metaclass's **`__new__`** or **`__init__`** that make the class object!)." 133 | ] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 3", 139 | "language": "python", 140 | "name": "python3" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 3 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython3", 152 | "version": "3.6.13" 153 | }, 154 | "colab": { 155 | "name": "Advance_Assignment_11.ipynb", 156 | "provenance": [], 157 | "collapsed_sections": [] 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 5 162 | } -------------------------------------------------------------------------------- /NLP/NLP_Assignment_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "NLP_Assignment_04.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "source": [ 22 | "**Submitted By: Gaurav Ratan**" 23 | ], 24 | "metadata": { 25 | "id": "WvuEP0kLpV8T" 26 | } 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "source": [ 31 | "Q1. Can you think of a few applications for a sequence-to-sequence RNN? What about a sequence-to-vector RNN? And a vector-to-sequence RNN?\n", 32 | "\n", 33 | "Ans: \n", 34 | "\n", 35 | "In Sequence to Sequence Learning, RNN is trained to map an input sequence to an output sequence which is not necessarily of the same length.\n", 36 | "\n", 37 | "Applications are speech recognition, machine translation, image captioning and question answering.\n", 38 | "\n", 39 | "A variable-length context vector can be used instead of a fixed-size vector. An Attention mechanism can be used to produces a sequence of vectors from the encoder RNN from each time step of the input sequence. The Decoder learns to pay selective attention to the vectors to produce the output at each time step.\n", 40 | "\n", 41 | "\n", 42 | "\n" 43 | ], 44 | "metadata": { 45 | "id": "-lko5IojpYrw" 46 | } 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "source": [ 51 | "Q2. Why do people use encoder–decoder RNNs rather than plain sequence-to-sequence RNNs for automatic translation?\n", 52 | "\n", 53 | "Ans:\n", 54 | "\n", 55 | "This two-step model, called an Encoder–Decoder, works much better than trying to translate on the fly with a single sequence-to-sequence RNN (like the one represented on the top left), since the last words of a sentence can affect the first words of the translation, so you need to wait until you have heard the whole sentence before translating it. Seq-2-seq RNNs translate one word at a time where as encoder-decoder RNNs read & translate a sentence at a time\n", 56 | "\n", 57 | "![0_V0GyOt3LoDVfY7y5.png](https://miro.medium.com/max/1400/1*ZPSeKkiXfxDHrpY0pgtAGQ.png)\n" 58 | ], 59 | "metadata": { 60 | "id": "UfvPugnWpk7-" 61 | } 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "source": [ 66 | "Q3. How could you combine a convolutional neural network with an RNN to classify videos?\n", 67 | "\n", 68 | "Ans:\n", 69 | "\n", 70 | "We can combine CNN and RNN.\n", 71 | "\n", 72 | "Each video is converted into sequential images and passed onto the CNN to extract spatial features. The outputs are then passed into a recurrent sequence learning model (i.e. LSTM) to identify temporal features within the image sequence.\n", 73 | "\n", 74 | "\n", 75 | "![0_V0GyOt3LoDVfY7y5.png](https://www.researchgate.net/profile/Maulik-Kamdar/publication/322167103/figure/fig5/AS:631611880124465@1527599415555/CRNN-Architecture-Overview-Combining-CNN-and-RNN-to-predict-the-methylation-state-from.png)\n" 76 | ], 77 | "metadata": { 78 | "id": "iPdzOnT8pk5f" 79 | } 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "source": [ 84 | "Q4. What are the advantages of building an RNN using dynamic_rnn() rather than static_rnn()?\n", 85 | "\n", 86 | "Ans:\n", 87 | "\n", 88 | "Below are the advantages of building an RNN using dynamic_rnn() rather than static_rnn():\n", 89 | "\n", 90 | "1. Avoids out-of-memory errors\n", 91 | "\n", 92 | "2. Directly takes single tensor as input and output (covering all time steps)\n", 93 | "\n", 94 | "3. No need to stack, unstack, or transpose\n", 95 | "\n", 96 | "4. Generates a smaller easier to visualize graph in TensorBoard\n", 97 | "\n", 98 | "\n", 99 | "\n", 100 | "\n" 101 | ], 102 | "metadata": { 103 | "id": "fVP_o59x6Cmi" 104 | } 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "source": [ 109 | "Q5. How can you deal with variable-length input sequences? What about variable-length output sequences?\n", 110 | "\n", 111 | "Ans:\n", 112 | "\n", 113 | "We can deal with varibale-length input sequences in the following ways:\n", 114 | "\n", 115 | "1. Set sequence_length parameter when calling static_rnn() or dynamic_rnn()\n", 116 | "\n", 117 | "2. Pad smaller input/output to make them same size as largest input/output\n", 118 | "\n", 119 | "\n", 120 | "\n", 121 | "\n" 122 | ], 123 | "metadata": { 124 | "id": "6V1aotumpk3C" 125 | } 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "source": [ 130 | "Q6. What is a common way to distribute training and execution of a deep RNN across multiple GPUs?\n", 131 | "Ans:\n", 132 | "\n", 133 | "A Common way to distribute training and execution of a deep RNN across multiple GPUs is to place each layer on a different GPU.\n", 134 | "\n", 135 | "![0_V0GyOt3LoDVfY7y5.png](https://www.tensorflow.org/images/Parallelism.png)\n", 136 | "\n" 137 | ], 138 | "metadata": { 139 | "id": "x3bo17oM6RGp" 140 | } 141 | } 142 | ] 143 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_20.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "57a871f3", 6 | "metadata": { 7 | "id": "57a871f3" 8 | }, 9 | "source": [ 10 | "# Assignment 20 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "T29eCLZ3AcI2" 20 | }, 21 | "id": "T29eCLZ3AcI2" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "05ee62b2", 26 | "metadata": { 27 | "id": "05ee62b2" 28 | }, 29 | "source": [ 30 | "#### 1. Compare and contrast the float and Decimal classes' benefits and drawbacks ?\n", 31 | "**Ans:** Both the `float` and `decimal` types store numerical values in Python.\n", 32 | "\n", 33 | "We use `floats` when convenience and speed matter. A float gives us an approximation of the number we declare.\n", 34 | "\n", 35 | "We use `decimals` when precision matters. `Decimals` can suffer from their own precision issues, but generally, `decimals` are more precise than `floats`. The performance difference between `float` and `decimal`, with Python 3, is not outlandish, and in my experience, the precision benefits of a `decimal` outweigh the performance benefits of a `float`." 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "fd433d14", 41 | "metadata": { 42 | "id": "fd433d14" 43 | }, 44 | "source": [ 45 | "#### 2. Decimal('1.200') and Decimal('1.2') are two objects to consider. In what sense are these the same object? Are these just two ways of representing the exact same value, or do they correspond to different internal states ?\n", 46 | "**Ans:** Both values are same but internal representation at storage Is different. Precsion differs, `Decimal('1.200')` gives internally `1.200` and `Decimal('1.2')` gives `1.2`." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "efc3efad", 52 | "metadata": { 53 | "id": "efc3efad" 54 | }, 55 | "source": [ 56 | "#### 3. What happens if the equality of Decimal('1.200') and Decimal('1.2') is checked ?\n", 57 | "**Ans:** Both values are checked to be equal since they only differ in precision." 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "id": "8f6cda09", 63 | "metadata": { 64 | "id": "8f6cda09" 65 | }, 66 | "source": [ 67 | "#### 4. Why is it preferable to start a Decimal object with a string rather than a floating-point value ?\n", 68 | "**Ans:** Floating-point value is converted to Decimal format. Decimal can store float value with absolute precision. But when float value is given as Decimal object,it first has to be converted from floating point value which might already have rounding error.\n", 69 | "\n", 70 | "Hence it is preferable to start a Decimal object with a string." 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "id": "2e42c0a6", 76 | "metadata": { 77 | "id": "2e42c0a6" 78 | }, 79 | "source": [ 80 | "#### 5. In an arithmetic phrase, how simple is it to combine Decimal objects with integers ?\n", 81 | "**Ans:** We can do it with use of `Decimal()`." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "6a9cf124", 87 | "metadata": { 88 | "id": "6a9cf124" 89 | }, 90 | "source": [ 91 | "#### 6. Can Decimal objects and floating-point values be combined easily ?\n", 92 | "**Ans:** Arithmetic operfations like Adding,subtracting or multiplying a Decimal object by a floating-point value generates an error.\n", 93 | "\n", 94 | "To do these operations, the floating point has to be converted to a Decimal." 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "860d14ca", 100 | "metadata": { 101 | "id": "860d14ca" 102 | }, 103 | "source": [ 104 | "#### 7. Using the Fraction class but not the Decimal class, give an example of a quantity that can be expressed with absolute precision ?\n", 105 | "**Ans:** Value of `0.5` will be represented as `½`." 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "6702505a", 111 | "metadata": { 112 | "id": "6702505a" 113 | }, 114 | "source": [ 115 | "#### 8.Consider the following two fraction objects: Fraction(1, 2) and Fraction(1, 2). (5, 10). Is the internal state of these two objects the same? Why do you think that is ?\n", 116 | "**Ans:** Both will be reduced to `1/2`" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "id": "1039ed6b", 122 | "metadata": { 123 | "id": "1039ed6b" 124 | }, 125 | "source": [ 126 | "#### 9. How do the Fraction class and the integer type (int) relate to each other? Containment or inheritance ?\n", 127 | "**Ans:** Fraction class and integer type(int) are related in form of a container. It contains two ints, one in the numerator and the other in the denominator" 128 | ] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 3", 134 | "language": "python", 135 | "name": "python3" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 3 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython3", 147 | "version": "3.8.8" 148 | }, 149 | "colab": { 150 | "name": "Advance_Assignment_20.ipynb", 151 | "provenance": [], 152 | "collapsed_sections": [] 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 5 157 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_08.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ce6590a1", 6 | "metadata": { 7 | "id": "ce6590a1" 8 | }, 9 | "source": [ 10 | "# Assignment 08 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "bhHOMHb-ydzh" 20 | }, 21 | "id": "bhHOMHb-ydzh" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "efb72245", 26 | "metadata": { 27 | "id": "efb72245" 28 | }, 29 | "source": [ 30 | "#### Q1. What are the two latest user-defined exception constraints in Python 3.X?\n", 31 | "**Ans:** **`raise`** and **`assert`** are the two latest user-defined exception constraints in Python 3.X" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "f1479448", 37 | "metadata": { 38 | "id": "f1479448" 39 | }, 40 | "source": [ 41 | "#### Q2. How are class-based exceptions that have been raised matched to handlers?\n", 42 | "**Ans:** In python, Users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly from built-in **`Exception`** class. This new exception class like other exceptions can be raised using the **`raise`** statement with an optional error message." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "id": "648a981d", 49 | "metadata": { 50 | "id": "648a981d", 51 | "outputId": "4a465c96-6dd1-4904-906e-6603a4085b8c" 52 | }, 53 | "outputs": [ 54 | { 55 | "ename": "ToYoungException", 56 | "evalue": "To Young For Voting", 57 | "output_type": "error", 58 | "traceback": [ 59 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 60 | "\u001b[1;31mToYoungException\u001b[0m Traceback (most recent call last)", 61 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmsg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m12\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mage\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m18\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mToYoungException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'To Young For Voting'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 62 | "\u001b[1;31mToYoungException\u001b[0m: To Young For Voting" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "class ToYoungException(Exception):\n", 68 | " def __init__(self,msg):\n", 69 | " self.msg = msg\n", 70 | "age = 12\n", 71 | "if age <= 18: raise ToYoungException('To Young For Voting')" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "id": "15ce7b10", 77 | "metadata": { 78 | "id": "15ce7b10" 79 | }, 80 | "source": [ 81 | "#### Q3. Describe two methods for attaching context information to exception artefacts ?\n", 82 | "**Ans:** The **`process()`** method of LoggerAdapter is where the contextual information is added to the logging output. its passes the message and keyword arguments of the logging call, and it passes back modified versions of these to use in the call to the underlying logger.\n", 83 | "\n", 84 | "Other method that can be used is **`exception()`**, Logs a messgae with level ERROR on this logger. The arguments are interpreted as for **`debug()`**. Exception info is added to the logging message." 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "id": "7fbfd775", 90 | "metadata": { 91 | "id": "7fbfd775" 92 | }, 93 | "source": [ 94 | "#### Q4. Describe two methods for specifying the text of an exception object's error message ?\n", 95 | "**Ans:** **`raise`** and **`assert`** are two methods for specifying the text of an exception object's error message.**`raise`** statement is used to trigger explicit exception, if certain condition is not as per requirement of programmer. it helps in triggering exception as per need of programmer and logic.\n", 96 | "\n", 97 | "There are few assertions that programmer always want to be True to avoid code failure. This type of requirment is fullfilled by **`assert`** statement. This statement takes a Boolean Condition output of which if True, further program executes. if output of assert statement is False it raises an **`Assertion Error`**." 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "0855de9b", 103 | "metadata": { 104 | "id": "0855de9b" 105 | }, 106 | "source": [ 107 | "#### Q5. Why do you no longer use string-based exceptions?\n", 108 | "**Ans:** String-based Exceptions doesn't inherit from Exceptions. so plain exceptions catch all exceptions and not only system." 109 | ] 110 | } 111 | ], 112 | "metadata": { 113 | "kernelspec": { 114 | "display_name": "Python 3", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.6.13" 129 | }, 130 | "colab": { 131 | "name": "Advance_Assignment_08.ipynb", 132 | "provenance": [] 133 | } 134 | }, 135 | "nbformat": 4, 136 | "nbformat_minor": 5 137 | } -------------------------------------------------------------------------------- /Python/Basic Python/Python_Basics_Assignment_14.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "bb28039f", 6 | "metadata": { 7 | "id": "bb28039f" 8 | }, 9 | "source": [ 10 | "# Assignment 14 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "C-1Hr2H6BLod" 20 | }, 21 | "id": "C-1Hr2H6BLod" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "7c6c3412", 26 | "metadata": { 27 | "id": "7c6c3412" 28 | }, 29 | "source": [ 30 | "#### 1. What does RGBA stand for?\n", 31 | "**Ans:** **RGBA** is a four-channel format containing data for `Red`, `Green`, `Blue`, and an `Alpha` value. Where Alpha Represents the Opacity. Each value is a floating point number between 0.0 and 1.0. For example, the tuple (1, 0, 0, 1) represents an opaque red, while (0, 1, 0, 0.5) represents a half transparent green." 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "39e9a315", 37 | "metadata": { 38 | "id": "39e9a315" 39 | }, 40 | "source": [ 41 | "#### 2. From the Pillow module, how do you get the RGBA value of any images?\n", 42 | "**Ans:** **`ImageColor.getcolor()`** gives rgba value of any image " 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "id": "185bf687", 48 | "metadata": { 49 | "id": "185bf687" 50 | }, 51 | "source": [ 52 | "#### 3. What is a box tuple, and how does it work?\n", 53 | "**Ans:** A box tuple is a tuple value of four integers: the left-edge x-coordinate, the top-edge y-coordinate,the width, and the height, respectively." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "id": "2dd0185d", 59 | "metadata": { 60 | "id": "2dd0185d" 61 | }, 62 | "source": [ 63 | "#### 4. Use your image and load in notebook then, How can you find out the width and height of an Image object?" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "id": "1b9e60eb", 70 | "metadata": { 71 | "id": "1b9e60eb", 72 | "outputId": "00a33d40-f481-4ec0-b839-ec156dd1cc2c" 73 | }, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "Width, Height -> (287, 70)\n", 80 | "Width, Height -> 287,70\n", 81 | "Width, Height -> 287,70\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "#Example Program\n", 87 | "from PIL import Image\n", 88 | "pic = Image.open('Pic.jpg')\n", 89 | "print(f'Width, Height -> {pic.size}') # Approach 1\n", 90 | "print(f'Width, Height -> {pic.width},{pic.height}') # Approach 2\n", 91 | "width,height = pic.size\n", 92 | "print(f'Width, Height -> {width},{height}') # Approach 3" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "id": "efc289ab", 98 | "metadata": { 99 | "id": "efc289ab" 100 | }, 101 | "source": [ 102 | "#### 5. What method would you call to get Image object for a 100×100 image, excluding the lower-left quarter of it?" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "id": "4a82a38a", 109 | "metadata": { 110 | "id": "4a82a38a" 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "from PIL import Image\n", 115 | "img = Image.open('Pic.jpg')\n", 116 | "new_img = img.crop((0,50,50,50))" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "id": "0ffd911b", 122 | "metadata": { 123 | "id": "0ffd911b" 124 | }, 125 | "source": [ 126 | "#### 6. After making changes to an Image object, how could you save it as an image file?" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "id": "e5939016", 133 | "metadata": { 134 | "id": "e5939016" 135 | }, 136 | "outputs": [], 137 | "source": [ 138 | "#Example Program\n", 139 | "from PIL import Image\n", 140 | "pic = Image.open('pic.jpg')\n", 141 | "pic.save('pic2.jpg')" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "5ad56500", 147 | "metadata": { 148 | "id": "5ad56500" 149 | }, 150 | "source": [ 151 | "#### 7. What module contains Pillow’s shape-drawing code?\n", 152 | "**Ans:** Pillows **`ImageDraw`** module contains Shape drawing methods. The 'ImageDraw' module provides simple 2D graphics support for Image Object. Generally, we use this module to create new images, annotate or retouch existing images and to generate graphics on the fly for web use. The graphics commands support the drawing of shapes and annotation of text." 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "2a09c289", 158 | "metadata": { 159 | "id": "2a09c289" 160 | }, 161 | "source": [ 162 | "#### 8. Image objects do not have drawing methods. What kind of object does? How do you get this kind of object?\n", 163 | "**Ans:** ImageDraw objects have shape-drawing methods such as `point()`, `line()`, or `rectangle()`.They are returned by passing the Image object to the `ImageDraw.Draw()` function." 164 | ] 165 | } 166 | ], 167 | "metadata": { 168 | "kernelspec": { 169 | "display_name": "Python 3", 170 | "language": "python", 171 | "name": "python3" 172 | }, 173 | "language_info": { 174 | "codemirror_mode": { 175 | "name": "ipython", 176 | "version": 3 177 | }, 178 | "file_extension": ".py", 179 | "mimetype": "text/x-python", 180 | "name": "python", 181 | "nbconvert_exporter": "python", 182 | "pygments_lexer": "ipython3", 183 | "version": "3.8.8" 184 | }, 185 | "colab": { 186 | "name": "Python Basics Assignment_14.ipynb", 187 | "provenance": [], 188 | "collapsed_sections": [] 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 5 193 | } -------------------------------------------------------------------------------- /Python/Basic Python/Python_Basics_Assignment_10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c750c84b", 6 | "metadata": { 7 | "id": "c750c84b" 8 | }, 9 | "source": [ 10 | "# Assignment 10 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "rjyDcrt4no1T" 20 | }, 21 | "id": "rjyDcrt4no1T" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "c00ae223", 26 | "metadata": { 27 | "id": "c00ae223" 28 | }, 29 | "source": [ 30 | "#### 1. How do you distinguish between shutil.copy() and shutil.copytree()?\n", 31 | "**Ans:** `shutil.copy()` method is used to copy the contents of a file from one file to another file/folder. It primarily takes two arguments `src` and `dest`. `src` represents the file to be copied where as destination refers to the file/folder to where the `src` data should be copied. If `dest` is a folder name the `src` with exact name will be copied to the `dest` folder whereas if its a file then the contents of `src` will be copied to `dest` where `dest` retains it name.\n", 32 | "\n", 33 | "`shutil.copytree()` function is used to copy the entire contents of a folder to other folder. It also takes two arguments `src` & `dest`. It copies all the content recursively and stores it in `dest`. The important point to note here is that `dest` must not exist prior to this and it will be created during the copy operation. Permissions and times of directories are copied with `shutil.copystat()` and individual files are copied using `shutil.copy2()` by default which can be modified using `copy_function` attribute.\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "0a932861", 39 | "metadata": { 40 | "id": "0a932861" 41 | }, 42 | "source": [ 43 | "#### 2. What function is used to rename files??\n", 44 | "**Ans:** `os.rename()` function is used to rename files or directories using a python program. this function takes two arguments `src` and `dest`. `src` represents the name file/directory which we want to rename, whereas `dest` represents the new name of the file/directory." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "34ce7a3d", 50 | "metadata": { 51 | "id": "34ce7a3d" 52 | }, 53 | "source": [ 54 | "#### 3. What is the difference between the delete functions in the send2trash and shutil modules?\n", 55 | "**Ans:** Shutil module provides a funciton called as `shutil.rmtree()` which deletes a directory and all of its contents. The other functions with similar functionality are `os.remove()` which removes a file. `os.rmdir()` removes a empty directory. The problem with these functions is once a file is deleted. it will be lost permanently and if a file is deleted accidentally using these methods there is no way we can recover the deleted file\n", 56 | "\n", 57 | "Where as send2trash module provides a function called `send2trash.send2trash()` to delete a file/directory. These methods moves the files/directories to trash folder instead of permanently deleting them. Hence if a file/folder is deleted accidentally using the `send2trash.send2trash()` it can be still recovered from trash folder. `send2trash` is not included with python standard libary like `os` & `shutil` modules. So, it needs to be installed explicitly using the command `!pip install send2trash`" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "id": "bf6ed792", 63 | "metadata": { 64 | "id": "bf6ed792" 65 | }, 66 | "source": [ 67 | "#### 4.ZipFile objects have a close() method just like File objects’ close() method. What ZipFile method is equivalent to File objects’ open() method?\n", 68 | "**Ans:** ZipFile Module provides a method called as `zipfile.ZipFile()` to read and write to zipFiles. it takes arugments such as filename and mode etc `zipfile.ZipFile('filename', mode = 'r')`" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "c625fcea", 74 | "metadata": { 75 | "id": "c625fcea" 76 | }, 77 | "source": [ 78 | "#### 5. Create a programme that searches a folder tree for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "id": "4710d7d8", 85 | "metadata": { 86 | "id": "4710d7d8" 87 | }, 88 | "outputs": [], 89 | "source": [ 90 | "import os\n", 91 | "import shutil\n", 92 | "\n", 93 | "def search_copy(source,destination,extensions):\n", 94 | " source = os.path.abspath(source)\n", 95 | " destination = os.path.abspath(destination)\n", 96 | " for foldername, subfolder, filenames in os.walk(source):\n", 97 | " print(f'Folder Name ➞ {foldername}',end='\\n\\n')\n", 98 | " print(f'Sub Folders ➞ {subfolder}',end='\\n\\n')\n", 99 | " print(f'Files ➞ {filenames}',end='\\n\\n')\n", 100 | " for filename in filenames:\n", 101 | " fileName,extension = os.path.splitext(filename)\n", 102 | " if extension in extensions:\n", 103 | " targetFile = foldername+os.path.sep+fileName+extension\n", 104 | " shutil.copy(targetFile, destination)\n", 105 | " print(f'Files copied successfully from {source} to {destination}')\n", 106 | " \n", 107 | "extensions = ['.pdf','.jpg','.ipynb']\n", 108 | "source = 'Source Folder'\n", 109 | "destination = 'Destination Folder'\n", 110 | "search_copy(source, destination, extensions)" 111 | ] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.8.8" 131 | }, 132 | "colab": { 133 | "name": "Python Basics Assignment_10.ipynb", 134 | "provenance": [], 135 | "collapsed_sections": [] 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 5 140 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_21.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "995281bf", 6 | "metadata": { 7 | "id": "995281bf" 8 | }, 9 | "source": [ 10 | "# Assignment 21 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "LLZGHXhOQW7u" 20 | }, 21 | "id": "LLZGHXhOQW7u" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "65a92b4a", 26 | "metadata": { 27 | "id": "65a92b4a" 28 | }, 29 | "source": [ 30 | "#### Q1. What is a probability distribution, exactly? If the values are meant to be random, how can you predict them at all ?\n", 31 | "**Ans:** A probability distribution describes how a random variable is distributed.\n", 32 | "\n", 33 | "It tells us which values a random variable is most likely to take on and which values are less likely. Based on the previous data and the occurences of the random event, we can predict the outcome in terms of probabilities. " 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "06c8734d", 39 | "metadata": { 40 | "id": "06c8734d" 41 | }, 42 | "source": [ 43 | "#### Q2. Is there a distinction between true random numbers and pseudo-random numbers, if there is one? Why are the latter considered “good enough”?\n", 44 | "**Ans:** : True Random Numbers are true physical values while pseudo-random numbers are generated internally by a program . Latter is considered good enough as they have sort of uniform distribution. Pseudo random numbers are generated by computers using an algorithm (there are many) and a seed (either chosen by the user or, sometimes, clock time). They are not strictly random as, if you start with the same seed and same algorithm. A truly random number, on the other hand, is completely unpredictable. There is no way to generate a series of truly random numbers via computer; the usual ways are flipping coins, throwing darts, rolling dice and other physical processes." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "1e19043f", 50 | "metadata": { 51 | "id": "1e19043f" 52 | }, 53 | "source": [ 54 | "#### Q3. What are the two main factors that influence the behaviour of a \"normal\" probability distribution ?\n", 55 | "**Ans:** Normal Probability Distribution are influenced by two factors: Mean and Variance. " 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "id": "e75110f5", 61 | "metadata": { 62 | "id": "e75110f5" 63 | }, 64 | "source": [ 65 | "#### Q4. Provide a real-life example of a normal distribution ?\n", 66 | "**Ans:** A Real-life example of a normal distribution are heights. Height of the population is the example of normal distribution. Most of the people in a specific population are of average height. The number of people taller and shorter than the average height people is almost equal, and a very small number of people are either extremely tall or extremely short. " 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "eba32f70", 72 | "metadata": { 73 | "id": "eba32f70" 74 | }, 75 | "source": [ 76 | "#### Q5. In the short term, how can you expect a probability distribution to behave? What do you think will happen as the number of trials grows ?\n", 77 | "**Ans:** Variance will decrease with number of trials. In probability theory and statistics, a probability distribution is the mathematical function that gives the probabilities of occurrence of different possible outcomes for an experiment.It is a mathematical description of a random phenomenon in terms of its sample space and the probabilities of events (subsets of the sample space)." 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "id": "82eba016", 83 | "metadata": { 84 | "id": "82eba016" 85 | }, 86 | "source": [ 87 | "#### Q6. What kind of object can be shuffled by using random.shuffle ?\n", 88 | "**Ans:** lists (`list`), strings (`str`) and tuples (`tuple`) objects can be shuffled by using `random.shuffle`." 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "id": "c1ba9930", 94 | "metadata": { 95 | "id": "c1ba9930" 96 | }, 97 | "source": [ 98 | "#### Q7. Describe the math package's general categories of functions ?\n", 99 | "**Ans:** The Math package's general categories of functions are:\n", 100 | "1. Trigonometric functions\n", 101 | "2. Quadratic functions\n", 102 | "3. Exponential functions\n", 103 | "4. Hyperbolic functions\n", 104 | "5. Periodic functions\n", 105 | "6. Arithmetic functions\n", 106 | "7. Logarithimic functions\n", 107 | "8. Conversions to Integer" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "id": "cbab2356", 113 | "metadata": { 114 | "id": "cbab2356" 115 | }, 116 | "source": [ 117 | "#### Q8. What is the relationship between exponentiation and logarithms ?\n", 118 | "**Ans:** Exponentiation and logarithms are inverse of each other. " 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "id": "43787cec", 124 | "metadata": { 125 | "id": "43787cec" 126 | }, 127 | "source": [ 128 | "#### Q9. What are the three logarithmic functions that Python supports ?\n", 129 | "**Ans:** The Three Logarithmic Functions that Python supports are:\n", 130 | "1. `log2(x)` - logarithmic value of x to base 2\n", 131 | "2. `log10(x)` - logarithmic value of x to base 10 \n", 132 | "3. `log1p(a)` - This function is used to compute logarithm(1+a) . " 133 | ] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 3", 139 | "language": "python", 140 | "name": "python3" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 3 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython3", 152 | "version": "3.8.8" 153 | }, 154 | "colab": { 155 | "name": "Advance_Assignment_21.ipynb", 156 | "provenance": [], 157 | "collapsed_sections": [] 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 5 162 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f539b01e", 6 | "metadata": { 7 | "id": "f539b01e" 8 | }, 9 | "source": [ 10 | "# Assignment 01 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN\n" 17 | ], 18 | "metadata": { 19 | "id": "5TcFy5FcglDi" 20 | }, 21 | "id": "5TcFy5FcglDi" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "942401c5", 26 | "metadata": { 27 | "id": "942401c5" 28 | }, 29 | "source": [ 30 | "#### Q1. What is the purpose of Python's OOP?\n", 31 | "**Ans:** Object-oriented programming is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects\n", 32 | "- In Python, object-oriented Programming (OOPs) uses objects and classes in programming. \n", 33 | "- It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming.\n", 34 | "- The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data. \n", 35 | "\n", 36 | "Some Advantages are mentioned below:\n", 37 | "- It helps to divide our over all program into different small segments and thus making problem solving easy through the use of objects\n", 38 | "- Helps in easy maintenance and modification of existing program\n", 39 | "- Multiple instances of an object can be made and explored." 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "id": "7cfcf3e9", 45 | "metadata": { 46 | "id": "7cfcf3e9" 47 | }, 48 | "source": [ 49 | "#### Q2. Where does an inheritance search look for an attribute?\n", 50 | "**Ans:** Python searches for an attribute in an upward tree of attributes. Tt first searches for the attribute in its instance and then looks in the class it is generated from, to all super classes listed in its class header" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "096d7b0b", 56 | "metadata": { 57 | "id": "096d7b0b" 58 | }, 59 | "source": [ 60 | "#### Q3. How do you distinguish between a class object and an instance object?\n", 61 | "**Ans:** The differences between a class object and an instance object are:\n", 62 | "1. Class is a template for creating objects whereas object is an instance of class\n", 63 | "2. Seperate memory is allocated for each object whenever an object is created. but for a class this doesnot happens.\n", 64 | "3. A Class is created once. Many objects are created using a class.\n", 65 | "4. As Classes have no allocated memory. they can't be manipulated. but objects can be manipulated." 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "id": "fd8cc6bc", 71 | "metadata": { 72 | "id": "fd8cc6bc" 73 | }, 74 | "source": [ 75 | "#### Q4. What makes the first argument in a class’s method function special?\n", 76 | "**Ans:** Python Classes usually have three types of methods which are:\n", 77 | "- Instance Methods (object level methods)\n", 78 | "- Class Methods (class level methods)\n", 79 | "- Static Methods (general utility methods)\n", 80 | "- **`self`** is the first argument for instance methods. which refers to the object itself\n", 81 | "- **`cls`** is the first argument for class methods which refers to the class itself" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "94de0426", 87 | "metadata": { 88 | "id": "94de0426" 89 | }, 90 | "source": [ 91 | "#### Q5. What is the purpose of the __init__ method?\n", 92 | "**Ans:** **`__init__`** is a reseved method in python classes. It serves the role of a **constructor** in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "id": "3e47943f", 98 | "metadata": { 99 | "id": "3e47943f" 100 | }, 101 | "source": [ 102 | "#### Q6. What is the process for creating a class instance?\n", 103 | "**Ans:** To create a class instance, we need to call the class by its name and pass the arguments to the class, which its **`init`** method accepts.\n", 104 | "\n", 105 | "**Example:** **`my_name = my_class(\"Ratan\",\"Gaurav\")`** Here `my_name` is an instance of class `my_class` with attributes \"Ratan\" and \"Gaurav\"." 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "id": "8ddf33a9", 111 | "metadata": { 112 | "id": "8ddf33a9" 113 | }, 114 | "source": [ 115 | "#### Q7. What is the process for creating a class?\n", 116 | "**Ans:** **`class`** keyword is used to created a class in python. The syntax to create a class in python is **`class :`**\n", 117 | "\n", 118 | "**Example:** **`class Shape:`** ➞ this creates a class called Shape" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "id": "b58c55d6", 124 | "metadata": { 125 | "id": "b58c55d6" 126 | }, 127 | "source": [ 128 | "#### Q8. How would you define the superclasses of a class?\n", 129 | "**Ans** Superclass/Parent class is given as a arugment to the child class\n", 130 | "\n", 131 | "**Example:** `class Employee(Person):` Here child class `Employee` inherits attributes and methods/functions from Superclass/Parent `Person`" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "source": [ 137 | "" 138 | ], 139 | "metadata": { 140 | "id": "wbizLsJMhilE" 141 | }, 142 | "id": "wbizLsJMhilE", 143 | "execution_count": null, 144 | "outputs": [] 145 | } 146 | ], 147 | "metadata": { 148 | "kernelspec": { 149 | "display_name": "Python 3", 150 | "language": "python", 151 | "name": "python3" 152 | }, 153 | "language_info": { 154 | "codemirror_mode": { 155 | "name": "ipython", 156 | "version": 3 157 | }, 158 | "file_extension": ".py", 159 | "mimetype": "text/x-python", 160 | "name": "python", 161 | "nbconvert_exporter": "python", 162 | "pygments_lexer": "ipython3", 163 | "version": "3.8.8" 164 | }, 165 | "colab": { 166 | "name": "Advance_Assignment_01.ipynb", 167 | "provenance": [], 168 | "collapsed_sections": [] 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 5 173 | } -------------------------------------------------------------------------------- /NLP/NLP_Assignment_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "NLP_Assignment_02.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "source": [ 21 | "**Submitted By: Gaurav Ratan**" 22 | ], 23 | "metadata": { 24 | "id": "WvuEP0kLpV8T" 25 | } 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "source": [ 30 | "Q1. What are Corpora?\n", 31 | "\n", 32 | "Ans: \n", 33 | "\n", 34 | "A corpus is a collection of authentic text or audio organized into datasets. Authentic here means text written or audio spoken by a native of the language or dialect. A corpus can be made up of everything from newspapers, novels, recipes, radio broadcasts to television shows, movies, and tweets.\n", 35 | "\n", 36 | "How it is done-->NLTK already defines a list of data paths or directories in nltk.data.path. Our custom corpora must be present within any of these given paths so it can be found by NLTK. We can also create a custom nltk_data directory in our home directory and verify that it is in the list of known paths specified by nltk.data.path." 37 | ], 38 | "metadata": { 39 | "id": "-lko5IojpYrw" 40 | } 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "source": [ 45 | "Q2. What are Tokens?\n", 46 | "\n", 47 | "Ans:\n", 48 | "\n", 49 | "Tokens are the building blocks of Natural Language. Tokenization is a way of separating a piece of text into smaller units called tokens. Here, tokens can be either words, characters, or subwords. Hence, tokenization can be broadly classified into 3 types – word, character, and subword (n-gram characters) tokenization" 50 | ], 51 | "metadata": { 52 | "id": "UfvPugnWpk7-" 53 | } 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "source": [ 58 | "Q3. What are Unigrams, Bigrams, Trigrams?\n", 59 | "\n", 60 | "Ans:\n", 61 | "\n", 62 | "A 1-gram (or unigram) is a one-word sequence. For the above sentence, the unigrams would simply be: “I”, “love”, “reading”, “blogs”, “about”, “data”, “science”, “on”, “Medium”, “Channel”.\n", 63 | "\n", 64 | "A 2-gram (or bigram) is a two-word sequence of words, like “I love”, “love reading”, or “Medium Channel”.\n", 65 | "\n", 66 | "And a 3-gram (or trigram) is a three-word sequence of words like “I love reading”, “about data science” .\n" 67 | ], 68 | "metadata": { 69 | "id": "iPdzOnT8pk5f" 70 | } 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "source": [ 75 | "Q4. How to generate n-grams from text?\n", 76 | "\n", 77 | "Ans:\n", 78 | "\n", 79 | "The Extract N-Gram Features from Text module creates two types of output: Results dataset: A summary of the analyzed text together with the n-grams that were extracted. Columns that you did not select in the Text column option are passed through to the output" 80 | ], 81 | "metadata": { 82 | "id": "fVP_o59x6Cmi" 83 | } 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "source": [ 88 | "Q5. Explain Lemmatization\n", 89 | "\n", 90 | "Ans:\n", 91 | "\n", 92 | "Lemmatization is a text normalization technique used in Natural Language Processing (NLP). It has been studied for a very long time and lemmatization algorithms have been made since the 1960s. Essentially, lemmatization is a technique that switches any kind of a word to its base root mode. It is a linguistic term that means grouping together words with the same root or lemma but with different inflections or derivatives of meaning so they can be analyzed as one item. The aim is to take away inflectional suffixes and prefixes to bring out the word's dictionary form." 93 | ], 94 | "metadata": { 95 | "id": "6V1aotumpk3C" 96 | } 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "source": [ 101 | "Q6. Explain Stemming\n", 102 | "\n", 103 | "Ans:\n", 104 | "\n", 105 | "In linguistic morphology and information retrieval, stemming is the process of reducing inflected (or sometimes derived) words to their word stem, base or root form—generally a written word form. ... A computer program or subroutine that stems word may be called a stemming program, stemming algorithm, or stemmer. \n", 106 | "\n", 107 | "Stemming is the process of reducing a word to its word stem that affixes to suffixes and prefixes or to the roots of words known as a lemma. Stemming is important in natural language understanding (NLU) and natural language processing (NLP)." 108 | ], 109 | "metadata": { 110 | "id": "x3bo17oM6RGp" 111 | } 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "source": [ 116 | "Q7. Explain Part-of-speech (POS) tagging\n", 117 | "\n", 118 | "Ans:\n", 119 | "\n", 120 | "Part-of-speech (POS) tagging is a popular Natural Language Processing process which refers to categorizing words in a text (corpus) in correspondence with a particular part of speech, depending on the definition of the word and its context." 121 | ], 122 | "metadata": { 123 | "id": "71a4xcTf6REX" 124 | } 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "source": [ 129 | "Q8. Explain Chunking or shallow parsing\n", 130 | "\n", 131 | "Ans:\n", 132 | "\n", 133 | "Shallow parsing (also chunking or light parsing) is an analysis of a sentence which first identifies constituent parts of sentences (nouns, verbs, adjectives, etc.) and then links them to higher order units that have discrete grammatical meanings (noun groups or phrases, verb groups, etc.)." 134 | ], 135 | "metadata": { 136 | "id": "h1TZjXWc6RCI" 137 | } 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "source": [ 142 | "Q9. Explain Noun Phrase (NP) chunking\n", 143 | "\n", 144 | "Ans:\n", 145 | "\n", 146 | "NP is the user-defined name of the chunk you are searching for. In this case NP stands for noun phrase." 147 | ], 148 | "metadata": { 149 | "id": "aAn1HoO86Q_j" 150 | } 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "source": [ 155 | "Q10. Explain Named Entity Recognition\n", 156 | "\n", 157 | "Ans:\n", 158 | "\n", 159 | "Named entity recognition (NER) — sometimes referred to as entity chunking, extraction, or identification — is the task of identifying and categorizing key information (entities) in text. ... NER is a form of natural language processing (NLP), a subfield of artificial intelligence." 160 | ], 161 | "metadata": { 162 | "id": "2aqC8dXA6Q9M" 163 | } 164 | } 165 | ] 166 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "56306a9b", 6 | "metadata": { 7 | "id": "56306a9b" 8 | }, 9 | "source": [ 10 | "# Assignment 07 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "6qGlTFvUvW9s" 20 | }, 21 | "id": "6qGlTFvUvW9s" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "890ac6ef", 26 | "metadata": { 27 | "id": "890ac6ef" 28 | }, 29 | "source": [ 30 | "#### Q1. What is the purpose of the try statement?\n", 31 | "**Ans:** **`try`** statement is used for exception handling in python. It consists of a block of risky statements which might cause an exception during runtime. If code within try block raises exception, then the exception will be reverted to the corresponding except block when multiple except blocks are present, else it will be reverted to the default except block. In short, try and except in union avoid programs from crashing during runtime due to exceptions." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "id": "72d89888", 38 | "metadata": { 39 | "colab": { 40 | "base_uri": "https://localhost:8080/" 41 | }, 42 | "id": "72d89888", 43 | "outputId": "5a9e235e-21b5-46bf-9e5d-b6d837473bdc" 44 | }, 45 | "outputs": [ 46 | { 47 | "output_type": "stream", 48 | "name": "stdout", 49 | "text": [ 50 | "Division by Zero is Not Possible\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "try:\n", 56 | " print(30/0)\n", 57 | "except ZeroDivisionError:\n", 58 | " print('Division by Zero is Not Possible')" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "id": "4edfb2b6", 64 | "metadata": { 65 | "id": "4edfb2b6" 66 | }, 67 | "source": [ 68 | "#### Q2. What are the two most popular try statement variations?\n", 69 | "**Ans:** The Popular try statement variations are:\n", 70 | "1. **`try`,`except`**\n", 71 | "2. **`try`,`except`,`finally`**\n", 72 | "3. **`try`,`except`,`finally`,`else`**\n", 73 | "\n", 74 | "The **`try`** block is used to check code for exceptions during runtime. i.e. code inside `try` block will execute completely when there is no error in the program where as the code inside `except` block will execute whenever the program encounters some error in the preceeding **`try`** block.\n", 75 | "\n", 76 | "Where as the code enters the **`else`** block if and only if the try clause does not raise an exception. The code in the **`finally`** block will execute irrespective of exception." 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "c39b56b0", 82 | "metadata": { 83 | "id": "c39b56b0" 84 | }, 85 | "source": [ 86 | "#### Q3. What is the purpose of the raise statement?\n", 87 | "**Ans:** **`raise`** statement is used to trigger an exception explicitly, if a certain condition is not as per requirement of programmer. **`raise`** statement helps in triggering exception as per programming logic." 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "id": "2ea68550", 93 | "metadata": { 94 | "id": "2ea68550" 95 | }, 96 | "source": [ 97 | "#### Q4. What does the assert statement do, and what other statement is it like?\n", 98 | "**Ans:** There are few assertions that programmer always want to be true to avoid code failure. This type of requirement is fulfilled by **`assert`** statement. This statement takes a boolean condition output of which is True which in turn helps Further Program Execute. If output of assert statement is False, it raises an Assertion Error." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "id": "6df3fe6c", 105 | "metadata": { 106 | "colab": { 107 | "base_uri": "https://localhost:8080/", 108 | "height": 168 109 | }, 110 | "id": "6df3fe6c", 111 | "outputId": "a14d7e58-f242-4500-ae7e-d1bad17d5cbc" 112 | }, 113 | "outputs": [ 114 | { 115 | "output_type": "error", 116 | "ename": "AssertionError", 117 | "evalue": "ignored", 118 | "traceback": [ 119 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 120 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 121 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34mf'this is not a valid statement'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 122 | "\u001b[0;31mAssertionError\u001b[0m: this is not a valid statement" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "assert(2 == 10), f'this is not a valid statement'" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "1cc50d65", 133 | "metadata": { 134 | "id": "1cc50d65" 135 | }, 136 | "source": [ 137 | "#### Q5. What is the purpose of the with/as argument, and what other statement is it like?\n", 138 | "**Ans:** **`with/as`** statement simplifies use of file handling in python. When we use a **`with`** statement for file reading, there is no need for programmer to explicitly take care of activities like resource deallocation and file closing by using file.close() method. **`with`** statement itself ensures proper acquisition and release of resources. This avoids triggering of exceptions if the file closing is unknowingly forgotten in the code execution.\n", 139 | "\n", 140 | "`with open('sample_file.txt','w') as file:\n", 141 | " file.write('Hello World')`" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "source": [ 147 | "" 148 | ], 149 | "metadata": { 150 | "id": "pN-hE0Inu2sA" 151 | }, 152 | "id": "pN-hE0Inu2sA", 153 | "execution_count": null, 154 | "outputs": [] 155 | } 156 | ], 157 | "metadata": { 158 | "kernelspec": { 159 | "display_name": "Python 3", 160 | "language": "python", 161 | "name": "python3" 162 | }, 163 | "language_info": { 164 | "codemirror_mode": { 165 | "name": "ipython", 166 | "version": 3 167 | }, 168 | "file_extension": ".py", 169 | "mimetype": "text/x-python", 170 | "name": "python", 171 | "nbconvert_exporter": "python", 172 | "pygments_lexer": "ipython3", 173 | "version": "3.6.13" 174 | }, 175 | "colab": { 176 | "name": "Advance_Assignment_07.ipynb", 177 | "provenance": [], 178 | "collapsed_sections": [] 179 | } 180 | }, 181 | "nbformat": 4, 182 | "nbformat_minor": 5 183 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d3a4bb27", 6 | "metadata": { 7 | "id": "d3a4bb27" 8 | }, 9 | "source": [ 10 | "# Assignment 7 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "v59RZ-cITmIv" 20 | }, 21 | "id": "v59RZ-cITmIv" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "823845d3", 26 | "metadata": { 27 | "id": "823845d3" 28 | }, 29 | "source": [ 30 | "#### 1. Write a Python Program to find sum of Array ?" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "id": "6dd2fd5c", 37 | "metadata": { 38 | "colab": { 39 | "base_uri": "https://localhost:8080/" 40 | }, 41 | "id": "6dd2fd5c", 42 | "outputId": "762a487d-de60-4f4c-aa8c-d561a62c59ac" 43 | }, 44 | "outputs": [ 45 | { 46 | "output_type": "stream", 47 | "name": "stdout", 48 | "text": [ 49 | "Enter the Array: 1,2,3,4,7\n", 50 | "The sum of (1, 2, 3, 4, 7) is 17\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "def sumOfArray():\n", 56 | " in_arr = eval(input(\"Enter the Array: \"))\n", 57 | " print(f'The sum of {in_arr} is {sum(in_arr)}')\n", 58 | "\n", 59 | "sumOfArray()" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "id": "9e6c1983", 65 | "metadata": { 66 | "id": "9e6c1983" 67 | }, 68 | "source": [ 69 | "#### 2. Write a Python Program to find Largest element in an Array ?" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 2, 75 | "id": "e5c57cb3", 76 | "metadata": { 77 | "colab": { 78 | "base_uri": "https://localhost:8080/" 79 | }, 80 | "id": "e5c57cb3", 81 | "outputId": "40722f0f-b893-4f5e-e49d-3bd4bc2a26c1" 82 | }, 83 | "outputs": [ 84 | { 85 | "output_type": "stream", 86 | "name": "stdout", 87 | "text": [ 88 | "Enter the Array: 12,100,9,8\n", 89 | "The Largest Element in (12, 100, 9, 8) is 100\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "def largestElement():\n", 95 | " in_arr = eval(input(\"Enter the Array: \"))\n", 96 | " print(f\"The Largest Element in {in_arr} is {sorted(in_arr, reverse=True)[0]}\")\n", 97 | " \n", 98 | "largestElement()" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "id": "c2705b7d", 104 | "metadata": { 105 | "id": "c2705b7d" 106 | }, 107 | "source": [ 108 | "#### 3. Write a Python Program for array rotation ?" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 3, 114 | "id": "08afb7ff", 115 | "metadata": { 116 | "colab": { 117 | "base_uri": "https://localhost:8080/" 118 | }, 119 | "id": "08afb7ff", 120 | "outputId": "a84a385b-24a8-454d-e9dc-0d20fe9864c6" 121 | }, 122 | "outputs": [ 123 | { 124 | "output_type": "stream", 125 | "name": "stdout", 126 | "text": [ 127 | "Enter the Array: 1,2,3,4,5\n", 128 | "The Reverse of Array (1, 2, 3, 4, 5) is (5, 4, 3, 2, 1)\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "def reverseOfArray():\n", 134 | " in_arr = eval(input(\"Enter the Array: \"))\n", 135 | " print(f\"The Reverse of Array {in_arr} is {in_arr[::-1]}\")\n", 136 | " \n", 137 | "reverseOfArray()" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "5b6207c1", 143 | "metadata": { 144 | "id": "5b6207c1" 145 | }, 146 | "source": [ 147 | "#### 4. Write a Python Program to Split the array and add the first part to the end ?" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 4, 153 | "id": "d6e5de3d", 154 | "metadata": { 155 | "colab": { 156 | "base_uri": "https://localhost:8080/" 157 | }, 158 | "id": "d6e5de3d", 159 | "outputId": "6e55bde1-d98a-4cae-87c5-4d117980f21e" 160 | }, 161 | "outputs": [ 162 | { 163 | "output_type": "stream", 164 | "name": "stdout", 165 | "text": [ 166 | "Enter the Array: 12,2,3,4,5\n", 167 | "The Sum of First and Last Elements of Array (12, 2, 3, 4, 5) is 17\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "def sumOfSplits():\n", 173 | " in_arr = eval(input(\"Enter the Array: \"))\n", 174 | " print(f\"The Sum of First and Last Elements of Array {in_arr} is {in_arr[0]+in_arr[-1]}\")\n", 175 | " \n", 176 | "sumOfSplits()" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "id": "eb0e084d", 182 | "metadata": { 183 | "id": "eb0e084d" 184 | }, 185 | "source": [ 186 | "#### 5. Write a Python Program to check if given array is Monotonic ?" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "id": "08c0196d", 193 | "metadata": { 194 | "colab": { 195 | "base_uri": "https://localhost:8080/" 196 | }, 197 | "id": "08c0196d", 198 | "outputId": "70825efd-8cd5-416e-8428-2e8a4b42bcfd" 199 | }, 200 | "outputs": [ 201 | { 202 | "output_type": "stream", 203 | "name": "stdout", 204 | "text": [ 205 | "Enter the Array: 1,2,3,4,5,6,\n", 206 | "Array (1, 2, 3, 4, 5, 6) is Monotonic\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "def checkMonotonic():\n", 212 | " in_arr = eval(input(\"Enter the Array: \"))\n", 213 | " if(all(in_arr[i]<=in_arr[i+1] for i in range(len(in_arr)-1)) or all(in_arr[i]>=in_arr[i+1] for i in range(len(in_arr)-1))):\n", 214 | " print(f'Array {in_arr} is Monotonic')\n", 215 | " else:\n", 216 | " print(f'Array {in_arr} is Not Monotonic')\n", 217 | "\n", 218 | "checkMonotonic()\n", 219 | "checkMonotonic()" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "source": [ 225 | "" 226 | ], 227 | "metadata": { 228 | "id": "26LnxAGAT-2t" 229 | }, 230 | "id": "26LnxAGAT-2t", 231 | "execution_count": null, 232 | "outputs": [] 233 | } 234 | ], 235 | "metadata": { 236 | "kernelspec": { 237 | "display_name": "Python 3 (ipykernel)", 238 | "language": "python", 239 | "name": "python3" 240 | }, 241 | "language_info": { 242 | "codemirror_mode": { 243 | "name": "ipython", 244 | "version": 3 245 | }, 246 | "file_extension": ".py", 247 | "mimetype": "text/x-python", 248 | "name": "python", 249 | "nbconvert_exporter": "python", 250 | "pygments_lexer": "ipython3", 251 | "version": "3.8.8" 252 | }, 253 | "colab": { 254 | "name": "Basic_Programming Assignment_07.ipynb", 255 | "provenance": [], 256 | "collapsed_sections": [] 257 | } 258 | }, 259 | "nbformat": 4, 260 | "nbformat_minor": 5 261 | } -------------------------------------------------------------------------------- /Python/Basic Python/Python_Basics_Assignment_8.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "bd4844c1", 6 | "metadata": { 7 | "id": "bd4844c1" 8 | }, 9 | "source": [ 10 | "# Assignment 8 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "GK6SqJNJaELg" 20 | }, 21 | "id": "GK6SqJNJaELg" 22 | }, 23 | { 24 | "cell_type": "code", 25 | "source": [ 26 | "!pip install PyInputPlus" 27 | ], 28 | "metadata": { 29 | "id": "kaVedZyFaV0d" 30 | }, 31 | "id": "kaVedZyFaV0d", 32 | "execution_count": null, 33 | "outputs": [] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "215594ca", 38 | "metadata": { 39 | "id": "215594ca" 40 | }, 41 | "source": [ 42 | "#### 1. Is the Python Standard Library included with PyInputPlus?\n", 43 | "**Ans:** No, **`PyInputPlus`** is not a part of Python Standard Library, it needs to be installed explicitly using the command **`!pip install PyInputPlus`**" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "id": "76a769e0", 49 | "metadata": { 50 | "id": "76a769e0" 51 | }, 52 | "source": [ 53 | "#### 2. Why is PyInputPlus commonly imported with import pyinputplus as pypi?\n", 54 | "**Ans:** You can import the module with **`import pyinputplus as pypi`** so that you can enter a shorter name when calling the module's functions." 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "source": [ 60 | "import pyinputplus as pypi" 61 | ], 62 | "metadata": { 63 | "id": "y8xwpD3uabjq" 64 | }, 65 | "id": "y8xwpD3uabjq", 66 | "execution_count": 2, 67 | "outputs": [] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "5d578dfb", 72 | "metadata": { 73 | "id": "5d578dfb" 74 | }, 75 | "source": [ 76 | "#### 3. How do you distinguish between inputInt() and inputFloat()?\n", 77 | "**Ans:** **`inputInt()`** function Accepts an integer value. This also takes additional parameters **`min`**, **`max`**, **`greaterThan`** and **`lessThan`** for bounds. And it always returns an int.\n", 78 | "\n", 79 | "Whereas **`inputFloat()`** function Accepts a floating-point numeric value. this also takes additional **`min`**, **`max`**, **`greaterThan`** and **`lessThan`** parameters. and always returns a float." 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "source": [ 85 | "inp = pypi.inputInt(prompt = \"Enter an Integer... \", \n", 86 | " default = 0, limit = 3)\n", 87 | " \n", 88 | "print(inp)\n", 89 | "\n", 90 | "inp2 = pypi.inputFloat(prompt = \"Enter Float value... \", \n", 91 | " default = 0, limit = 3)\n", 92 | " \n", 93 | "print(inp2)" 94 | ], 95 | "metadata": { 96 | "colab": { 97 | "base_uri": "https://localhost:8080/" 98 | }, 99 | "id": "g2GwvffBadoN", 100 | "outputId": "4f614fc6-c982-4d35-b7e2-83e5789b1135" 101 | }, 102 | "id": "g2GwvffBadoN", 103 | "execution_count": 6, 104 | "outputs": [ 105 | { 106 | "output_type": "stream", 107 | "name": "stdout", 108 | "text": [ 109 | "Enter an Integer... 10\n", 110 | "10\n", 111 | "Enter Float value... 10\n", 112 | "10.0\n" 113 | ] 114 | } 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "id": "5a276409", 120 | "metadata": { 121 | "id": "5a276409" 122 | }, 123 | "source": [ 124 | "#### 4. Using PyInputPlus, how do you ensure that the user enters a whole number between 0 and 99?\n", 125 | "**Ans:** **`PyInputPlus`** module provides a function called as **`inputInt()`** which only returns only integer values. inorder to restrict the input between 0 and 99, i'ii use parameters like **`min`** & **`max`** to ensure that user enters the values between the defined range only.\n", 126 | "\n", 127 | "`import pyinputplus as pyip` \n", 128 | "`wholenumber = pyip.inputInt(prompt='Enter a number: ', min=0, max=100)` \n", 129 | "`print(wholenumber)` " 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "source": [ 135 | "import pyinputplus as pyip\n", 136 | "wholenumber = pyip.inputInt(prompt='Enter a number: ', min=0, max=100) \n", 137 | "print(wholenumber)" 138 | ], 139 | "metadata": { 140 | "colab": { 141 | "base_uri": "https://localhost:8080/" 142 | }, 143 | "id": "hhZgUQuTbKuH", 144 | "outputId": "ce8c5895-cb8d-4174-d893-b291a2c8ef2b" 145 | }, 146 | "id": "hhZgUQuTbKuH", 147 | "execution_count": 7, 148 | "outputs": [ 149 | { 150 | "output_type": "stream", 151 | "name": "stdout", 152 | "text": [ 153 | "Enter a number: 189\n", 154 | "Number must be at maximum 100.\n", 155 | "Enter a number: 98\n", 156 | "98\n" 157 | ] 158 | } 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "id": "a3266159", 164 | "metadata": { 165 | "id": "a3266159" 166 | }, 167 | "source": [ 168 | "#### 5. What is transferred to the keyword arguments allowRegexes and blockRegexes?\n", 169 | "**Ans:** we can use **`allowRegexes`** and **`blockRegexes`** keyword arguments to take list of regular expression strings to determine\n", 170 | "what the pyinputplus function will reject or accept valid input." 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "id": "20251b09", 176 | "metadata": { 177 | "id": "20251b09" 178 | }, 179 | "source": [ 180 | "#### 6. If a blank input is entered three times, what does inputStr(limit=3) do?\n", 181 | "**Ans:** The statement **`inputStr(limit=3)`** will throw two exceptions **`ValidationException`** and **`RetryLimitException`**. The first exception is thrown because blank values are not allowed by inputStr() function by default. it we want to consider blank values as valid input, we have to set **`blank=True`**.\n", 182 | "\n", 183 | "The second exception is occured because we have reached the max limit we have specified by using **`limit`** parameter. inorder to avoid this exception we can use **`default`** parameter to return a default value when max limit is reached." 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "id": "a57b26ea", 189 | "metadata": { 190 | "id": "a57b26ea" 191 | }, 192 | "source": [ 193 | "#### 7. If blank input is entered three times, what does inputStr(limit=3, default='hello') do?\n", 194 | "**Ans:** Since the default parameter is set to **`hello`**. after blank input is entered three times instead of raising **`RetryLimitException`** exception. the function will return **`hello`** as response to the calling function" 195 | ] 196 | } 197 | ], 198 | "metadata": { 199 | "kernelspec": { 200 | "display_name": "Python 3", 201 | "language": "python", 202 | "name": "python3" 203 | }, 204 | "language_info": { 205 | "codemirror_mode": { 206 | "name": "ipython", 207 | "version": 3 208 | }, 209 | "file_extension": ".py", 210 | "mimetype": "text/x-python", 211 | "name": "python", 212 | "nbconvert_exporter": "python", 213 | "pygments_lexer": "ipython3", 214 | "version": "3.8.8" 215 | }, 216 | "colab": { 217 | "name": "Python Basics Assignment_8.ipynb", 218 | "provenance": [], 219 | "collapsed_sections": [] 220 | } 221 | }, 222 | "nbformat": 4, 223 | "nbformat_minor": 5 224 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "cdb59e56", 6 | "metadata": { 7 | "id": "cdb59e56" 8 | }, 9 | "source": [ 10 | "# Assignment 06 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "hPXM50eas3rV" 20 | }, 21 | "id": "hPXM50eas3rV" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "9dce2ceb", 26 | "metadata": { 27 | "id": "9dce2ceb" 28 | }, 29 | "source": [ 30 | "#### Q1. Describe three applications for exception processing.\n", 31 | "**Ans:** Exception Processing is important to find exceptions that causes the runtime error as runtime errors halt the program execution when exception occurs.\n", 32 | "\n", 33 | "Exception Processing is used in Various Applications. Few examples are:\n", 34 | "1. Checking Appropriate use of input in an application\n", 35 | "2. Checking for Arithmetic exceptions in mathematical executions\n", 36 | "3. Checking File I/O exceptions during File handling" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "id": "c82ae389", 42 | "metadata": { 43 | "id": "c82ae389" 44 | }, 45 | "source": [ 46 | "#### Q2. What happens if you don't do something extra to treat an exception?\n", 47 | "**Ans:** If Exceptions are not handled, flow of program will be broken during the run time which might lead to an abnormal termination of the program. Inshort inability of program to handle exceptions will result in crashing of program and we will not be able to get the desired result." 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "id": "5adab9bd", 53 | "metadata": { 54 | "id": "5adab9bd" 55 | }, 56 | "source": [ 57 | "#### Q3. What are your options for recovering from an exception in your script?\n", 58 | "**Ans:** Python provides **`try`** and **`except`** statements for recovering from an exception in our script." 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "source": [ 64 | "try:\n", 65 | " print(x)\n", 66 | "except:\n", 67 | " print(\"An exception occurred\") " 68 | ], 69 | "metadata": { 70 | "colab": { 71 | "base_uri": "https://localhost:8080/" 72 | }, 73 | "id": "jPcq5_YZtSD0", 74 | "outputId": "d7c3e83c-c901-4326-a098-0ef101b55f8c" 75 | }, 76 | "id": "jPcq5_YZtSD0", 77 | "execution_count": 1, 78 | "outputs": [ 79 | { 80 | "output_type": "stream", 81 | "name": "stdout", 82 | "text": [ 83 | "An exception occurred\n" 84 | ] 85 | } 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "id": "d7379d80", 91 | "metadata": { 92 | "id": "d7379d80" 93 | }, 94 | "source": [ 95 | "#### Q4. Describe two methods for triggering exceptions in your script ?\n", 96 | "**Ans:** **`raise`** and **`assert`** are two methods that can be used to trigger manual exceptions in our script.\n", 97 | "- **`raise`** method triggers an exception if condition provided to it turns out to be True.\n", 98 | "- **`assert`** will let the program to continue execution if condition provided to it turns out to be True else exception will be raised" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 2, 104 | "id": "55ca2b5b", 105 | "metadata": { 106 | "colab": { 107 | "base_uri": "https://localhost:8080/", 108 | "height": 203 109 | }, 110 | "id": "55ca2b5b", 111 | "outputId": "46b5513a-afe8-48b9-c58b-ae00f49aebd2" 112 | }, 113 | "outputs": [ 114 | { 115 | "output_type": "error", 116 | "ename": "Exception", 117 | "evalue": "ignored", 118 | "traceback": [ 119 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 120 | "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", 121 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Example of raise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m101\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'X Value Should not exceed 5 The Provided Value of X is {x}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 122 | "\u001b[0;31mException\u001b[0m: X Value Should not exceed 5 The Provided Value of X is 101" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "# Example of raise\n", 128 | "x = 101\n", 129 | "raise Exception(f'X Value Should not exceed 5 The Provided Value of X is {x}')" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 3, 135 | "id": "b9bfe958", 136 | "metadata": { 137 | "colab": { 138 | "base_uri": "https://localhost:8080/", 139 | "height": 186 140 | }, 141 | "id": "b9bfe958", 142 | "outputId": "9b49489d-6785-4d88-ac5c-b8eaf40fb60d" 143 | }, 144 | "outputs": [ 145 | { 146 | "output_type": "error", 147 | "ename": "AssertionError", 148 | "evalue": "ignored", 149 | "traceback": [ 150 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 151 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 152 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Example of assert\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"2 is not equal to 4\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 153 | "\u001b[0;31mAssertionError\u001b[0m: 2 is not equal to 4" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "# Example of assert\n", 159 | "assert(2==10), \"2 is not equal to 4\"" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "id": "6335bd5c", 165 | "metadata": { 166 | "id": "6335bd5c" 167 | }, 168 | "source": [ 169 | "#### Q5. Identify two methods for specifying actions to be executed at termination time, regardless of whether or not an exception exists.\n", 170 | "**Ans:** Python Provides **`else`** and **`finally`** blocks for specifying actions to be executed at termination time, regardless of whether an exceptions exists or not." 171 | ] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "Python 3", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.6.13" 191 | }, 192 | "colab": { 193 | "name": "Advance_Assignment_06.ipynb", 194 | "provenance": [], 195 | "collapsed_sections": [] 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 5 200 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "0a66efc2", 6 | "metadata": { 7 | "id": "0a66efc2" 8 | }, 9 | "source": [ 10 | "# Assignment 6 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "wpRqvutVSMpW" 20 | }, 21 | "id": "wpRqvutVSMpW" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "cd035ec4", 26 | "metadata": { 27 | "id": "cd035ec4" 28 | }, 29 | "source": [ 30 | "#### 1. Write a Python Program to Display Fibonacci Sequence using Recursion ?" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "id": "c6a0db1b", 37 | "metadata": { 38 | "colab": { 39 | "base_uri": "https://localhost:8080/" 40 | }, 41 | "id": "c6a0db1b", 42 | "outputId": "298efa41-47da-4dca-f96e-844eb94e6bdd" 43 | }, 44 | "outputs": [ 45 | { 46 | "output_type": "stream", 47 | "name": "stdout", 48 | "text": [ 49 | "Enter the length of Series: 5\n", 50 | "0, 1, 3, 5, 8, 13, 21, " 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "def genFibonacci(n,a,b):\n", 56 | " if n == 0:\n", 57 | " return 1\n", 58 | " else:\n", 59 | " result = a+b\n", 60 | " print(result, end=', ')\n", 61 | " genFibonacci(n-1,b,result)\n", 62 | "in_num = int(input('Enter the length of Series: '))\n", 63 | "print('0, 1',end=', ')\n", 64 | "genFibonacci(in_num,1,2)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "15b838c7", 70 | "metadata": { 71 | "id": "15b838c7" 72 | }, 73 | "source": [ 74 | "#### 2. Write a Python Program to Find Factorial of a Number using Recursion ?" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 2, 80 | "id": "56e04b25", 81 | "metadata": { 82 | "colab": { 83 | "base_uri": "https://localhost:8080/" 84 | }, 85 | "id": "56e04b25", 86 | "outputId": "82be9ece-d85b-4cb2-901c-9a50ee12921d" 87 | }, 88 | "outputs": [ 89 | { 90 | "output_type": "stream", 91 | "name": "stdout", 92 | "text": [ 93 | "Enter a number: 10\n", 94 | "The Factorial of 10 is 3628800\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "def factorial(num):\n", 100 | " if (num < 1):\n", 101 | " return 1\n", 102 | " else:\n", 103 | " return num*factorial(num-1)\n", 104 | "num = int(input('Enter a number: '))\n", 105 | "value = factorial(num)\n", 106 | "print(f'The Factorial of {num} is {value}')" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "id": "95f7ab1e", 112 | "metadata": { 113 | "id": "95f7ab1e" 114 | }, 115 | "source": [ 116 | "#### 3. Write a Python Program to Calculate your Body Mass Index ?" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 3, 122 | "id": "8e0c3d17", 123 | "metadata": { 124 | "colab": { 125 | "base_uri": "https://localhost:8080/" 126 | }, 127 | "id": "8e0c3d17", 128 | "outputId": "40597c0d-13ec-482a-e39a-342277030eed" 129 | }, 130 | "outputs": [ 131 | { 132 | "output_type": "stream", 133 | "name": "stdout", 134 | "text": [ 135 | "Enter your Weight(kgs): 120\n", 136 | "Enter your Height(mts): 1.8\n", 137 | "Your're BMI is 37.03703703703704 and status is Suffering from Obesity \n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "def calculateBMI():\n", 143 | " in_weight = eval(input('Enter your Weight(kgs): '))\n", 144 | " in_height = eval(input('Enter your Height(mts): '))\n", 145 | " calc_bmi = in_weight/pow(in_height,2)\n", 146 | " if (calc_bmi < 18.5):\n", 147 | " status = 'Underweight'\n", 148 | " elif (calc_bmi >= 18.5 and calc_bmi < 24.9):\n", 149 | " status = 'Healthy'\n", 150 | " elif (calc_bmi >= 24.9 and calc_bmi < 30):\n", 151 | " status = 'Overweight'\n", 152 | " elif (calc_bmi >=30):\n", 153 | " status = 'Suffering from Obesity'\n", 154 | " print(f'Your\\'re BMI is {calc_bmi} and status is {status} ')\n", 155 | "calculateBMI()" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "id": "8be593fe", 161 | "metadata": { 162 | "id": "8be593fe" 163 | }, 164 | "source": [ 165 | "#### 4. Write a Python Program to Calculate the Natural Logarithm of any Number ?" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 4, 171 | "id": "e1db8d0f", 172 | "metadata": { 173 | "colab": { 174 | "base_uri": "https://localhost:8080/" 175 | }, 176 | "id": "e1db8d0f", 177 | "outputId": "2ac7c9f5-4c6b-48dd-e320-7d3e75c88758" 178 | }, 179 | "outputs": [ 180 | { 181 | "output_type": "stream", 182 | "name": "stdout", 183 | "text": [ 184 | "Enter a Number:10\n", 185 | "2.302585092994046\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "import math\n", 191 | "def genNatLog():\n", 192 | " in_num = eval(input(\"Enter a Number:\"))\n", 193 | " print(math.log(in_num))\n", 194 | "\n", 195 | "genNatLog()" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "id": "5dc827ea", 201 | "metadata": { 202 | "id": "5dc827ea" 203 | }, 204 | "source": [ 205 | "#### 5. Write a Python Program for Cube sum of first n Natural Numbers ?" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 5, 211 | "id": "70a3a203", 212 | "metadata": { 213 | "colab": { 214 | "base_uri": "https://localhost:8080/" 215 | }, 216 | "id": "70a3a203", 217 | "outputId": "a9904809-8898-4a17-a95e-a6f98ba8f4df" 218 | }, 219 | "outputs": [ 220 | { 221 | "output_type": "stream", 222 | "name": "stdout", 223 | "text": [ 224 | "Enter the no of Natural Numbers: 120\n", 225 | "The Cube Sum of First 120 Natural Numbers is 52707600.0\n" 226 | ] 227 | } 228 | ], 229 | "source": [ 230 | "def cubeOfNaturalNumbers():\n", 231 | " in_num = int(input(\"Enter the no of Natural Numbers: \"))\n", 232 | " result = pow(((in_num * (in_num +1))/2),2)\n", 233 | " print(f'The Cube Sum of First {in_num} Natural Numbers is {result}')\n", 234 | "\n", 235 | "cubeOfNaturalNumbers()" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "source": [ 241 | "" 242 | ], 243 | "metadata": { 244 | "id": "lUqvC8cBSa7-" 245 | }, 246 | "id": "lUqvC8cBSa7-", 247 | "execution_count": null, 248 | "outputs": [] 249 | } 250 | ], 251 | "metadata": { 252 | "kernelspec": { 253 | "display_name": "Python 3", 254 | "language": "python", 255 | "name": "python3" 256 | }, 257 | "language_info": { 258 | "codemirror_mode": { 259 | "name": "ipython", 260 | "version": 3 261 | }, 262 | "file_extension": ".py", 263 | "mimetype": "text/x-python", 264 | "name": "python", 265 | "nbconvert_exporter": "python", 266 | "pygments_lexer": "ipython3", 267 | "version": "3.8.8" 268 | }, 269 | "colab": { 270 | "name": "Basic_Programming Assignment_06.ipynb", 271 | "provenance": [] 272 | } 273 | }, 274 | "nbformat": 4, 275 | "nbformat_minor": 5 276 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_18.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "22800d6c", 6 | "metadata": { 7 | "id": "22800d6c" 8 | }, 9 | "source": [ 10 | "# Assignment 18 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "Jt-U-DxS4Coa" 20 | }, 21 | "id": "Jt-U-DxS4Coa" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "fcd9b173", 26 | "metadata": { 27 | "id": "fcd9b173" 28 | }, 29 | "source": [ 30 | "#### Q1. Describe the differences between text and binary files in a single paragraph.\n", 31 | "**Ans:** The differences between Text Files and Binary Files are:\n", 32 | "\n", 33 | "**Text files** are special subset of binary files that are used to store human readable characters as a rich text document or plain text document. Text files also store data in sequential bytes but bits in text file represents characters.\n", 34 | "where as **Binary files** are those typical files that store data in the form of sequence of bytes grouped into eight bits or sometimes sixteen bits. These bits represent custom data and such files can store multiple types of data (images, audio, text, etc) under a single file." 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "3e363a3b", 40 | "metadata": { 41 | "id": "3e363a3b" 42 | }, 43 | "source": [ 44 | "#### Q2. What are some scenarios where using text files will be the better option? When would you like to use binary files instead of text files?\n", 45 | "**Ans:** Text files are less prone to get corrupted as any undesired change may just show up once the file is opened and then can easily be removed whereas binary files can be used instead of text files for image data/video/audio. For many types of data, binary representations are much more space-efficient. For instance, you could represent an image as a text file. But any color represented as a 24-bit RGB value in a binary file has to be represented as a six-character code (e.g. FFFFFF for white) in text, which is literally twice as many bits. Another reason for using binary formats is that they can map directly onto the internal representation of the data that the program is using. If my program uses a complex data structure that’s full of integer, float, and byte values, when I read it in from a text file I have to parse every character of text and interpret it, spending processing time (and writing logic) to figure how to convert the 88 bits in “2983102931,” into the 32-bit integer that the text represents. If I read it from a binary file, the program just copies those 32 bits from the file into memory." 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "id": "79fabe8b", 51 | "metadata": { 52 | "id": "79fabe8b" 53 | }, 54 | "source": [ 55 | "#### Q3. What are some of the issues with using binary operations to read and write a Python integer directly to disc?\n", 56 | "**Ans:** When we read or write a python integer using binary operations \n", 57 | "1. Binary operations deal with raw data\n", 58 | "2. One needs to identify how many bytes one would read or write.\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "id": "96674fa6", 64 | "metadata": { 65 | "id": "96674fa6" 66 | }, 67 | "source": [ 68 | "#### Q4. Describe a benefit of using the with keyword instead of explicitly opening a file ?\n", 69 | "**Ans:** When a file is opened using the **`with`** keyword,it automatically closes the file if some exceptions occur after opening a file, or at the end of the file . Thereby not leaving a file in open mode and also there would no need to explicitly close a file. Using **with** means that the file will be closed as soon as you leave the block. This is beneficial because closing a file is something that can easily be forgotten and ties up resources that you no longer need." 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "7ec11be2", 75 | "metadata": { 76 | "id": "7ec11be2" 77 | }, 78 | "source": [ 79 | "#### Q5. Does Python have the trailing newline while reading a line of text? Does Python append a newline when you write a line of text?\n", 80 | "**Ans:** Yes, Python have the trailing newline while reading a line of text. When we write, a newline has to be provided in python explicitly to append a newline. Hence Python does not append a newline implicitly" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "8d433ac7", 86 | "metadata": { 87 | "id": "8d433ac7" 88 | }, 89 | "source": [ 90 | "#### Q6. What file operations enable for random-access operation?\n", 91 | "**Ans:** The file operations enable for random-access operation are **`seek()`** and **`tell()`**" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "id": "6e521586", 97 | "metadata": { 98 | "id": "6e521586" 99 | }, 100 | "source": [ 101 | "#### Q7. When do you think you'll use the struct package the most?\n", 102 | "**Ans:** The **`struct`** package is mostly used while converting a common `python` language types into **`C`** language types." 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "id": "9609ec3f", 108 | "metadata": { 109 | "id": "9609ec3f" 110 | }, 111 | "source": [ 112 | "#### Q8. When is pickling the best option?\n", 113 | "**Ans:** Pickling is best option for creating a new binary file using python. Pickle in Python is primarily used in serializing and deserializing a Python object structure. In other words, it’s the process of converting a Python object into a byte stream to store it in a file/database, maintain program state across sessions, or transport data over the network. The pickled byte stream can be used to re-create the original object hierarchy by unpickling the stream. This whole process is similar to object serialization in Java or .Net." 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "id": "98b90ed9", 119 | "metadata": { 120 | "id": "98b90ed9" 121 | }, 122 | "source": [ 123 | "#### Q9. When will it be best to use the shelve package?\n", 124 | "**Ans:** **`Shelve`** package is used to pickle data but treats the entire file as dictionary. A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings.\n", 125 | "\n", 126 | "Syntax: shelve.open(filename, flag='c', protocol=None, writeback=False)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "id": "a4ad2a21", 132 | "metadata": { 133 | "id": "a4ad2a21" 134 | }, 135 | "source": [ 136 | "#### Q10. What is a special restriction when using the shelve package, as opposed to using other data dictionaries?\n", 137 | "**Ans:** Only string data type can be used as key in this special dictionary (shelve package) object, whereas any pickle Python object can be used as value." 138 | ] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.8.8" 158 | }, 159 | "colab": { 160 | "name": "Advance_Assignment_18.ipynb", 161 | "provenance": [], 162 | "collapsed_sections": [] 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 5 167 | } -------------------------------------------------------------------------------- /Python/Basic Python/Python_Basics_Assignment_25.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7f9b8597", 6 | "metadata": { 7 | "id": "7f9b8597" 8 | }, 9 | "source": [ 10 | "# Assignment 25 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "i-wesRSd-wXG" 20 | }, 21 | "id": "i-wesRSd-wXG" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "5aee2c7e", 26 | "metadata": { 27 | "id": "5aee2c7e" 28 | }, 29 | "source": [ 30 | "#### 1) . What is the difference between enclosing a list comprehension in square brackets and parentheses?\n", 31 | "**Ans:** Enclosing a list comprehension in square brackets returns a list but enclosing a list comprehension in parentheses returns a `generator` object. Example is mentioned below:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "id": "b4745353-bbeb-46d7-bcab-9f81ab1914b5", 38 | "metadata": { 39 | "id": "b4745353-bbeb-46d7-bcab-9f81ab1914b5", 40 | "outputId": "1850a9dd-01cf-419e-dd53-6a456ed85010" 41 | }, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] \n", 48 | " at 0x00000193F09C5C10> \n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "l = [ele for ele in range(10)]\n", 54 | "print(l, type(l))\n", 55 | "g = (ele for ele in range(10))\n", 56 | "print(g, type(g))" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "id": "43ead631", 62 | "metadata": { 63 | "id": "43ead631" 64 | }, 65 | "source": [ 66 | "#### 2) What is the relationship between generators and iterators?\n", 67 | "**Ans:** An `iterator` is an object which contains a countable number of values and it is used to iterate over iterable objects like list, tuples, sets, etc. Iterators are implemented using a class. It follows lazy evaluation where the evaluation of the expression will be on hold and stored in the memory until the item is called specifically which helps us to avoid repeated evaluation. As lazy evaluation is implemented, it requires only 1 memory location to process the value and when we are using a large dataset then, wastage of RAM space will be reduced the need to load the entire dataset at the same time will not be there.For an iterator: `iter()` keyword is used to create an iterator containing an iterable object.`next()` \n", 68 | "keyword is used to call the next element in the iterable object.\n", 69 | "\n", 70 | "Similarly `Generators` are an another way of creating iterators in a simple way where it uses the keyword `yield` statement instead of `return` statement in a defined function.Generators are implemented using a function. Just as iterators, generators also follow `lazy evaluation`. Here, the yield function returns the data without affecting or exiting the function. It will return a sequence of data in an iterable format where we need to iterate over the sequence to use the data as they won’t store the entire sequence in the memory." 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 1, 76 | "id": "475ad9eb-18b8-4e3a-a246-86c341de25ad", 77 | "metadata": { 78 | "tags": [], 79 | "colab": { 80 | "base_uri": "https://localhost:8080/" 81 | }, 82 | "id": "475ad9eb-18b8-4e3a-a246-86c341de25ad", 83 | "outputId": "d4204da5-948a-43d5-ea19-03ef0405cc29" 84 | }, 85 | "outputs": [ 86 | { 87 | "output_type": "stream", 88 | "name": "stdout", 89 | "text": [ 90 | "\n", 91 | "iNeuron\n", 92 | "Full\n", 93 | "Stack\n", 94 | "Data Science\n", 95 | "\n", 96 | "0\n", 97 | "1\n", 98 | "8\n", 99 | "27\n", 100 | "64\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "# Example of iterator\n", 106 | "iter_str = iter(['iNeuron','Full','Stack','Data Science'])\n", 107 | "print(type(iter_str))\n", 108 | "print(next(iter_str))\n", 109 | "print(next(iter_str))\n", 110 | "print(next(iter_str))\n", 111 | "print(next(iter_str))\n", 112 | "print(iter_str) # After the iterable object is completed, to use them again we have reassign them to the same object.\n", 113 | "\n", 114 | "# Example of Generator\n", 115 | "def cube_numbers(in_num):\n", 116 | " for ele in range(in_num+1):\n", 117 | " yield ele**3\n", 118 | "\n", 119 | "out_num = cube_numbers(4)\n", 120 | "print(next(out_num))\n", 121 | "print(next(out_num))\n", 122 | "print(next(out_num))\n", 123 | "print(next(out_num))\n", 124 | "print(next(out_num))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "id": "c4bd6bf0", 130 | "metadata": { 131 | "id": "c4bd6bf0" 132 | }, 133 | "source": [ 134 | "#### 3) What are the signs that a function is a generator function?\n", 135 | "**Ans:** A generator function uses a `yield` statement instead of a `return` statement. A generator function will always return a iterable object called generator. where as a normal function can return a `string/list/tuple/dict/NoneType` etc." 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "id": "b35dd5fe", 141 | "metadata": { 142 | "id": "b35dd5fe" 143 | }, 144 | "source": [ 145 | "#### 4) What is the purpose of a yield statement?\n", 146 | "**Ans:** The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list." 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "id": "d2182986", 152 | "metadata": { 153 | "tags": [], 154 | "id": "d2182986" 155 | }, 156 | "source": [ 157 | "#### 5) What is the relationship between map calls and list comprehensions? Make a comparison and contrast between the two ?\n", 158 | "**Ans:** The main differences between map calls and list comprehensiosn are:\n", 159 | "1. List comprehension is more concise and easier to read as compared to map.\n", 160 | "2. List comprehension allows filtering. In map, we have no such facility. For example, to print all odd numbers in range of 50, we can write `[n for n in range(50) if n%2 != 0]`. There is no alternate for it in map\n", 161 | "3. List comprehension are used when a list of results is required as final output.but map only returns a map object. it needs to be explicitly coverted to desired datatype.\n", 162 | "4. List comprehension is faster than map when we need to evaluate expressions that are too long or complicated to express\n", 163 | "5. Map is faster in case of calling an already defined function on a set of values." 164 | ] 165 | } 166 | ], 167 | "metadata": { 168 | "kernelspec": { 169 | "display_name": "Python 3", 170 | "language": "python", 171 | "name": "python3" 172 | }, 173 | "language_info": { 174 | "codemirror_mode": { 175 | "name": "ipython", 176 | "version": 3 177 | }, 178 | "file_extension": ".py", 179 | "mimetype": "text/x-python", 180 | "name": "python", 181 | "nbconvert_exporter": "python", 182 | "pygments_lexer": "ipython3", 183 | "version": "3.8.8" 184 | }, 185 | "colab": { 186 | "name": "Python Basics Assignment_25.ipynb", 187 | "provenance": [], 188 | "collapsed_sections": [] 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 5 193 | } -------------------------------------------------------------------------------- /Python/Basic Python/Python_Basics_Assignment_18.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "008a10cf", 6 | "metadata": { 7 | "id": "008a10cf" 8 | }, 9 | "source": [ 10 | "# Assignment 18 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "uzYDKCNcJHGl" 20 | }, 21 | "id": "uzYDKCNcJHGl" 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "id": "ceee30c4-2e1d-4191-9069-33d032e6b72f", 27 | "metadata": { 28 | "id": "ceee30c4-2e1d-4191-9069-33d032e6b72f", 29 | "outputId": "62c36318-e8c4-4b0b-b98b-25573c7f6bc9" 30 | }, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "def hours():\n", 37 | " print(\"Open 9-5 daily\")\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "!type zoo.py" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "id": "4b0045ff", 48 | "metadata": { 49 | "id": "4b0045ff" 50 | }, 51 | "source": [ 52 | "#### 1. Create a zoo.py file first. Define the hours() function, which prints the string 'Open 9-5 daily'. Then, use the interactive interpreter to import the zoo module and call its hours() function." 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "id": "6ccab705", 59 | "metadata": { 60 | "id": "6ccab705", 61 | "outputId": "e855e5a9-c3b5-48a9-c7e0-8c58b7d8c035" 62 | }, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "Open 9-5 daily\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "import zoo\n", 74 | "zoo.hours()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "id": "77c06807", 80 | "metadata": { 81 | "id": "77c06807" 82 | }, 83 | "source": [ 84 | "#### 2. In the interactive interpreter, import the zoo module as menagerie and call its hours() function." 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "e907fa84", 91 | "metadata": { 92 | "id": "e907fa84", 93 | "outputId": "2a4fa914-b611-4f49-838b-0ab45359c9ce" 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "Open 9-5 daily\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "import zoo as menagerie\n", 106 | "menagerie.hours()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "id": "7674c3cd", 112 | "metadata": { 113 | "id": "7674c3cd" 114 | }, 115 | "source": [ 116 | "#### 3. Using the interpreter, explicitly import and call the hours() function from zoo." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "id": "5cd291d4", 123 | "metadata": { 124 | "id": "5cd291d4", 125 | "outputId": "6d4fb5e6-7a19-487b-8599-a2d816f38fcd" 126 | }, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Open 9-5 daily\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "from zoo import hours\n", 138 | "hours()" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "id": "1b939228", 144 | "metadata": { 145 | "id": "1b939228" 146 | }, 147 | "source": [ 148 | "#### 4. Import the hours() function as info and call it." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "id": "ad74dc37", 155 | "metadata": { 156 | "id": "ad74dc37", 157 | "outputId": "c33efe78-ebc6-40ef-9125-ad2b7dc02420" 158 | }, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "Open 9-5 daily\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "from zoo import hours as info\n", 170 | "info()" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "id": "8ff06188", 176 | "metadata": { 177 | "id": "8ff06188" 178 | }, 179 | "source": [ 180 | "#### 5. Create a plain dictionary with the key-value pairs 'a': 1, 'b': 2, and 'c': 3, and print it out." 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "id": "fbec4dfb", 187 | "metadata": { 188 | "id": "fbec4dfb", 189 | "outputId": "4ae5bbb5-3497-40fe-b6ec-f5729501990b" 190 | }, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | "{'a': 1, 'b': 2, 'c': 3}\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "plain_dict = {'a':1,'b':2,'c':3}\n", 202 | "print(plain_dict)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "id": "096c1b21", 208 | "metadata": { 209 | "id": "096c1b21" 210 | }, 211 | "source": [ 212 | "#### 6.Make an OrderedDict called fancy from the same pairs listed in 5 and print it. Did it print in the same order as plain?" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "id": "dd606af8", 219 | "metadata": { 220 | "id": "dd606af8", 221 | "outputId": "d15ae280-9760-4876-bba0-e9ad44a42b95" 222 | }, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "plain_dict -> {'a': 1, 'b': 2, 'c': 3}\n", 229 | "fancy -> OrderedDict([('a', 1), ('b', 2), ('c', 3)])\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "from collections import OrderedDict\n", 235 | "fancy = OrderedDict(plain_dict)\n", 236 | "print(f'plain_dict -> {plain_dict}')\n", 237 | "print(f'fancy -> {fancy}')" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "id": "e55d1c69", 243 | "metadata": { 244 | "id": "e55d1c69" 245 | }, 246 | "source": [ 247 | "#### 7. Make a default dictionary called dict_of_lists and pass it the argument list. Make the list dict_of_lists['a'] and append the value 'something for a' to it in one assignment. Print dict_of_lists['a']." 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "id": "264fc2cd", 254 | "metadata": { 255 | "id": "264fc2cd", 256 | "outputId": "2aa47fa0-65bd-463c-8828-a9ca6fc92a45" 257 | }, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "['something for a']\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "from collections import defaultdict\n", 269 | "dict_of_lists = defaultdict(list)\n", 270 | "dict_of_lists['a'].append('something for a')\n", 271 | "print(dict_of_lists['a'])" 272 | ] 273 | } 274 | ], 275 | "metadata": { 276 | "kernelspec": { 277 | "display_name": "Python 3 (ipykernel)", 278 | "language": "python", 279 | "name": "python3" 280 | }, 281 | "language_info": { 282 | "codemirror_mode": { 283 | "name": "ipython", 284 | "version": 3 285 | }, 286 | "file_extension": ".py", 287 | "mimetype": "text/x-python", 288 | "name": "python", 289 | "nbconvert_exporter": "python", 290 | "pygments_lexer": "ipython3", 291 | "version": "3.8.8" 292 | }, 293 | "colab": { 294 | "name": "Python Basics Assignment_18.ipynb", 295 | "provenance": [], 296 | "collapsed_sections": [] 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 5 301 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_15.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "62dc089c", 6 | "metadata": { 7 | "id": "62dc089c" 8 | }, 9 | "source": [ 10 | "# Assignment 15 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "h-jBgqwW-Nku" 20 | }, 21 | "id": "h-jBgqwW-Nku" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "3ea2824c", 26 | "metadata": { 27 | "id": "3ea2824c" 28 | }, 29 | "source": [ 30 | "#### 1. Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console ? \n", 31 | "**Example:** \n", 32 | "If the following n is given as input to the program: `100` \n", 33 | "Then, the output of the program should be: `0,35,70` " 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "id": "1fb66583", 40 | "metadata": { 41 | "id": "1fb66583", 42 | "outputId": "9593e336-5a64-4d30-b50d-a0f5d684e75a" 43 | }, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "0 35 70 " 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "def showDivisible(in_num):\n", 55 | " for ele in range(0,in_num):\n", 56 | " if (ele%5 == 0) and (ele%7 == 0):\n", 57 | " yield ele\n", 58 | "for ele in showDivisible(100):\n", 59 | " print(ele,end=' ')" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "id": "22fd82eb", 65 | "metadata": { 66 | "id": "22fd82eb" 67 | }, 68 | "source": [ 69 | "#### 2.Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console ?\n", 70 | "**Example:** \n", 71 | "If the following n is given as input to the program: `10` \n", 72 | "Then, the output of the program should be: `0,2,4,6,8,10`" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "id": "3e415e4c", 79 | "metadata": { 80 | "id": "3e415e4c", 81 | "outputId": "cd809205-32a6-4c28-9540-24ca0d30535d" 82 | }, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "0 2 4 6 8 10 " 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "def genEvenNumbers(in_num):\n", 94 | " for ele in range(in_num+1):\n", 95 | " if ele%2 == 0:\n", 96 | " yield ele\n", 97 | "\n", 98 | "for ele in genEvenNumbers(10):\n", 99 | " print(ele,end=' ')" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "id": "549ded1e", 105 | "metadata": { 106 | "id": "549ded1e" 107 | }, 108 | "source": [ 109 | "#### 3.The Fibonacci Sequence is computed based on the following formula below, Please write a program using list comprehension to print the Fibonacci Sequence in comma separated form with a given n input by console ?\n", 110 | "`f(n)=0 if n=0\n", 111 | "f(n)=1 if n=1\n", 112 | "f(n)=f(n-1)+f(n-2) if n>1` " 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "id": "90585ee0", 119 | "metadata": { 120 | "id": "90585ee0", 121 | "outputId": "2e2aa54a-10a7-452f-ec05-fa65c4fdff6c" 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "def genFibonaci(in_num):\n", 134 | " if in_num == 0:\n", 135 | " return 0\n", 136 | " elif in_num == 1:\n", 137 | " return 1\n", 138 | " else:\n", 139 | " return genFibonaci(in_num-1)+genFibonaci(in_num-2)\n", 140 | " \n", 141 | "print([genFibonaci(x) for x in range(20)])" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "03d71fc6", 147 | "metadata": { 148 | "id": "03d71fc6" 149 | }, 150 | "source": [ 151 | "#### 4.Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only ?\n", 152 | "**Example:** \n", 153 | "If the following email address is given as input to the program: `john@google.com` \n", 154 | "Then, the output of the program should be: `john`" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 1, 160 | "id": "63af57a3", 161 | "metadata": { 162 | "colab": { 163 | "base_uri": "https://localhost:8080/" 164 | }, 165 | "id": "63af57a3", 166 | "outputId": "c09747ac-4066-452b-a52d-f07cb9d75736" 167 | }, 168 | "outputs": [ 169 | { 170 | "output_type": "stream", 171 | "name": "stdout", 172 | "text": [ 173 | "Enter Email Address(es): gaurav@gmail.com\n", 174 | "Username of gaurav@gmail.com is gaurav\n", 175 | "Enter Email Address(es): sudhanshu@ineuron.ai\n", 176 | "Username of sudhanshu@ineuron.ai is sudhanshu\n", 177 | "Enter Email Address(es): krish@ineuron.ai\n", 178 | "Username of krish@ineuron.ai is krish\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "def getUsernames():\n", 184 | " in_string = input('Enter Email Address(es): ')\n", 185 | " out_string = in_string.split('@')\n", 186 | " print(f'Username of {in_string} is {out_string[0]}')\n", 187 | "\n", 188 | "for i in range(3):\n", 189 | " getUsernames()" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "id": "8fbb0d51", 195 | "metadata": { 196 | "id": "8fbb0d51" 197 | }, 198 | "source": [ 199 | "#### 5.Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default." 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "id": "edcee9a6", 206 | "metadata": { 207 | "id": "edcee9a6", 208 | "outputId": "bff84f78-5a8a-4e12-be5e-82eb88aa354c" 209 | }, 210 | "outputs": [ 211 | { 212 | "name": "stdout", 213 | "output_type": "stream", 214 | "text": [ 215 | "2500\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "class Shape:\n", 221 | " def area(self):\n", 222 | " return 0\n", 223 | "\n", 224 | "class Square(Shape):\n", 225 | " def __init__(self, length):\n", 226 | " self.length = length\n", 227 | " def area(self):\n", 228 | " return self.length*self.length\n", 229 | "\n", 230 | "square = Square(50)\n", 231 | "print(square.area())" 232 | ] 233 | } 234 | ], 235 | "metadata": { 236 | "kernelspec": { 237 | "display_name": "Python 3 (ipykernel)", 238 | "language": "python", 239 | "name": "python3" 240 | }, 241 | "language_info": { 242 | "codemirror_mode": { 243 | "name": "ipython", 244 | "version": 3 245 | }, 246 | "file_extension": ".py", 247 | "mimetype": "text/x-python", 248 | "name": "python", 249 | "nbconvert_exporter": "python", 250 | "pygments_lexer": "ipython3", 251 | "version": "3.8.8" 252 | }, 253 | "colab": { 254 | "name": "Basic_Programming Assignment_15.ipynb", 255 | "provenance": [], 256 | "collapsed_sections": [] 257 | } 258 | }, 259 | "nbformat": 4, 260 | "nbformat_minor": 5 261 | } -------------------------------------------------------------------------------- /NLP/NLP_Assignment_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "NLP_Assignment_07.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "source": [ 22 | "**Submitted By: Gaurav Ratan**" 23 | ], 24 | "metadata": { 25 | "id": "WvuEP0kLpV8T" 26 | } 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "source": [ 31 | "Q1. Explain the architecture of BERT\n", 32 | "\n", 33 | "Ans: \n", 34 | "\n", 35 | "BERT stands for Bidirectional Encoder Representations from Transformers. It is designed to pre-train deep bidirectional representations from unlabeled text by jointly conditioning on both left and right context. BERT is based on the Transformer architecture\n", 36 | "\n", 37 | "![0_V0GyOt3LoDVfY7y5.png](https://miro.medium.com/max/876/0*ViwaI3Vvbnd-CJSQ.png)\n", 38 | "\n", 39 | "\n" 40 | ], 41 | "metadata": { 42 | "id": "-lko5IojpYrw" 43 | } 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "source": [ 48 | "Q2. Explain Masked Language Modeling (MLM)\n", 49 | "\n", 50 | "Ans:\n", 51 | "\n", 52 | "Masked language modeling (MLM), a self-supervised pretraining objective, is widely used in natural language processing for learning text representations. MLM trains a model to predict a random sample of input tokens that have been replaced by a [MASK] placeholder in a multi-class setting over the entire vocabulary.\n", 53 | "\n", 54 | "![0_V0GyOt3LoDVfY7y5.png](https://raw.githubusercontent.com/UKPLab/sentence-transformers/master/docs/img/MLM.png)\n" 55 | ], 56 | "metadata": { 57 | "id": "UfvPugnWpk7-" 58 | } 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "source": [ 63 | "Q3. Explain Next Sentence Prediction (NSP)\n", 64 | "\n", 65 | "Ans:\n", 66 | "\n", 67 | "Next sentence prediction (NSP) is one-half of the training process behind the BERT model (the other being masked-language modeling — MLM). Where MLM teaches BERT to understand relationships between words — NSP teaches BERT to understand longer-term dependencies across sentences.\n", 68 | "\n" 69 | ], 70 | "metadata": { 71 | "id": "iPdzOnT8pk5f" 72 | } 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "source": [ 77 | "Q4. What is Matthews evaluation?\n", 78 | "\n", 79 | "Ans:\n", 80 | "\n", 81 | "Matthew's correlation coefficient, also abbreviated as MCC was invented by Brian Matthews in 1975. MCC is a statistical tool used for model evaluation. Its job is to gauge or measure the difference between the predicted values and actual values and is equivalent to chi-square statistics for a 2 x 2 contingency table. A correlation of: C = 1 indicates perfect agreement, C = 0 is expected for a prediction no better than random, and C = -1 indicates total disagreement between prediction and observation\n", 82 | "\n", 83 | "![0_V0GyOt3LoDVfY7y5.png](https://miro.medium.com/max/875/1*8E2rPn_ccOqGuPYj1gBTAg.png)\n", 84 | "\n", 85 | "\n", 86 | "\n", 87 | "\n", 88 | "\n", 89 | "\n", 90 | "\n" 91 | ], 92 | "metadata": { 93 | "id": "fVP_o59x6Cmi" 94 | } 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "source": [ 99 | "Q5. What is Matthews Correlation Coefficient (MCC)?\n", 100 | "\n", 101 | "Ans:\n", 102 | "\n", 103 | "Matthew's correlation coefficient, also abbreviated as MCC was invented by Brian Matthews in 1975. MCC is a statistical tool used for model evaluation. Its job is to gauge or measure the difference between the predicted values and actual values and is equivalent to chi-square statistics for a 2 x 2 contingency table. A correlation of: C = 1 indicates perfect agreement, C = 0 is expected for a prediction no better than random, and C = -1 indicates total disagreement between prediction and observation\n", 104 | "\n", 105 | "![0_V0GyOt3LoDVfY7y5.png](https://miro.medium.com/max/875/1*8E2rPn_ccOqGuPYj1gBTAg.png)\n", 106 | "\n", 107 | "\n", 108 | "\n", 109 | "\n", 110 | "\n" 111 | ], 112 | "metadata": { 113 | "id": "6V1aotumpk3C" 114 | } 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "source": [ 119 | "Q6. Explain Semantic Role Labeling\n", 120 | "Ans:\n", 121 | "\n", 122 | "In natural language processing, semantic role labeling (also called shallow semantic parsing or slot-filling) is the process that assigns labels to words or phrases in a sentence that indicates their semantic role in the sentence, such as that of an agent, goal, or result.\n", 123 | "\n" 124 | ], 125 | "metadata": { 126 | "id": "x3bo17oM6RGp" 127 | } 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "source": [ 132 | "Q7. Why Fine-tuning a BERT model takes less time than pretraining\n", 133 | "\n", 134 | "Ans:\n", 135 | "\n", 136 | "Specifically, we will take the pre-trained BERT model, add an untrained layer of neurons on the end, and train the new model for our classification task. Why do this rather than train a train a specific deep learning model (a CNN, BiLSTM, etc.) that is well suited for the specific NLP task you need? When the model is trained on a large generic corpus, it is called 'pre-training'. When it is adapted to a particular task or dataset it is called as 'fine-tuning'. Hence Fine-tuning a BERT model takes less time than pretraining. Below are other reasons:\n", 137 | "\n", 138 | "#### Easy Training\n", 139 | "\n", 140 | "First, the pre-trained BERT model weights already encode a lot of information about our language. As a result, it takes much less time to train our fine-tuned model - it is as if we have already trained the bottom layers of our network extensively and only need to gently tune them while using their output as features for our classification task. In fact, the authors recommend only 2-4 epochs of training for fine-tuning BERT on a specific NLP task (compared to the hundreds of GPU hours needed to train the original BERT model or a LSTM from scratch!). \n", 141 | "\n", 142 | "#### Less Data\n", 143 | "\n", 144 | "In addition and perhaps just as important, because of the pre-trained weights this method allows us to fine-tune our task on a much smaller dataset than would be required in a model that is built from scratch. A major drawback of NLP models built from scratch is that we often need a prohibitively large dataset in order to train our network to reasonable accuracy, meaning a lot of time and energy had to be put into dataset creation. By fine-tuning BERT, we are now able to get away with training a model to good performance on a much smaller amount of training data.\n", 145 | "\n", 146 | "#### Good Results\n", 147 | "\n", 148 | "Second, this simple fine-tuning procedure (typically adding one fully-connected layer on top of BERT and training for a few epochs) was shown to achieve state of the art results with minimal task-specific adjustments for a wide variety of tasks: classification, language inference, semantic similarity, question answering, etc. Rather than implementing custom and sometimes-obscure architetures shown to work well on a specific task, simply fine-tuning BERT is shown to be a better (or at least equal) alternative.\n", 149 | "\n", 150 | "##Recognizing Textual Entailment (RTE)\n", 151 | "\n", 152 | "Textual entailment recognition is the task of deciding, given two text fragments, whether the meaning of one text is entailed (can be inferred) from another text (see the Instructions tab for the specific operational definition of textual entailment assumed in the challenge).\n", 153 | "\n", 154 | "##Explain the decoder stack of GPT models.\n", 155 | "\n", 156 | "GPT model was based on Transformer architecture. It was made of decoders stacked on top of each other (12 decoders). ... GPT model works on a principle called autoregressive which is similar to one used in RNN. It is a technique where the previous output becomes current input.\n" 157 | ], 158 | "metadata": { 159 | "id": "c-ylSkrghF_O" 160 | } 161 | } 162 | ] 163 | } -------------------------------------------------------------------------------- /NLP/NLP_Assignment_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "NLP_Assignment_06.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "source": [ 22 | "**Submitted By: Gaurav Ratan**" 23 | ], 24 | "metadata": { 25 | "id": "WvuEP0kLpV8T" 26 | } 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "source": [ 31 | "Q1. What are Vanilla autoencoders\n", 32 | "\n", 33 | "Ans: \n", 34 | "\n", 35 | "The vanilla autoencoder, as proposed by Hinton, consists of only one hidden layer. The number of neurons in the hidden layer is less than the number of neurons in the input (or output) layer. This results in producing a bottleneck effect on the flow of information in the network, and therefore we can think of the hidden layer as a bottleneck layer, restricting the information that would be stored.\n", 36 | "\n", 37 | "![0_V0GyOt3LoDVfY7y5.png](https://www.researchgate.net/profile/Guillaume-Alexandre-Bilodeau/publication/327434418/figure/fig2/AS:667345727025158@1536119028351/Diagram-showing-a-Vanilla-Autoencoder.png)\n", 38 | "\n", 39 | "\n" 40 | ], 41 | "metadata": { 42 | "id": "-lko5IojpYrw" 43 | } 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "source": [ 48 | "Q2. What are Sparse autoencoders\n", 49 | "\n", 50 | "\n", 51 | "\n", 52 | "\n", 53 | "\n", 54 | "Ans:\n", 55 | "\n", 56 | "A Sparse Autoencoder is a type of autoencoder that employs sparsity to achieve an information bottleneck. Specifically the loss function is constructed so that activations are penalized within a layer.\n", 57 | "\n", 58 | "![0_V0GyOt3LoDVfY7y5.png](https://www.researchgate.net/profile/Mohamed-Loey/publication/317734695/figure/fig1/AS:507899973730304@1498104198885/Sparse-autoencoder-structure.png)\n" 59 | ], 60 | "metadata": { 61 | "id": "UfvPugnWpk7-" 62 | } 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "source": [ 67 | "Q3. What are Denoisingautoencoders?\n", 68 | "\n", 69 | "Ans:\n", 70 | "\n", 71 | "Denoising autoencoders are an extension of the basic autoencoder, and represent a stochastic version of it. Denoising autoencoders attempt to address identity-function risk by randomly corrupting input (i.e. introducing noise) that the autoencoder must then reconstruct, or denoise.\n", 72 | "\n", 73 | "\n", 74 | "![0_V0GyOt3LoDVfY7y5.png](https://miro.medium.com/max/1400/0*ECdHu2yeal38Jl3P.png)\n" 75 | ], 76 | "metadata": { 77 | "id": "iPdzOnT8pk5f" 78 | } 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "source": [ 83 | "Q4. What are Convolutional autoencoders\n", 84 | "\n", 85 | "Ans:\n", 86 | "\n", 87 | "Convolutional Autoencoder is a variant of Convolutional Neural Networks that are used as the tools for unsupervised learning of convolution filters. They are generally applied in the task of image reconstruction to minimize reconstruction errors by learning the optimal filters.\n", 88 | "\n", 89 | "![0_V0GyOt3LoDVfY7y5.png](https://miro.medium.com/max/1400/1*KxeovF6fhyN12cH4vbpOEA.png)\n", 90 | "\n", 91 | "\n", 92 | "\n", 93 | "\n", 94 | "\n", 95 | "\n", 96 | "\n" 97 | ], 98 | "metadata": { 99 | "id": "fVP_o59x6Cmi" 100 | } 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "source": [ 105 | "Q5. What are Stacked autoencoders\n", 106 | "\n", 107 | "Ans:\n", 108 | "\n", 109 | "A stacked autoencoder is a neural network consist several layers of sparse autoencoders where output of each hidden layer is connected to the input of the successive hidden layer. ... The learned data from the previous layer is used as an input for the next layer and this continues until the training is completed.\n", 110 | "\n", 111 | "\n", 112 | "\n", 113 | "\n" 114 | ], 115 | "metadata": { 116 | "id": "6V1aotumpk3C" 117 | } 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "source": [ 122 | "Q6. Explain how to generate sentences using LSTM autoencoders\n", 123 | "Ans:\n", 124 | "\n", 125 | "Creating an LSTM Autoencoder in Keras can be achieved by implementing an Encoder-Decoder LSTM architecture and configuring the model to recreate the input sequence.\n", 126 | "\n" 127 | ], 128 | "metadata": { 129 | "id": "x3bo17oM6RGp" 130 | } 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "source": [ 135 | "Q7. Explain Extractive summarization\n", 136 | "\n", 137 | "Ans:\n", 138 | "\n", 139 | "Extractive summarization aims at identifying the salient information that is then extracted and grouped together to form a concise summary. Abstractive summary generation rewrites the entire document by building internal semantic representation, and then a summary is created using natural language processing." 140 | ], 141 | "metadata": { 142 | "id": "c-ylSkrghF_O" 143 | } 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "source": [ 148 | "Q8. Explain Abstractive summarization\n", 149 | "\n", 150 | "Ans:\n", 151 | "\n", 152 | "Abstractive summarization, on the other hand is a technique in which the summary is generated by generating novel sentences by either rephrasing or using the new words, instead of simply extracting the important sentences.\n" 153 | ], 154 | "metadata": { 155 | "id": "rHoq_IdwhF8f" 156 | } 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "source": [ 161 | "Q9. Explain Beam search\n", 162 | "\n", 163 | "Ans:\n", 164 | "\n", 165 | "Beam search is a heuristic search algorithm that explores a graph by expanding the most optimistic node in a limited set. Beam search is an optimization of best-first search that reduces its memory requirements.\n", 166 | "\n", 167 | "Best-first search is a graph search that orders all partial solutions according to some heuristic. But in beam search, only a predetermined number of best partial solutions are kept as candidates. Therefore, it is a greedy algorithm.\n", 168 | "\n", 169 | "Beam search uses breadth-first search to build its search tree. At each level of the tree, it generates all successors of the states at the current level, sorting them in increasing order of heuristic cost. However, it only stores a predetermined number (β), of best states at each level called the beamwidth. Only those states are expanded next.\n", 170 | "\n", 171 | "![0_V0GyOt3LoDVfY7y5.png](https://i.stack.imgur.com/1A9Ed.png)\n", 172 | "\n" 173 | ], 174 | "metadata": { 175 | "id": "Gdc99k7MhF59" 176 | } 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "source": [ 181 | "Q10. Explain Length normalization\n", 182 | "\n", 183 | "Ans:\n", 184 | "\n", 185 | "Document length normalization adjusts the term frequency or the relevance score in order to normalize the effect of document length on the document ranking.\n" 186 | ], 187 | "metadata": { 188 | "id": "ATb7INT8hF3V" 189 | } 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "source": [ 194 | "Q11.\tExplain Coverage normalization\n", 195 | "\n", 196 | "Ans:\n", 197 | "\n", 198 | "To calculate normalized coverage, the coverage is divided by the average coverage over all 10 base positions.The mean sequencing coverage required is calculated by dividing the desired coverage by the mean normalized coverage." 199 | ], 200 | "metadata": { 201 | "id": "fHg42KHIXl-O" 202 | } 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "source": [ 207 | "Q12.\tExplain ROUGE metric evaluation\n", 208 | "\n", 209 | "Ans:\n", 210 | "\n", 211 | "The Recall-Oriented Understudy for Gisting Evaluation (ROUGE) scoring algorithm evaluates the similarity between a candidate document and a collection of reference documents. Use the ROUGE score to evaluate the quality of document translation and summarization models." 212 | ], 213 | "metadata": { 214 | "id": "8Ye6z24aXl6B" 215 | } 216 | } 217 | ] 218 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "1juCtHy0MTmr" 7 | }, 8 | "source": [ 9 | "# Assignment 1 Solutions" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "source": [ 15 | "SUBMITTED BY: GAURAV RATAN" 16 | ], 17 | "metadata": { 18 | "id": "rR4LhqjQMcu7" 19 | } 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "id": "sGQvaucXMTmt" 25 | }, 26 | "source": [ 27 | "#### 1.Write a Python program to print 'Hello Python' ?" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "metadata": { 34 | "colab": { 35 | "base_uri": "https://localhost:8080/" 36 | }, 37 | "id": "NZOlfAazMTmv", 38 | "outputId": "c359614d-c42f-4b27-fc64-805ca5079e22" 39 | }, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "name": "stdout", 44 | "text": [ 45 | "Hello Python\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "print('Hello Python')" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": { 56 | "id": "KYF_AKGIMTmx" 57 | }, 58 | "source": [ 59 | "#### 2.Write a Python program to do arithmetic operations addition and division ?" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": { 66 | "id": "7OJCa3vBMTmy", 67 | "outputId": "1b8f25f1-7083-43f2-a085-80f4210ba3a1" 68 | }, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "Select a Arithmetic Operation: \n", 75 | "1.Addition(+) \n", 76 | "2.Division(-) \n", 77 | "2.Multiplication(*) \n", 78 | "4.Division(/) \n", 79 | "3.Stop(0)\n", 80 | "\n", 81 | "Enter a arithmetic operation -> +\n", 82 | "\n", 83 | "Enter 1st Number: 10\n", 84 | "Enter 2nd Number: 20\n", 85 | "10+20=30\n", 86 | "\n", 87 | "Enter a arithmetic operation -> -\n", 88 | "\n", 89 | "Enter 1st Number: 10\n", 90 | "Enter 2nd Number: 20\n", 91 | "10-20=-10\n", 92 | "\n", 93 | "Enter a arithmetic operation -> *\n", 94 | "\n", 95 | "Enter 1st Number: 20\n", 96 | "Enter 2nd Number: 10\n", 97 | "20*10=200\n", 98 | "\n", 99 | "Enter a arithmetic operation -> /\n", 100 | "\n", 101 | "Enter 1st Number: 200\n", 102 | "Enter 2nd Number: 2\n", 103 | "200/2=100.0\n", 104 | "\n", 105 | "Enter a arithmetic operation -> 0\n", 106 | "Program Stopped successfully\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "import operator\n", 112 | "\n", 113 | "ops = { \"+\": operator.add, \"-\": operator.sub, \"*\":operator.mul, \"/\":operator.truediv } \n", 114 | "\n", 115 | "print('Select a Arithmetic Operation: \\\n", 116 | " \\n1.Addition(+)\\\n", 117 | " \\n2.Division(-)\\\n", 118 | " \\n2.Multiplication(*)\\\n", 119 | " \\n4.Division(/)\\\n", 120 | " \\n3.Stop(0)\\n')\n", 121 | " \n", 122 | "\n", 123 | "while True:\n", 124 | " operator = input('Enter a arithmetic operation -> ')\n", 125 | " if operator == '0':\n", 126 | " print(\"Program Stopped successfully\")\n", 127 | " break\n", 128 | " elif operator not in ['+','-','*','/']:\n", 129 | " print(\"Please enter a valid operator\")\n", 130 | " else:\n", 131 | " num_1 = int(input('\\nEnter 1st Number: '))\n", 132 | " num_2 = int(input('Enter 2nd Number: '))\n", 133 | " print('{}{}{}={}\\n'.format(num_1, operator, num_2, ops[operator](num_1,num_2)))\n" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": { 139 | "id": "chxrmHnUMTm0" 140 | }, 141 | "source": [ 142 | "#### 3.Write a Python program to find the area of a triangle ?" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": { 149 | "id": "znzsvzh8MTm0", 150 | "outputId": "9fdfb6b3-9b25-4fa4-e490-19cbfcea8f01" 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "Enter height of triangle: 100\n", 158 | "Enter base of triangle: 50\n", 159 | "\n", 160 | "Area of triangle -> 2500.0\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "height = int(input('Enter height of triangle: '))\n", 166 | "base = int(input('Enter base of triangle: '))\n", 167 | "\n", 168 | "def areaOfTriangle(height, base):\n", 169 | " print('\\nArea of triangle ->', 0.5*height*base)\n", 170 | "\n", 171 | "areaOfTriangle(height,base)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": { 177 | "id": "ToQ0TRQ2MTm1" 178 | }, 179 | "source": [ 180 | "#### 4.Write a Python program to swap two variables ?" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": { 187 | "id": "sxErkaJgMTm2", 188 | "outputId": "fb933ad5-ff8d-4d5c-8bea-98145f5a8860" 189 | }, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "Enter First Number: 100\n", 196 | "Enter Second Number: 200\n", 197 | "Before swapping -> 100 200\n", 198 | "After swapping -> 200 100\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "num_1 = int(input(\"Enter First Number: \"))\n", 204 | "num_2 = int(input(\"Enter Second Number: \"))\n", 205 | "\n", 206 | "def swapNumbers(a,b):\n", 207 | " temp = a\n", 208 | " a = b\n", 209 | " b = temp\n", 210 | " return a,b\n", 211 | "\n", 212 | "print('Before swapping -> ',num_1, num_2)\n", 213 | "num_1, num_2 = swapNumbers(num_1, num_2)\n", 214 | "print('After swapping -> ',num_1,num_2)\n", 215 | "\n", 216 | " " 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": { 222 | "id": "3mYhNcG_MTm4" 223 | }, 224 | "source": [ 225 | "#### 5.Write a Python program to generate a random number ?" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": null, 231 | "metadata": { 232 | "id": "qnRMbKQLMTm5", 233 | "outputId": "d84f3c57-b983-4339-851f-b3eca36fd1e1" 234 | }, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "Random number -> 75610\n", 241 | "Random number -> 34\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "from random import randint\n", 247 | "\n", 248 | "def generateRandomNumber(start=0, end=100000):\n", 249 | " print('Random number -> ',randint(start,end))\n", 250 | "\n", 251 | "# Generating random numbers without arguments \n", 252 | "generateRandomNumber()\n", 253 | "\n", 254 | "# Generating random numbers with arguments \n", 255 | "generateRandomNumber(0,100)" 256 | ] 257 | } 258 | ], 259 | "metadata": { 260 | "kernelspec": { 261 | "display_name": "Python 3", 262 | "language": "python", 263 | "name": "python3" 264 | }, 265 | "language_info": { 266 | "codemirror_mode": { 267 | "name": "ipython", 268 | "version": 3 269 | }, 270 | "file_extension": ".py", 271 | "mimetype": "text/x-python", 272 | "name": "python", 273 | "nbconvert_exporter": "python", 274 | "pygments_lexer": "ipython3", 275 | "version": "3.8.8" 276 | }, 277 | "colab": { 278 | "name": "Basic_Programming Assignment_01.ipynb", 279 | "provenance": [], 280 | "collapsed_sections": [] 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 0 285 | } -------------------------------------------------------------------------------- /Python/Advance Python/Advance_Assignment_16.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "1b34bd2a", 6 | "metadata": { 7 | "id": "1b34bd2a" 8 | }, 9 | "source": [ 10 | "# Assignment 16 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "xjTkVvoDWMIw" 20 | }, 21 | "id": "xjTkVvoDWMIw" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "f8313cc0", 26 | "metadata": { 27 | "id": "f8313cc0" 28 | }, 29 | "source": [ 30 | "#### Q1. What is the benefit of regular expressions?\n", 31 | "**Ans:** Regular Expressions, also known as **`regex`** or **`regexp`**, are used to match strings of text such as particular characters, words, or patterns of characters. It means that we can match and extract any string pattern from the text with the help of regular expressions. it helps the programmers to Write less and cleaner code. it also avoids multiple use of **`if/else`** statements. Regular expressions are a powerful feature of any language and solve a particular class of problems extraordinarily well. They can be used to validate user input, extract relevant data from a string, or even modify a string based on its contents.\n", 32 | "\n", 33 | "Python's regular expression library is called \"re\" and is based on Perl's regular expression engine, which is pretty much an industry standard now." 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "3ec69ca0", 39 | "metadata": { 40 | "id": "3ec69ca0" 41 | }, 42 | "source": [ 43 | "#### Q2. Describe the difference between the effects of \"(ab)c+\" and \"a(bc)+\" Which of these, if any, is the unqualified pattern \"abc+\"?\n", 44 | "**Ans:** Both **`(ab)c+`** and **`a(bc)+`** are valid patterns. The difference between both these patterns is that in **`(ab)c+`** **`ab`** is a group whereas in **`a(bc)+`** **`bc`** is a group. Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "2b4c8728", 50 | "metadata": { 51 | "id": "2b4c8728" 52 | }, 53 | "source": [ 54 | "#### Q3. How much do you need to use the following sentence while using regular expressions?\n", 55 | "**`import re`** \n", 56 | "\n", 57 | "**Ans:** **`import re`** statement always has to be imported before using regular expressions. In a program, we need to import this once so that we can use Regular Expressions subsequently." 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "id": "2338be46", 63 | "metadata": { 64 | "id": "2338be46" 65 | }, 66 | "source": [ 67 | "#### Q4. Which characters have special significance in square brackets when expressing a range, and under what circumstances?\n", 68 | "**Ans:** The Characters **`.`,`*`,`?`,`^`**,or,**`()`**, have a special signiface when used with square brackets. They need not be be explicitly escaped by **`\\`** as in case of pattern texts in a raw string. Range in Regular Expressions Ranges of characters can be indicated by giving two characters and separating them by a ‘-‘, for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59." 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "bc0c3bef", 74 | "metadata": { 75 | "id": "bc0c3bef" 76 | }, 77 | "source": [ 78 | "#### Q5. How does compiling a regular-expression object benefit you?\n", 79 | "**Ans:** We can Combine a regular expression pattern into pattern Objects which can be used for pattern matching. It also helps to search a pattern again without rewriting it." 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "c2885976", 85 | "metadata": { 86 | "id": "c2885976" 87 | }, 88 | "source": [ 89 | "#### Q6. What are some examples of how to use the match object returned by re.match and re.search?\n", 90 | "**Ans:** The **`re.search()`** and **`re.match()`** both are functions of re module in python. These functions are very efficient and fast for searching in strings. The function searches for some substring in a string and returns a match object if found, else it returns none.\n", 91 | "\n", 92 | "There is a difference between the use of both functions. Both return the first match of a substring found in the string, but **`re.match()`** searches only from the beginning of the string and return match object if found. But if a match of substring is found somewhere in the middle of the string, it returns none. \n", 93 | "\n", 94 | "Where as **`re.search()`** searches for the whole string even if the string contains multi-lines and tries to find a match of the substring in all the lines of string" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 1, 100 | "id": "358eb508", 101 | "metadata": { 102 | "colab": { 103 | "base_uri": "https://localhost:8080/" 104 | }, 105 | "id": "358eb508", 106 | "outputId": "dcedc7c5-36a0-4146-e330-c79983ca564e" 107 | }, 108 | "outputs": [ 109 | { 110 | "output_type": "stream", 111 | "name": "stdout", 112 | "text": [ 113 | "\n", 114 | "None\n", 115 | "\n", 116 | "\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "import re\n", 122 | "Substring ='string' \n", 123 | "String1 ='We are learning regex with iNeuron. Regex is very useful for string matching. It is fast too.' \n", 124 | "String2 ='string We are learning regex with iNeuron. Regex is very useful for string matching. It is fast too.'\n", 125 | "print(re.search(Substring, String1, re.IGNORECASE))\n", 126 | "print(re.match(Substring, String1, re.IGNORECASE))\n", 127 | "print(re.search(Substring, String2, re.IGNORECASE))\n", 128 | "print(re.match(Substring, String2, re.IGNORECASE))" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "id": "babed21c", 134 | "metadata": { 135 | "id": "babed21c" 136 | }, 137 | "source": [ 138 | "#### Q7. What is the difference between using a vertical bar (|) as an alteration and using square brackets as a character set?\n", 139 | "**Ans:** When **`|`** us used then patterns searches for **`or`** option. i.e **`|`** means it searches as **`or<`** in the searched string. The first occurance of matched string will be returned as the Match Object.\n", 140 | "Using Character set in square Brackets searches for all the character set in the square bracket and if match is found, it returns it.\n", 141 | "\n", 142 | "\n", 143 | "\n", 144 | " " 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "source": [ 150 | "#For example\n", 151 | "#[p – t] = [ pqrst ]\n", 152 | "#For example, (x|y|z)ab match by any string-like, x, y, z, a, b." 153 | ], 154 | "metadata": { 155 | "id": "cQquj1zMYZsB" 156 | }, 157 | "id": "cQquj1zMYZsB", 158 | "execution_count": 2, 159 | "outputs": [] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "id": "aad2feb4", 164 | "metadata": { 165 | "id": "aad2feb4" 166 | }, 167 | "source": [ 168 | "#### Q8. In regular-expression search patterns, why is it necessary to use the raw-string indicator (r)? In replacement strings?\n", 169 | "**Ans:** Raw Strings are used in the regular-expression search patterns, so that backslash do not have to be escaped. When one wants to match a literal backslash, it must be escaped in the regular expression. With raw string notation, this means r\"\\\\\". Without raw string notation, one must use 4 times back slashes" 170 | ] 171 | } 172 | ], 173 | "metadata": { 174 | "kernelspec": { 175 | "display_name": "Python 3", 176 | "language": "python", 177 | "name": "python3" 178 | }, 179 | "language_info": { 180 | "codemirror_mode": { 181 | "name": "ipython", 182 | "version": 3 183 | }, 184 | "file_extension": ".py", 185 | "mimetype": "text/x-python", 186 | "name": "python", 187 | "nbconvert_exporter": "python", 188 | "pygments_lexer": "ipython3", 189 | "version": "3.6.13" 190 | }, 191 | "colab": { 192 | "name": "Advance_Assignment_16.ipynb", 193 | "provenance": [], 194 | "collapsed_sections": [] 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 5 199 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_20.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "660b64a3", 6 | "metadata": { 7 | "id": "660b64a3" 8 | }, 9 | "source": [ 10 | "# Assignment 20 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "N_7VCzi9EyK5" 20 | }, 21 | "id": "N_7VCzi9EyK5" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "a8e30a46", 26 | "metadata": { 27 | "id": "a8e30a46" 28 | }, 29 | "source": [ 30 | "#### 1.Create a function that takes a list of strings and integers, and filters out the list so that it returns a list of integers only.\n", 31 | "**Examples:** \n", 32 | "`filter_list([1, 2, 3, \"a\", \"b\", 4]) ➞ [1, 2, 3, 4]` \n", 33 | "`filter_list([\"A\", 0, \"Edabit\", 1729, \"Python\", \"1729\"]) ➞ [0, 1729]` \n", 34 | "`filter_list([\"Nothing\", \"here\"]) ➞ []` " 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "id": "ec956bcf", 41 | "metadata": { 42 | "id": "ec956bcf", 43 | "outputId": "02de6301-823b-4973-c5c7-3f5fe1aa63a8" 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Output ➞ [1, 2, 3, 4]\n", 51 | "Output ➞ [0, 1729]\n", 52 | "Output ➞ []\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "def filter_list(in_list):\n", 58 | " out_list = []\n", 59 | " for ele in in_list:\n", 60 | " if type(ele) == int:\n", 61 | " out_list.append(ele)\n", 62 | " print(f'Output ➞ {out_list}')\n", 63 | "\n", 64 | "filter_list([1, 2, 3, \"a\", \"b\", 4])\n", 65 | "filter_list([\"A\", 0, \"Edabit\", 1729, \"Python\", \"1729\"])\n", 66 | "filter_list([\"Nothing\", \"here\"])" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "aa411a6b", 72 | "metadata": { 73 | "id": "aa411a6b" 74 | }, 75 | "source": [ 76 | "#### 2.Given a list of numbers, create a function which returns the list but with each element's index in the list added to itself. This means you add 0 to the number at index 0, add 1 to the number at index 1, etc...\n", 77 | "**Examples:** \n", 78 | "`add_indexes([0, 0, 0, 0, 0]) ➞ [0, 1, 2, 3, 4]` \n", 79 | "`add_indexes([1, 2, 3, 4, 5]) ➞ [1, 3, 5, 7, 9]` \n", 80 | "`add_indexes([5, 4, 3, 2, 1]) ➞ [5, 5, 5, 5, 5]` " 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "id": "07906f6f", 87 | "metadata": { 88 | "id": "07906f6f", 89 | "outputId": "32b09214-67c3-4717-e679-d72a753a31e8" 90 | }, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "[0, 0, 0, 0, 0] ➞ [0, 1, 2, 3, 4]\n", 97 | "[1, 2, 3, 4, 5] ➞ [1, 3, 5, 7, 9]\n", 98 | "[5, 4, 3, 2, 1] ➞ [5, 5, 5, 5, 5]\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "def add_indexes(in_list):\n", 104 | " out_list = []\n", 105 | " for ele in range(len(in_list)):\n", 106 | " out_list.append(ele+in_list[ele])\n", 107 | " print(f'{in_list} ➞ {out_list}')\n", 108 | " \n", 109 | "add_indexes([0, 0, 0, 0, 0])\n", 110 | "add_indexes([1, 2, 3, 4, 5])\n", 111 | "add_indexes([5, 4, 3, 2, 1])" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "id": "37ed4057", 117 | "metadata": { 118 | "id": "37ed4057" 119 | }, 120 | "source": [ 121 | "#### 3.Create a function that takes the height and radius of a cone as arguments and returns the volume of the cone rounded to the nearest hundredth. See the resources tab for the formula.\n", 122 | "**Examples:** \n", 123 | "`cone_volume(3, 2) ➞ 12.57` \n", 124 | "`cone_volume(15, 6) ➞ 565.49` \n", 125 | "`cone_volume(18, 0) ➞ 0` " 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "id": "a78639a2", 132 | "metadata": { 133 | "id": "a78639a2", 134 | "outputId": "4b72d677-1ee5-4123-953f-423abf44b15a" 135 | }, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "Output ➞ 12.57\n", 142 | "Output ➞ 565.49\n", 143 | "Output ➞ 0.00\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "import math\n", 149 | "\n", 150 | "def cube_volume(height, radius):\n", 151 | " output = ((math.pi)*pow(radius,2))*(height/3)\n", 152 | " print(f'Output ➞ {output:.2f}')\n", 153 | "\n", 154 | "cube_volume(3,2) \n", 155 | "cube_volume(15,6) \n", 156 | "cube_volume(18,0) " 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "id": "00f3960c", 162 | "metadata": { 163 | "id": "00f3960c" 164 | }, 165 | "source": [ 166 | "#### 4.This Triangular Number Sequence is generated from a pattern of dots that form a triangle.\n", 167 | "The first 5 numbers of the sequence, or dots, are: `1, 3, 6, 10, 15` \n", 168 | "This means that the first triangle has just one dot, the second one has three dots, the third one has 6 dots and so on.\n", 169 | "Write a function that gives the number of dots with its corresponding triangle number of the sequence. \n", 170 | "\n", 171 | "**Examples:** \n", 172 | "`triangle(1) ➞ 1` \n", 173 | "`triangle(6) ➞ 21` \n", 174 | "`triangle(215) ➞ 23220` " 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "id": "8b9c27de", 181 | "metadata": { 182 | "id": "8b9c27de", 183 | "outputId": "ad037ba2-18d4-40c4-b2e1-30adc2d4be59" 184 | }, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Output ➞ 1\n", 191 | "Output ➞ 21\n", 192 | "Output ➞ 23220\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "def triangle(in_num):\n", 198 | " print(f'Output ➞ {int((in_num)*((in_num+1)/2))}')\n", 199 | "\n", 200 | "triangle(1)\n", 201 | "triangle(6)\n", 202 | "triangle(215)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "id": "7cca0905", 208 | "metadata": { 209 | "id": "7cca0905" 210 | }, 211 | "source": [ 212 | "#### 5.Create a function that takes a list of numbers between 1 and 10 (excluding one number) and returns the missing number.\n", 213 | "**Examples:** \n", 214 | "`missing_num([1, 2, 3, 4, 6, 7, 8, 9, 10]) ➞ 5` \n", 215 | "`missing_num([7, 2, 3, 6, 5, 9, 1, 4, 8]) ➞ 10` \n", 216 | "`missing_num([10, 5, 1, 2, 4, 6, 8, 3, 9]) ➞ 7` " 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "id": "ea3bb881", 223 | "metadata": { 224 | "id": "ea3bb881", 225 | "outputId": "812b90a2-2041-4501-921e-781e67cdbe00" 226 | }, 227 | "outputs": [ 228 | { 229 | "name": "stdout", 230 | "output_type": "stream", 231 | "text": [ 232 | "[1, 2, 3, 4, 6, 7, 8, 9, 10] ➞ 5\n", 233 | "[7, 2, 3, 6, 5, 9, 1, 4, 8] ➞ 10\n", 234 | "[10, 5, 1, 2, 4, 6, 8, 3, 9] ➞ 7\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "def missing_num(in_list):\n", 240 | " for i in range(1,11):\n", 241 | " if i not in in_list:\n", 242 | " print(f'{in_list} ➞ {i}')\n", 243 | "\n", 244 | "missing_num([1, 2, 3, 4, 6, 7, 8, 9, 10])\n", 245 | "missing_num([7, 2, 3, 6, 5, 9, 1, 4, 8])\n", 246 | "missing_num([10, 5, 1, 2, 4, 6, 8, 3, 9])" 247 | ] 248 | } 249 | ], 250 | "metadata": { 251 | "kernelspec": { 252 | "display_name": "Python 3", 253 | "language": "python", 254 | "name": "python3" 255 | }, 256 | "language_info": { 257 | "codemirror_mode": { 258 | "name": "ipython", 259 | "version": 3 260 | }, 261 | "file_extension": ".py", 262 | "mimetype": "text/x-python", 263 | "name": "python", 264 | "nbconvert_exporter": "python", 265 | "pygments_lexer": "ipython3", 266 | "version": "3.8.8" 267 | }, 268 | "colab": { 269 | "name": "Basic_Programming Assignment_20.ipynb", 270 | "provenance": [] 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 5 275 | } -------------------------------------------------------------------------------- /Python/Basic Python/Python_Basics_Assignment_23.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "372ee7a9", 6 | "metadata": { 7 | "id": "372ee7a9" 8 | }, 9 | "source": [ 10 | "# Assignment 23 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "zmio8PZFoIOe" 20 | }, 21 | "id": "zmio8PZFoIOe" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "cbe5add1", 26 | "metadata": { 27 | "id": "cbe5add1" 28 | }, 29 | "source": [ 30 | "#### 1. What is the result of the code, and why?\n", 31 | "`>>> def func(a, b=6, c=8):` \n", 32 | "`print(a, b, c)` \n", 33 | "`>>> func(1, 2)` \n", 34 | "**Ans:** The result of the above code is `1 2 8`. It is because the function uses the default value of c i.e 8 which is provided at the time of declaration. We can see this below:" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "id": "25277357", 41 | "metadata": { 42 | "id": "25277357", 43 | "outputId": "c869f4e2-854a-4d9a-a28e-c2de64adf7af" 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "1 2 8\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "def func(a,b=6,c=8):\n", 56 | " print(a,b,c)\n", 57 | "func(1,2)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "id": "9fcdfe9e", 63 | "metadata": { 64 | "id": "9fcdfe9e" 65 | }, 66 | "source": [ 67 | "#### 2. What is the result of this code, and why?\n", 68 | "`>>> def func(a, b, c=5):` \n", 69 | "`print(a, b, c)` \n", 70 | "`>>> func(1, c=3, b=2)` \n", 71 | "**Ans:** The result of the above code is `1 2 3`. It is because the function will use default values only when a value for a argument is not provided and if the argument name is mentioned while doing a function call, the order of arguments is also ignored by the python interpreter" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "id": "dfb849ce", 78 | "metadata": { 79 | "id": "dfb849ce", 80 | "outputId": "e1dd1981-04ad-4155-d0c0-da4c8ca8baae" 81 | }, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "1 2 3\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "def func(a,b,c=5):\n", 93 | " print(a,b,c)\n", 94 | "func(1,c=3,b=2)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "d030f9f1", 100 | "metadata": { 101 | "id": "d030f9f1" 102 | }, 103 | "source": [ 104 | "#### 3. How about this code: what is its result, and why?\n", 105 | "`>>> def func(a, *pargs):` \n", 106 | "`print(a, pargs)` \n", 107 | "`>>> func(1, 2, 3)` \n", 108 | "**Ans:** The result of the code is `1 (2,3)`. `*pargs` stands for variable length arguments. This format is used when we are not sure about the number of arguments to be passed to a function. All the values under this argument will be stored in a tuple." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "id": "82ddc925", 115 | "metadata": { 116 | "id": "82ddc925", 117 | "outputId": "5eec7570-55d9-46b1-8e25-f6bb30fe19e4" 118 | }, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "1 (2, 3)\n" 125 | ] 126 | } 127 | ], 128 | "source": [ 129 | "def func(a, *pargs):\n", 130 | " print(a,pargs)\n", 131 | "func(1,2,3)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "id": "c1c3848b", 137 | "metadata": { 138 | "id": "c1c3848b" 139 | }, 140 | "source": [ 141 | "#### 4. What does this code print, and why?\n", 142 | "`>>> def func(a, **kargs):` \n", 143 | "`print(a, kargs)` \n", 144 | "`>>> func(a=1, c=3, b=2)` \n", 145 | "**Ans:** The result of the above code is `1 {'c': 3, 'b': 2}`. `**args` stands for variable length keyword arguments. This format is used when we want pass key value pairs as input to a function. All these key value pairs will be stored in a dictionary" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "53f34da6", 152 | "metadata": { 153 | "id": "53f34da6", 154 | "outputId": "b167e132-5640-43fb-d5a1-7923a70b81e9" 155 | }, 156 | "outputs": [ 157 | { 158 | "name": "stdout", 159 | "output_type": "stream", 160 | "text": [ 161 | "1 {'c': 3, 'b': 2}\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "def func(a,**kargs):\n", 167 | " print(a,kargs)\n", 168 | "func(a=1,c=3,b=2)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "id": "473e1bdf", 174 | "metadata": { 175 | "id": "473e1bdf" 176 | }, 177 | "source": [ 178 | "#### 5. What gets printed by this, and explain?\n", 179 | "`>>> def func(a, b, c=8, d=5): print(a, b, c, d)` \n", 180 | "`>>> func(1, *(5, 6))` \n", 181 | " **Ans:** The output of the above is `1 5 6 5`. The reason for this function not throwing an error is because, this function expects 4 arguments. The value for a is provided explicitly whereas for arguments b and c, the function will expand `*(5,6)` and consider the value of b as 5 and value of c as 6. Since the default value of d is provided in function declaration, d value will be 5. However it is recommended to use the feature of positional arguments at the end." 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "id": "ab5de589", 188 | "metadata": { 189 | "id": "ab5de589", 190 | "outputId": "22916abd-9a1b-4729-fd33-8f964a391f28" 191 | }, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "1 5 6 5\n" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "def func(a,b,c=8,d=5):\n", 203 | " print(a,b,c,d)\n", 204 | "func(1,*(5,6))" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "id": "e6fd179c", 210 | "metadata": { 211 | "id": "e6fd179c" 212 | }, 213 | "source": [ 214 | "#### 6. what is the result of this, and explain?\n", 215 | "`>>> def func(a, b, c): a = 2; b[0] = 'x'; c['a'] = 'y'` \n", 216 | "`>>> l=1; m=[1]; n={'a':0}` \n", 217 | "`>>> func(l, m, n)` \n", 218 | "`>>> l, m, n` \n", 219 | "**Ans:** The output of above code is `1, ['x'], {'a': 'y'}`.\n", 220 | "1. Even though Python gives importance to indentation, it provides a facility to declare an entire function in one single line where statements in a function body are sepereated by `;`\n", 221 | "2. When `l,m,n` are provided as inputs to the function, it modifies the values of l,m,n and sets the value of `l=2` ,`m=['x']` and `n={'a':'y'}`" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "id": "3a23e9c9", 228 | "metadata": { 229 | "id": "3a23e9c9", 230 | "outputId": "de21cc37-9143-4088-d6dc-52348464f72b" 231 | }, 232 | "outputs": [ 233 | { 234 | "data": { 235 | "text/plain": [ 236 | "(1, ['x'], {'a': 'y'})" 237 | ] 238 | }, 239 | "execution_count": 6, 240 | "metadata": {}, 241 | "output_type": "execute_result" 242 | } 243 | ], 244 | "source": [ 245 | "def func(a, b, c): a = 2; b[0] = 'x'; c['a'] = 'y'\n", 246 | "l=1; m=[1]; n={'a':0}\n", 247 | "func(l, m, n)\n", 248 | "l,m,n" 249 | ] 250 | } 251 | ], 252 | "metadata": { 253 | "kernelspec": { 254 | "display_name": "Python 3", 255 | "language": "python", 256 | "name": "python3" 257 | }, 258 | "language_info": { 259 | "codemirror_mode": { 260 | "name": "ipython", 261 | "version": 3 262 | }, 263 | "file_extension": ".py", 264 | "mimetype": "text/x-python", 265 | "name": "python", 266 | "nbconvert_exporter": "python", 267 | "pygments_lexer": "ipython3", 268 | "version": "3.8.8" 269 | }, 270 | "colab": { 271 | "name": "Python Basics Assignment_23.ipynb", 272 | "provenance": [], 273 | "collapsed_sections": [] 274 | } 275 | }, 276 | "nbformat": 4, 277 | "nbformat_minor": 5 278 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_23.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "fde597ec", 6 | "metadata": { 7 | "id": "fde597ec" 8 | }, 9 | "source": [ 10 | "# Assignment 23 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "FqDkAzcFaa50" 20 | }, 21 | "id": "FqDkAzcFaa50" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "3b8368dc", 26 | "metadata": { 27 | "id": "3b8368dc" 28 | }, 29 | "source": [ 30 | "#### 1.Create a function that takes a number as an argument and returns True or False depending on whether the number is symmetrical or not. A number is symmetrical when it is the same as its reverse.\n", 31 | "**Examples:** \n", 32 | "`is_symmetrical(7227) ➞ True` \n", 33 | "`is_symmetrical(12567) ➞ False` \n", 34 | "`is_symmetrical(44444444) ➞ True` \n", 35 | "`is_symmetrical(9939) ➞ False` \n", 36 | "`is_symmetrical(1112111) ➞ True` " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "c26dbf7e", 43 | "metadata": { 44 | "id": "c26dbf7e", 45 | "outputId": "bb6fbccf-8931-42ca-d01b-e547dcb9165c" 46 | }, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "7227 ➞ True\n", 53 | "12567 ➞ False\n", 54 | "44444444 ➞ True\n", 55 | "9939 ➞ False\n", 56 | "1112111 ➞ True\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "def is_symmetrical(in_num):\n", 62 | " if str(in_num) == str(in_num)[::-1]:\n", 63 | " print(f'{in_num} ➞ {True}')\n", 64 | " else:\n", 65 | " print(f'{in_num} ➞ {False}')\n", 66 | "\n", 67 | "is_symmetrical(7227)\n", 68 | "is_symmetrical(12567)\n", 69 | "is_symmetrical(44444444)\n", 70 | "is_symmetrical(9939)\n", 71 | "is_symmetrical(1112111)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "id": "45b97989", 77 | "metadata": { 78 | "id": "45b97989" 79 | }, 80 | "source": [ 81 | "#### 2.Given a string of numbers separated by a comma and space, return the product of the numbers.\n", 82 | "**Examples:** \n", 83 | "`multiply_nums(\"2, 3\") ➞ 6` \n", 84 | "`multiply_nums(\"1, 2, 3, 4\") ➞ 24` \n", 85 | "`multiply_nums(\"54, 75, 453, 0\") ➞ 0` \n", 86 | "`multiply_nums(\"10, -2\") ➞ -20` " 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "60133913", 93 | "metadata": { 94 | "id": "60133913", 95 | "outputId": "a469f52d-8ea4-4777-f310-93c308f967f7" 96 | }, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "2, 3 ➞ 6\n", 103 | "1, 2, 3, 4 ➞ 24\n", 104 | "54, 75, 453, 0 ➞ 0\n", 105 | "10, -2 ➞ -20\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "def multiply_nums(in_string):\n", 111 | " out_string = in_string.replace(' ','').split(',')\n", 112 | " out_num = 1\n", 113 | " for ele in out_string:\n", 114 | " out_num *= int(ele)\n", 115 | " print(f'{in_string} ➞ {out_num}')\n", 116 | " \n", 117 | "multiply_nums(\"2, 3\")\n", 118 | "multiply_nums(\"1, 2, 3, 4\")\n", 119 | "multiply_nums(\"54, 75, 453, 0\")\n", 120 | "multiply_nums(\"10, -2\")" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "id": "42e17e6f", 126 | "metadata": { 127 | "id": "42e17e6f" 128 | }, 129 | "source": [ 130 | "#### 3.Create a function that squares every digit of a number.\n", 131 | "**Examples:** \n", 132 | "`square_digits(9119) ➞ 811181` \n", 133 | "`square_digits(2483) ➞ 416649` \n", 134 | "`square_digits(3212) ➞ 9414` \n", 135 | "\n", 136 | "**Notes:** \n", 137 | "The function receives an integer and must return an integer." 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "id": "fb0d6d2b", 144 | "metadata": { 145 | "id": "fb0d6d2b", 146 | "outputId": "f7f3c46e-e925-456a-ffbb-5936de1d59ee" 147 | }, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "9119 ➞ 811181\n", 154 | "2483 ➞ 416649\n", 155 | "3212 ➞ 9414\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "def square_digits(in_num):\n", 161 | " in_list = [str(int(ele)**2) for ele in str(in_num)]\n", 162 | " out_list = ''.join(in_list)\n", 163 | " print(f'{in_num} ➞ {int(out_list)}')\n", 164 | "\n", 165 | "square_digits(9119)\n", 166 | "square_digits(2483)\n", 167 | "square_digits(3212)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "id": "5edc0e1b", 173 | "metadata": { 174 | "id": "5edc0e1b" 175 | }, 176 | "source": [ 177 | "#### 4.Create a function that sorts a list and removes all duplicate items from it.\n", 178 | "**Examples:** \n", 179 | "`setify([1, 3, 3, 5, 5]) ➞ [1, 3, 5]` \n", 180 | "`setify([4, 4, 4, 4]) ➞ [4]` \n", 181 | "`setify([5, 7, 8, 9, 10, 15]) ➞ [5, 7, 8, 9, 10, 15]` \n", 182 | "`setify([3, 3, 3, 2, 1]) ➞ [1, 2, 3]` " 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "id": "8ca50bbd", 189 | "metadata": { 190 | "id": "8ca50bbd", 191 | "outputId": "d8865cf5-4221-487e-bcc4-614894b7772c" 192 | }, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "[1, 3, 3, 5, 5] ➞ [1, 3, 5]\n", 199 | "[4, 4, 4, 4] ➞ [4]\n", 200 | "[5, 7, 8, 9, 10, 15] ➞ [5, 7, 8, 9, 10, 15]\n", 201 | "[3, 3, 3, 2, 1] ➞ [1, 2, 3]\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "def setify(in_list):\n", 207 | " out_list = sorted(set(in_list))\n", 208 | " print(f'{in_list} ➞ {out_list}')\n", 209 | " \n", 210 | "setify([1, 3, 3, 5, 5]) \n", 211 | "setify([4, 4, 4, 4])\n", 212 | "setify([5, 7, 8, 9, 10, 15])\n", 213 | "setify([3, 3, 3, 2, 1])" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "id": "02d28ede", 219 | "metadata": { 220 | "id": "02d28ede" 221 | }, 222 | "source": [ 223 | "#### 5.Create a function that returns the mean of all digits.\n", 224 | "**Examples:** \n", 225 | "`mean(42) ➞ 3` \n", 226 | "`mean(12345) ➞ 3` \n", 227 | "`mean(666) ➞ 6` \n", 228 | "\n", 229 | "**Notes:** \n", 230 | " 1.The mean of all digits is the sum of digits / how many digits there are (e.g. mean of digits in 512 is (5+1+2)/3(number of digits) = 8/3=2). \n", 231 | " 2.The mean will always be an integer." 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "id": "30baa94c", 238 | "metadata": { 239 | "id": "30baa94c", 240 | "outputId": "b7d75f5a-dd62-4a67-ec1e-79e24ee5c336" 241 | }, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "Mean of 42 ➞ 3\n", 248 | "Mean of 12345 ➞ 3\n", 249 | "Mean of 666 ➞ 6\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "def mean(in_num):\n", 255 | " in_list = [int(ele) for ele in str(in_num)]\n", 256 | " out_num = sum(in_list)/len(str(in_num))\n", 257 | " print(f'Mean of {in_num} ➞ {out_num:.0f}')\n", 258 | " \n", 259 | "mean(42)\n", 260 | "mean(12345)\n", 261 | "mean(666)" 262 | ] 263 | } 264 | ], 265 | "metadata": { 266 | "kernelspec": { 267 | "display_name": "Python 3", 268 | "language": "python", 269 | "name": "python3" 270 | }, 271 | "language_info": { 272 | "codemirror_mode": { 273 | "name": "ipython", 274 | "version": 3 275 | }, 276 | "file_extension": ".py", 277 | "mimetype": "text/x-python", 278 | "name": "python", 279 | "nbconvert_exporter": "python", 280 | "pygments_lexer": "ipython3", 281 | "version": "3.8.8" 282 | }, 283 | "colab": { 284 | "name": "Basic_Programming Assignment_23.ipynb", 285 | "provenance": [] 286 | } 287 | }, 288 | "nbformat": 4, 289 | "nbformat_minor": 5 290 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_19.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "63646903", 6 | "metadata": { 7 | "id": "63646903" 8 | }, 9 | "source": [ 10 | "# Assignment 19 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "eht4tWQWEQe8" 20 | }, 21 | "id": "eht4tWQWEQe8" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "a80b431a", 26 | "metadata": { 27 | "id": "a80b431a" 28 | }, 29 | "source": [ 30 | "#### 1.Create a function that takes a string and returns a string in which each character is repeated once.\n", 31 | "**Examples:** \n", 32 | "`double_char(\"String\") ➞ \"SSttrriinngg\"` \n", 33 | "`double_char(\"Hello World!\") ➞ \"HHeelllloo WWoorrlldd!!\"` \n", 34 | "`doublechar(\"1234!_\") ➞ \"11223344!!__\"` " 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "id": "7c34f85c", 41 | "metadata": { 42 | "id": "7c34f85c", 43 | "outputId": "5c11d046-53f7-4b4f-f97f-31b8e9890e49" 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "➞ SSttrriinngg\n", 51 | "➞ HHeelllloo WWoorrlldd!!\n", 52 | "➞ 11223344!!__\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "def double_char(in_string):\n", 58 | " out_string = ''\n", 59 | " for ele in in_string:\n", 60 | " out_string += ele*2\n", 61 | " return out_string\n", 62 | "\n", 63 | "print(f'➞ {double_char(\"String\")}')\n", 64 | "print(f'➞ {double_char(\"Hello World!\")}')\n", 65 | "print(f'➞ {double_char(\"1234!_\")}')" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "id": "36c73da9", 71 | "metadata": { 72 | "id": "36c73da9" 73 | }, 74 | "source": [ 75 | "#### 2.Create a function that reverses a boolean value and returns the string \"boolean expected\" if another variable type is given.\n", 76 | "**Examples:** \n", 77 | "`reverse(True) ➞ False` \n", 78 | "`reverse(False) ➞ True` \n", 79 | "`reverse(0) ➞ \"boolean expected\"` \n", 80 | "`reverse(None) ➞ \"boolean expected\"` " 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "id": "9cb5a79b", 87 | "metadata": { 88 | "id": "9cb5a79b", 89 | "outputId": "453d851b-36a0-48b2-fa3e-cd766b3fd799" 90 | }, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "reverse(True) ➞ False\n", 97 | "reverse(False) ➞ True\n", 98 | "reverse(0) ➞ Boolean Expected\n", 99 | "reverse(None) ➞ Boolean Expected\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "def reverse(in_bool):\n", 105 | " if type(in_bool) == bool:\n", 106 | " return not in_bool\n", 107 | " else:\n", 108 | " return \"Boolean Expected\"\n", 109 | "\n", 110 | "print(f'reverse(True) ➞ {reverse(True)}')\n", 111 | "print(f'reverse(False) ➞ {reverse(False)}')\n", 112 | "print(f'reverse(0) ➞ {reverse(0)}')\n", 113 | "print(f'reverse(None) ➞ {reverse(None)}')" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "id": "e6161ea2", 119 | "metadata": { 120 | "id": "e6161ea2" 121 | }, 122 | "source": [ 123 | "#### 3. Create a function that returns the thickness (in meters) of a piece of paper after folding it n number of times. The paper starts off with a thickness of 0.5mm.\n", 124 | "**Examples:** \n", 125 | "`num_layers(1) ➞ \"0.001m\"\n", 126 | " # Paper folded once is 1mm (equal to 0.001m)\n", 127 | "num_layers(4) ➞ \"0.008m\"\n", 128 | " # Paper folded 4 times is 8mm (equal to 0.008m)\n", 129 | "num_layers(21) ➞ \"1048.576m\"\n", 130 | " # Paper folded 21 times is 1048576mm (equal to 1048.576m)`" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "id": "3abb23af", 137 | "metadata": { 138 | "id": "3abb23af", 139 | "outputId": "e7e15c70-05c9-45c2-a35f-d9f30f626881" 140 | }, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "Output ➞ 0.001m\n", 147 | "Output ➞ 0.008m\n", 148 | "Output ➞ 1048.576m\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "def num_layers(in_num):\n", 154 | " out_num = 0.5\n", 155 | " for ele in range(in_num):\n", 156 | " out_num *= 2\n", 157 | " print(f'Output ➞ {out_num/1000}m')\n", 158 | " \n", 159 | "num_layers(1)\n", 160 | "num_layers(4)\n", 161 | "num_layers(21)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "id": "c71bac5c", 167 | "metadata": { 168 | "id": "c71bac5c" 169 | }, 170 | "source": [ 171 | "#### 4.Create a function that takes a single string as argument and returns an ordered list containing the indices of all capital letters in the string.\n", 172 | "**Examples:** \n", 173 | "`index_of_caps(\"eDaBiT\") ➞ [1, 3, 5]` \n", 174 | "`index_of_caps(\"eQuINoX\") ➞ [1, 3, 4, 6]` \n", 175 | "`index_of_caps(\"determine\") ➞ []` \n", 176 | "`index_of_caps(\"STRIKE\") ➞ [0, 1, 2, 3, 4, 5]` \n", 177 | "`index_of_caps(\"sUn\") ➞ [1]` " 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "id": "e6ce83db", 184 | "metadata": { 185 | "id": "e6ce83db", 186 | "outputId": "74ffb942-4904-4c78-9a3a-e7e97a76ce01" 187 | }, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "eDaBiT ➞ [1, 3, 5]\n", 194 | "eQuINoX ➞ [1, 3, 4, 6]\n", 195 | "determine ➞ []\n", 196 | "STRIKE ➞ [0, 1, 2, 3, 4, 5]\n", 197 | "sUn ➞ [1]\n" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "def index_of_caps(in_string):\n", 203 | " out_string = []\n", 204 | " for ele in in_string:\n", 205 | " if ele.isupper():\n", 206 | " out_string.append(in_string.index(ele))\n", 207 | " print(f'{in_string} ➞ {out_string}')\n", 208 | "\n", 209 | "index_of_caps(\"eDaBiT\")\n", 210 | "index_of_caps(\"eQuINoX\")\n", 211 | "index_of_caps(\"determine\")\n", 212 | "index_of_caps(\"STRIKE\")\n", 213 | "index_of_caps(\"sUn\")" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "id": "11770ca9", 219 | "metadata": { 220 | "id": "11770ca9" 221 | }, 222 | "source": [ 223 | "#### 5.Using list comprehensions, create a function that finds all even numbers from 1 to the given number.\n", 224 | "**Examples:** \n", 225 | "`find_even_nums(8) ➞ [2, 4, 6, 8]` \n", 226 | "`find_even_nums(4) ➞ [2, 4]` \n", 227 | "`find_even_nums(2) ➞ [2]` " 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "id": "3609695d", 234 | "metadata": { 235 | "id": "3609695d", 236 | "outputId": "c297ac94-05b4-43d4-ae5a-44244ec5c568" 237 | }, 238 | "outputs": [ 239 | { 240 | "name": "stdout", 241 | "output_type": "stream", 242 | "text": [ 243 | "Output ➞ [2, 4, 6, 8]\n", 244 | "Output ➞ [2, 4]\n", 245 | "Output ➞ [2]\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "def find_even_nums(in_num):\n", 251 | " out_list = [i for i in range(1,in_num+1) if i%2 == 0]\n", 252 | " print(f'Output ➞ {out_list}')\n", 253 | " \n", 254 | "find_even_nums(8)\n", 255 | "find_even_nums(4)\n", 256 | "find_even_nums(2)" 257 | ] 258 | } 259 | ], 260 | "metadata": { 261 | "kernelspec": { 262 | "display_name": "Python 3", 263 | "language": "python", 264 | "name": "python3" 265 | }, 266 | "language_info": { 267 | "codemirror_mode": { 268 | "name": "ipython", 269 | "version": 3 270 | }, 271 | "file_extension": ".py", 272 | "mimetype": "text/x-python", 273 | "name": "python", 274 | "nbconvert_exporter": "python", 275 | "pygments_lexer": "ipython3", 276 | "version": "3.8.8" 277 | }, 278 | "colab": { 279 | "name": "Basic_Programming Assignment_19.ipynb", 280 | "provenance": [] 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 5 285 | } -------------------------------------------------------------------------------- /Python/Basic_Programming/Basic_Programming_Assignment_08.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d3a4bb27", 6 | "metadata": { 7 | "id": "d3a4bb27" 8 | }, 9 | "source": [ 10 | "# Assignment 8 Solutions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "SUBMITTED BY: GAURAV RATAN" 17 | ], 18 | "metadata": { 19 | "id": "sx4Gig6QUgsi" 20 | }, 21 | "id": "sx4Gig6QUgsi" 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "823845d3", 26 | "metadata": { 27 | "id": "823845d3" 28 | }, 29 | "source": [ 30 | "#### 1. Write a Python Program to Add two Matrices ?" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "id": "6dd2fd5c", 37 | "metadata": { 38 | "colab": { 39 | "base_uri": "https://localhost:8080/" 40 | }, 41 | "id": "6dd2fd5c", 42 | "outputId": "6579120c-3bba-47aa-d275-1925a65b3783" 43 | }, 44 | "outputs": [ 45 | { 46 | "output_type": "stream", 47 | "name": "stdout", 48 | "text": [ 49 | "Inputs: [[1, 2, 3], [4, 5, 6], [7, 8, 9]],[[10, 11, 12], [13, 14, 15], [16, 17, 18]]\n", 50 | "Output: [[11, 13, 15], [17, 19, 21], [23, 25, 27]]\n", 51 | "Inputs: [[2, 3, 5], [1, 1, 1], [2, 2, 2]],[[4, 3, 5], [1, 2, 3], [3, 2, 1]]\n", 52 | "Output: [[6, 6, 10], [2, 3, 4], [5, 4, 3]]\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "def addMatrices(a,b):\n", 58 | " print(f'Inputs: {a},{b}')\n", 59 | " if len(a) == len(b):\n", 60 | " out_matrix = []\n", 61 | " for ele in range(len(a)):\n", 62 | " if len(a[ele]) == len(b[ele]):\n", 63 | " out_matrix.append([])\n", 64 | " for sub_ele in range(len(a[ele])):\n", 65 | " out_matrix[ele].append(a[ele][sub_ele]+b[ele][sub_ele])\n", 66 | " else:\n", 67 | " print('Both Matrices must contains same no of rows and columns') \n", 68 | " else:\n", 69 | " print('Both Matrices must contains same no of rows and columns')\n", 70 | " print(f'Output: {out_matrix}')\n", 71 | "\n", 72 | "addMatrices([[1,2,3],[4,5,6],[7,8,9]],[[10,11,12],[13,14,15],[16,17,18]])\n", 73 | "addMatrices([[2,3,5],[1,1,1],[2,2,2]],[[4,3,5],[1,2,3],[3,2,1]])" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "id": "9e6c1983", 79 | "metadata": { 80 | "id": "9e6c1983" 81 | }, 82 | "source": [ 83 | "#### 2. Write a Python Program to Multiply two Matrices ?" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 3, 89 | "id": "e5c57cb3", 90 | "metadata": { 91 | "colab": { 92 | "base_uri": "https://localhost:8080/" 93 | }, 94 | "id": "e5c57cb3", 95 | "outputId": "d21f4554-014a-4d3a-fa50-948abdfad38d" 96 | }, 97 | "outputs": [ 98 | { 99 | "output_type": "stream", 100 | "name": "stdout", 101 | "text": [ 102 | "[[48, 60, 56], [132, 159, 146], [216, 258, 236]]\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "a = [[1,2,3],[4,5,6],[7,8,9]]\n", 108 | "b = [[11,12,13],[14,15,8],[3,6,9]]\n", 109 | "\n", 110 | "def multiply_matrice(a,b):\n", 111 | " output = []\n", 112 | " if len(a[0]) == len(b):\n", 113 | " for ele in range(len(a[0])):\n", 114 | " output.append([0 for ele in range(len(b[0]))])\n", 115 | " for i in range(len(a)):\n", 116 | " for j in range(len(b[0])):\n", 117 | " for k in range(len(b)):\n", 118 | " output[i][j] += a[i][k]*b[k][j]\n", 119 | " print(output) \n", 120 | " else:\n", 121 | " print('Matrix Multiplication is Not Possible')\n", 122 | " \n", 123 | "multiply_matrice(a,b)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "id": "c2705b7d", 129 | "metadata": { 130 | "id": "c2705b7d" 131 | }, 132 | "source": [ 133 | "#### 3. Write a Python Program to transpose a Matrix ?" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 4, 139 | "id": "08afb7ff", 140 | "metadata": { 141 | "colab": { 142 | "base_uri": "https://localhost:8080/" 143 | }, 144 | "id": "08afb7ff", 145 | "outputId": "605b1e01-883a-4ff3-dc9c-ed3cf4a405b9" 146 | }, 147 | "outputs": [ 148 | { 149 | "output_type": "stream", 150 | "name": "stdout", 151 | "text": [ 152 | "[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]\n", 153 | "[[1, 2], [4, 5], [7, 8]] -> [[1, 4, 7], [2, 5, 8]]\n", 154 | "[[1, 2, 3], [4, 5, 6]] -> [[1, 4], [2, 5], [3, 6]]\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "a = [[1,2,3],[4,5,6],[7,8,9]]\n", 160 | "b = [[1,2],[4,5],[7,8]]\n", 161 | "c = [[1,2,3],[4,5,6]]\n", 162 | "\n", 163 | "def generate_transpose(in_matrix):\n", 164 | " out_matrix = []\n", 165 | " for ele in range(len(in_matrix[0])):\n", 166 | " out_matrix.append([0 for i in range(len(in_matrix))])\n", 167 | " for i in range(len(in_matrix)):\n", 168 | " for j in range(len(in_matrix[i])):\n", 169 | " out_matrix[j][i] = in_matrix[i][j]\n", 170 | " print(f'{in_matrix} -> {out_matrix}')\n", 171 | " \n", 172 | "generate_transpose(a)\n", 173 | "generate_transpose(b)\n", 174 | "generate_transpose(c)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "id": "5b6207c1", 180 | "metadata": { 181 | "id": "5b6207c1" 182 | }, 183 | "source": [ 184 | "#### 4. Write a Python Program to sort Words in an Alphabatical Order ?" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 5, 190 | "id": "d6e5de3d", 191 | "metadata": { 192 | "colab": { 193 | "base_uri": "https://localhost:8080/" 194 | }, 195 | "id": "d6e5de3d", 196 | "outputId": "01a3f9f7-6bb4-45b1-ca29-2993b48c0c57" 197 | }, 198 | "outputs": [ 199 | { 200 | "output_type": "stream", 201 | "name": "stdout", 202 | "text": [ 203 | "Enter a String: Gaurav Ratan\n", 204 | "Gaurav Ratan\n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "def sortString():\n", 210 | " in_string = input(\"Enter a String: \").title()\n", 211 | " sorted_list = sorted(in_string.split(' '))\n", 212 | " print(' '.join(sorted_list))\n", 213 | "\n", 214 | "sortString()" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "id": "eb0e084d", 220 | "metadata": { 221 | "id": "eb0e084d" 222 | }, 223 | "source": [ 224 | "#### 5. Write a Python Program to remove Punctuations From a String ?" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 6, 230 | "id": "08c0196d", 231 | "metadata": { 232 | "colab": { 233 | "base_uri": "https://localhost:8080/" 234 | }, 235 | "id": "08c0196d", 236 | "outputId": "513e17e7-ce76-40ce-9995-c930e1197eb4" 237 | }, 238 | "outputs": [ 239 | { 240 | "output_type": "stream", 241 | "name": "stdout", 242 | "text": [ 243 | "Enter a String: Gaurav,Ratan\n", 244 | "GauravRatan\n" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "def removePunctuations():\n", 250 | " punctuations = '''!()-[]{};:'\"\\,<>./?@#$%^&*_~'''\n", 251 | " in_string = input('Enter a String: ')\n", 252 | " out_string = ''\n", 253 | " for ele in in_string:\n", 254 | " if ele not in punctuations:\n", 255 | " out_string += ele\n", 256 | " print(out_string)\n", 257 | " \n", 258 | "removePunctuatuions()" 259 | ] 260 | } 261 | ], 262 | "metadata": { 263 | "kernelspec": { 264 | "display_name": "Python 3 (ipykernel)", 265 | "language": "python", 266 | "name": "python3" 267 | }, 268 | "language_info": { 269 | "codemirror_mode": { 270 | "name": "ipython", 271 | "version": 3 272 | }, 273 | "file_extension": ".py", 274 | "mimetype": "text/x-python", 275 | "name": "python", 276 | "nbconvert_exporter": "python", 277 | "pygments_lexer": "ipython3", 278 | "version": "3.8.8" 279 | }, 280 | "colab": { 281 | "name": "Basic_Programming Assignment_08.ipynb", 282 | "provenance": [] 283 | } 284 | }, 285 | "nbformat": 4, 286 | "nbformat_minor": 5 287 | } --------------------------------------------------------------------------------