{ "cells": [ { "cell_type": "markdown", "source": [ "# Importing external modules\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Modularization/tutorials/Modules.ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FModularization%2Ftutorials%2FModules.ipynb)\n", "\n", "You can use external modules o libraries using the keyword `import`. These libraries contain functions and variables that you can reuse, no need to write them from scratch. For instance, let us use the [math](https://docs.python.org/3/library/math.html) library that contains mathematical variables and functions.\n", "\n", "### Mathematical library math\n" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 1, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793\n", "2.718281828459045\n", "362880\n", "148.4131591025766\n", "24\n", "1.6094379124341003\n", "-1.0\n", "1.8622957433108482\n" ] } ], "source": [ "import math\n", "\n", "print(math.pi) # The mathematical constant π = 3.141592…\n", "\n", "print(math.e) # The mathematical constant e = 2.718281…\n", "\n", "print(math.factorial(9)) # math.factorial(x) returns the factorial mathematical of x (x!)\n", "\n", "print(math.exp(5)) # math.exp(x) returns e to the power of x\n", "\n", "print(math.perm(4,3)) # math.perm(n,k) Returns the number of ways to choose k items from n items without\n", " # repetion\n", "\n", "print(math.log(5)) # math.log(x) returns the natural logarithm of x\n", "\n", "print(math.cos(math.pi)) # math.cos(rad) Returns the cosine of rad (the angle is passed in radians)\n", " # Other trigonometric functions are sin, tan, atan, acos, asin...\n", "\n", "print(math.asinh(math.pi)) # math.asinh(rad) Returns the hyperbolic sin of rad (the angle is passed in radians)\n", " # Other hyperbolic functions are cosh, tanh, acosh, asinh,...\n" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "Note that we need to prepend the name of the library to the name of the variable or function that we want to use.\n", "\n", "## Random Library random\n", "The library [random](https://docs.python.org/3/library/random.html) can be used to generate pseudo-random numbers. The term pseudo-random number is preferred since at the end programs in this library use deterministic methods to calculate numbers that appear to be random.\n", "\n" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 2, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "2\n", "[1, 3, 2]\n", "0.9425762742917996\n" ] }, { "data": { "text/plain": "3.32206754941009" }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random\n", "\n", "print(random.randint(1, 6)) # randint(a,b) returns a random integer number a<=N<=b\n", "\n", "print(random.choice([1, 2, 3])) # choice(sequence) returns an element in sequence selected at random\n", "\n", "x = [1, 2, 3]\n", "random.shuffle(x)\n", "print(x) # shuffle(x) shuffles the elements of x in place\n", "\n", "print(random.uniform(0.0, 1.0)) # uniform(a,b) returns a floating number drawn from a uniform distribution in\n", " # the interval [a,b]\n", "\n", "random.gauss(1.0, 2.0) # gauss(mu=0.0, sigma=1.0) returns a floating number drawn from a normal\n", " # distributions with parameters mu and sigma\n", "\n" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Modules\n", "Complex libraries are organised into modules, so that we do not need import the entire library if we are only interested in some parts of it. The keyword ```from``` is used to import a module from a library.\n", "For instance the [datetime](https://docs.python.org/3/library/datetime.html) library includes a module called ```datetime``` which we can import and use as:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "from datetime import datetime\n", "print(datetime.now())" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "Note that the name of the library and the module is the same, but we have only imported the module datetime and therefore cannot use any other constant, function, or module included in the ```datetime``` library." ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Aliases\n", "We can define aliases to make our code easier to read. An alias is just an alternative name we give to the library or module name to shorten it and make it more contact. Aliases are defined with the keyworkd ```as```:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "from datetime import datetime as dt\n", "print(dt.now())" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Installing libraries\n", "The libraries and modules above are built-in the standard Python distribution and there is no need to install them. If you use Google Colabs, it packs some additional modules to make your work more efficient, but you may find that they are missing in other distributions and need to be installed. Libraries can be installed using the package installer [PiP](https://pypi.org/project/pip/). Fortunately, you can install libraries in the runtime environment of your Notebook with pip adding an exclamation mark before the pip command:\n" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "!pip install numpy" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "The line above will install [Numpy](https://numpy.org/) and all its related dependencies in your runtime if it is not already available (if you run it in colabs, you will be happy to know it is already preinstalled)" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }