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

Department of Data Science

\n", "

Course: Tools and Techniques for Data Science

\n", "\n", "---\n", "

Instructor: Muhammad Arif Butt, Ph.D.

" ] }, { "cell_type": "markdown", "id": "4e6fa84e", "metadata": {}, "source": [ "

Lecture 2.14

" ] }, { "cell_type": "markdown", "id": "36476b93", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "3f009d4a", "metadata": {}, "source": [ "## _Python Built-in Modules.ipynb_\n", "#### [Check out the full list of Python Built-in modules](https://docs.python.org/3/py-modindex.html)" ] }, { "cell_type": "markdown", "id": "6fb1e84f", "metadata": {}, "source": [ "## Learning agenda of this notebook\n", "Python has tons of Built-in modules that can be read from above link. In this notebook file, we will be discussing a short but important subset of it:\n", "1. What are Python Built-in Modules\n", "2. Different ways to import a Module in Python\n", "3. The Math Module \n", "4. The Random Module\n", "5. The Time Module\n", "6. The DateTime Module\n", "7. The Calendar Module\n", "8. The OS Module\n", "9. The Threading Module\n", "10. The URLLIB Module" ] }, { "cell_type": "markdown", "id": "50ad5e55", "metadata": {}, "source": [ "## 1. What are Python Built-in Modules\n", "- In Python, Modules are simply files with the `. py` extension containing Python code (variables, functions, classes etc) that can be imported inside another Python Program. \n", "- You can think of Python module like a C library, which is linked with C program during the linking phase.\n", "- Some advantages of Modular programming are:\n", ">- **Modularity:** We use modules to break down large programs into small manageable and organized files. \n", ">- **Simplicity:** Rather than focusing the entire problem at hand, a module typically focuses on one relatively small portion of the problem.\n", ">- **Maintainability:** Modules are typically designed so that they enforce logical boundries between different problem domains.\n", ">- **Reusability:** Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to duplicate code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.\n", ">- **Scoping:** Modules typically define a separate namespace, which helps avoid collisions between identifiers in different areas of a program. The key benefit of using modules is _namespaces_: you must import the module to use its functions within a Python script or notebook. Namespaces provide encapsulation and avoid naming conflicts between your code and a module or across modules.\n", "\n", "\n", "**Note**: - A module is a single file of Python code that is meant to be imported, while a Python package is a simple directory having collections of Python modules under a common namespace. " ] }, { "cell_type": "markdown", "id": "7922ac39", "metadata": {}, "source": [ "## 2. Ways to Import a Python Module\n", "- Python math module contains rich set of functions, that allows you to perform mathematical tasks on numbers.\n", "- Since the math module comes packaged with the Python release, you don't have to install it separately. Using it is just a matter of importing the module" ] }, { "cell_type": "markdown", "id": "41737b29", "metadata": {}, "source": [ "### a. Option 1: `import math`\n", ">- We can use the **`import`** keyword to import a module, and later using the module name we can access its functions using the dot . operator, like `math.ceil()` " ] }, { "cell_type": "code", "execution_count": 1, "id": "d48f3f31", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'quit']\n" ] } ], "source": [ "# We have seen the use of dir() function. When called without argument it displays symbols of current module\n", "print(dir())" ] }, { "cell_type": "code", "execution_count": 2, "id": "22cd180f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'math', 'quit']\n" ] }, { "data": { "text/plain": [ "3628800" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "print(dir())\n", "math.ceil(2.3)\n", "math.factorial(10)" ] }, { "cell_type": "markdown", "id": "cdf3c072", "metadata": {}, "source": [ "### b. Option 2: `import math as m`\n", ">- We can also import a module by using a short alias, thus saving typing time in some cases. Note that in this case, the name `math` will not be recognized in our scope. Hence, `math.ceil()` is invalid and `m.ceil()` is the correct implementation." ] }, { "cell_type": "code", "execution_count": 3, "id": "31e728bc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '_2', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_ih', '_ii', '_iii', '_oh', 'exit', 'get_ipython', 'm', 'math', 'quit']\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math as m\n", "print(dir())\n", "m.ceil(2.3)" ] }, { "cell_type": "markdown", "id": "a6ace8fb", "metadata": {}, "source": [ "### c. Option 3:`from math import ceil` OR `from math import ceil, floor`\n", ">- We can use the **`from`** keyword to import specific name(s) from a module instead of importing the entire contents of a module. This way we don't have to use the dot operator and can access the function directly by its name" ] }, { "cell_type": "code", "execution_count": 4, "id": "b62713ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '_2', '_3', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_ih', '_ii', '_iii', '_oh', 'ceil', 'exit', 'get_ipython', 'm', 'math', 'quit']\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import ceil\n", "print(dir())\n", "ceil(2.3)" ] }, { "cell_type": "markdown", "id": "53ef1c74", "metadata": {}, "source": [ "### d. Option 4:`from mymath import *`\n", ">- We can import all the attributes from a module using asterik `*` construct. The difference between `import math` and `from math import *` is that in the later case you can don't have to use the dot operator and can directly use the functions, e.g., `ceil()`\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "50342f76", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '_2', '_3', '_4', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_i5', '_ih', '_ii', '_iii', '_oh', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exit', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'get_ipython', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'm', 'math', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'quit', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import *\n", "print(dir())\n", "ceil(2.3)" ] }, { "cell_type": "markdown", "id": "e7e56f88", "metadata": {}, "source": [ "## 3. The `math` Module\n", "- Python math module contains rich set of functions, that allows you to perform mathematical tasks on numbers.\n", "- Since the math module comes packaged with the Python release, you don't have to install it separately. Using it is just a matter of importing the module\n", "#### [Read Python Documentation for details about `math` module](https://docs.python.org/3/library/math.html#module-math)" ] }, { "cell_type": "markdown", "id": "08897acc", "metadata": {}, "source": [ "### a. Constants of Math Module" ] }, { "cell_type": "markdown", "id": "d28fdb10", "metadata": {}, "source": [ "- **PI:** \n", " - PI is the ratio of a circle's circumference (c) to its diameter (d).\n", " - It is an irrational number, so it can be approximated to the value 22/7 = 3.141592...\n", " - You can access its value since it is defined as a constant inside the math module with the name of 'pi', and is given correct upto 15 digits after the decimal point\n", " - Pi has been calculated to over 50 trillion digits beyond its decimal point. PI’s infinite nature makes it a fun challenge to memorize, and to computationally calculate more and more digits\n", " - Pi Day is celebrated on March 14th (3/14) around the world. " ] }, { "cell_type": "code", "execution_count": 6, "id": "471f1e31", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.pi" ] }, { "cell_type": "markdown", "id": "67d637ae", "metadata": {}, "source": [ "- **TAU:**\n", " - TAU is the ratio of a circule's circumference (c) to its radius (r).\n", " - This constant is equal to 2PI, or roughly 6.28\n", " - Like PI, TAU is also an irrational number, and can be approximated to the value 2PI = 6.28318..." ] }, { "cell_type": "code", "execution_count": 7, "id": "028c43a3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.283185307179586" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.tau" ] }, { "cell_type": "markdown", "id": "ada0eb14", "metadata": {}, "source": [ "- **Euler's Number:**\n", " - Euler's number (e) is a constant that is the base of natural logarithm.\n", " - It is a mathematical function that is commonly used to calculate rates of growth of decay.\n", " - As with PI and TAU, `e` is also an irrational number with approximated value of 2.718" ] }, { "cell_type": "code", "execution_count": 8, "id": "4444763e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.e" ] }, { "cell_type": "markdown", "id": "f580b099", "metadata": {}, "source": [ "- **Infinity:**\n", " - Infinity can't be defined by a number or a numeric value\n", " - It is a mathematical concept representing something that is never ending or boundless.\n", " - Infinity can go in either direction (positive as well as negative)\n", " - `math.inf` (added to Python 3.5) is a special data type equivalent to a float" ] }, { "cell_type": "code", "execution_count": 9, "id": "4ea12edb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.inf" ] }, { "cell_type": "code", "execution_count": 10, "id": "ba1e2b3d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(math.inf)" ] }, { "cell_type": "code", "execution_count": 11, "id": "5c66b5f7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Proof of concept: Positive infinity is greater than any highest known number\n", "math.inf > 99993999999999929999999999456748884839999883" ] }, { "cell_type": "code", "execution_count": 12, "id": "ae6a078d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Proof of concept: Negative infinity is smaller than any smallest known number\n", "-math.inf < -91876999999999999999999999954309873211234" ] }, { "cell_type": "code", "execution_count": 13, "id": "679b347a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Proof of concept: Whatever number is added/subtracted to positive infinity, the result is positive infinity\n", "math.inf + 324987699999999543" ] }, { "cell_type": "code", "execution_count": 14, "id": "f51caa6b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-inf" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Proof of concept: Whatever number is subtracted/added from negative infinity, the result is negative infinity\n", "-math.inf - 324999999999876543" ] }, { "cell_type": "markdown", "id": "18f35283", "metadata": {}, "source": [ "- **NaN (Not a Number):**\n", " - Not a Number is not a mathematical concept, rather is introduced in the field of computer science as a reference to values that are not numeric\n", " - `NaN` value can be due to invalid inputs, or it can indicate that a variable that should be numerical has been corrupted by text characters or symbols" ] }, { "cell_type": "code", "execution_count": 15, "id": "ec1c16a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.nan" ] }, { "cell_type": "code", "execution_count": 16, "id": "fe15017a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(math.nan)" ] }, { "cell_type": "markdown", "id": "4df04cb5", "metadata": {}, "source": [ "### b. Arithmetic Functions of Math Module" ] }, { "cell_type": "markdown", "id": "4d543f31", "metadata": {}, "source": [ "- Factorial of a number is obtained by multiplying that number and all numbers below it till one\n", "- Factorial is not defined for negative values as well as for decimal values. Factorial of zero is 1" ] }, { "cell_type": "code", "execution_count": 17, "id": "ad2e9bd3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30414093201713378043612608166064768844377641568960512000000000000" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fact_loop(num):\n", " if num < 0:\n", " return 0\n", " if num == 0:\n", " return 1\n", "\n", " factorial = 1\n", " for i in range(1, num + 1):\n", " factorial = factorial * i\n", " return factorial\n", "fact_loop(50)" ] }, { "cell_type": "code", "execution_count": 18, "id": "15432514", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30414093201713378043612608166064768844377641568960512000000000000" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fact_recursion(num):\n", " if num < 0:\n", " return 0\n", " if num == 0:\n", " return 1\n", "\n", " return num * fact_recursion(num - 1)\n", "fact_loop(50)" ] }, { "cell_type": "code", "execution_count": 19, "id": "95038bb8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30414093201713378043612608166064768844377641568960512000000000000" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.factorial(50)" ] }, { "cell_type": "markdown", "id": "9867d051", "metadata": {}, "source": [ "**Lets compare the execution time of calculating factorial using above three ways, using the `timeit()` method which returns the time taken to execute the statements a specified number of times**\n", "```\n", "timeit.timeit(stmt, setup, globals, number)\n", "```\n", "Where\n", "- `stmt`: Code statement(s) whose execution time is to be measured.(Use ; for multiple statements)\n", "- `setup`: Used to import some modules or declare some necessary variables. (Use ; for multiple statements)\n", "- `globals`: You can simplay pass `globals()` to the globals parameter, which will cause the code to be executed within your current global namespace\n", "- `number`: It specifies the number of times stmt will be executed. (Default is 1 million times)" ] }, { "cell_type": "code", "execution_count": 20, "id": "09b58dbb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7165604919999993" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import timeit\n", "timeit.timeit(\"fact_loop(10)\", globals=globals(), number = 1000000)" ] }, { "cell_type": "code", "execution_count": 21, "id": "b9763a00", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.374110029999997" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeit.timeit(\"fact_recursion(10)\", globals=globals(), number = 1000000)" ] }, { "cell_type": "code", "execution_count": 22, "id": "e004e08c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1095546269999943" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeit.timeit(\"math.factorial(10)\", setup = \"import math\", number = 1000000)" ] }, { "cell_type": "code", "execution_count": null, "id": "4f94dc77", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 23, "id": "36c8d064", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(21, -11)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.ceil(20.222), math.ceil(-11.85)" ] }, { "cell_type": "code", "execution_count": 24, "id": "c0ed10e4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(20, -14)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.floor(20.99), math.floor(-13.1)" ] }, { "cell_type": "code", "execution_count": 25, "id": "a2292213", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(20, -13)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.trunc(20.99), math.trunc(-13.1)" ] }, { "cell_type": "code", "execution_count": 26, "id": "3e9712f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# perm(n,k) = n!/(n-k)!\n", "import math\n", "math.perm(3,2)\n", "#All permutations made by with letters a, b, c by taking two at a time are six (ab, ba, ac, ca, bc, cb)" ] }, { "cell_type": "code", "execution_count": 1, "id": "f5175fbc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# comb(n,k) = n!/k!(n-k)!\n", "import math\n", "math.comb(3,2)\n", "#All combinations made by with letters a, b, c by taking two at a time are three (ab, ac, bc)" ] }, { "cell_type": "code", "execution_count": 28, "id": "f82bb454", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 50)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.gcd(39,27), math.gcd(100,50)" ] }, { "cell_type": "code", "execution_count": 1, "id": "ed0a9e8b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.lcm(20,30) # 60. Available on Python 3.9, I have currently Python3.8 :(" ] }, { "cell_type": "markdown", "id": "eeb467a3", "metadata": {}, "source": [ "### c. Power and Logarithmic Functions of Math Module" ] }, { "cell_type": "code", "execution_count": 30, "id": "755e5ecd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(32, 32.0, 32.0)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example: The power(a,b) function returns a**b. Available in the math module as well as Python built-in function\n", "# The pow() function in the math module is computationally faster\n", "import math\n", "a = 2\n", "b = 5\n", "a**b , pow(a,b), math.pow(a,b)" ] }, { "cell_type": "code", "execution_count": 31, "id": "0c49c68a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example: The sqrt(x) function returns a number y such that y² = x;\n", "import math\n", "math.sqrt(25)" ] }, { "cell_type": "code", "execution_count": 32, "id": "f3d224f8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(20.085536923187664, 20.085536923187668)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example: The exp(x) function returns e**x, where e is Euler's number (2.718281828459045)\n", "import math\n", "x = 3\n", "math.e ** x, math.exp(x)" ] }, { "cell_type": "code", "execution_count": 33, "id": "d7e30bd9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2.0794415416798357, 2.0794415416798357, 3.0, 0.9030899869919434)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example: The log(x, base) function return the logarithm of x to the mentioned base. Default base is e\n", "# Logarithm is the inverse function to exponentiation \n", "\n", "math.log(8), math.log(8, math.e), math.log(8, 2), math.log(8, 10)\n" ] }, { "cell_type": "markdown", "id": "53435ef8", "metadata": {}, "source": [ "### d. Trigonometric and Hyperbolic Functions of Math Module\n", "- The word trigonometry comes from the Greek words trigonon (“triangle”) and metron (“to measure”). \n", "- Trigonometry is the branch of mathematics dealing with the relations of the sides and angles of triangles and with the relevant functions of any angles. \n", "- Trigonometric functions are used in obtaining unknown angles and distances from known or measured angles in geometric figures.\n", "- Note: Hyperbolic functions are analogues of the ordinary trigonometric functions, but defined using the hyperbola rather than the circle. " ] }, { "cell_type": "code", "execution_count": 34, "id": "80c0753f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.0, 0.0015926529164868282)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Six functions of an angle: sin(), cos(), tan(), asin(), acos(), atan().\n", "# The angle given to these functions should be in radians. A circle has 360 degrees and 2pi radians\n", "import math\n", "math.sin(0), math.sin(3.14)" ] }, { "cell_type": "code", "execution_count": 35, "id": "603f86b5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11.53029203041011" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Examples of Hyperbolic Functions: sinh(), cosh(), tanh(), asinh(), acosh(), atanh()\n", "import math\n", "math.sinh(3.14)" ] }, { "cell_type": "markdown", "id": "43637d2d", "metadata": {}, "source": [ "## 4. The `random` Module\n", "- The Random module is used to perform random actions such as generating random numbers, print random value for a list or string, etc.\n", "#### [Read Python Documentation for details about `random` module](https://docs.python.org/3/library/random.html#module-random)" ] }, { "cell_type": "code", "execution_count": 36, "id": "70c6c490", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Existing functions in Random module: \n", "\n", " ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']\n" ] } ], "source": [ "#import random module\n", "import random\n", "\n", "# use dir() to get the list of complete functions in random module\n", "print(\"Existing functions in Random module: \\n\\n\", dir(random))" ] }, { "cell_type": "markdown", "id": "74f74cd2", "metadata": {}, "source": [ "### a. The `random.random()` Function\n", "- This function `random.random()` returns a random float value in the interval [0,1), i.e., 0 is inclusive while 1 is not" ] }, { "cell_type": "code", "execution_count": 37, "id": "bc1b4c79", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3315107182746464" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rv = random.random()\n", "rv" ] }, { "cell_type": "markdown", "id": "b96d3f21", "metadata": {}, "source": [ "### b. The `random.uniform()` Function\n", "- This function `random.uniform(a, b)` returns a random float value in the interval a and b " ] }, { "cell_type": "code", "execution_count": 38, "id": "3022eec8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "29.74603093979552" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rv = random.uniform(0, 100)\n", "rv" ] }, { "cell_type": "code", "execution_count": 39, "id": "90e21995", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint(1,100)" ] }, { "cell_type": "markdown", "id": "ee23fa99", "metadata": {}, "source": [ "### c. The `random.randint()` and `random.randrange()` Functions\n", "- We have seen the use of the built-in `range()` function that provides a range object, using which you can generate a list of numbers in that specific range\n", "- The `random.randint(start, stop)` returns one random integer (with start and stop both inclusive).\n", "- The `random.randrange(start, stop=None, step=1)` returns one random integer (with stop NOT inclusive). " ] }, { "cell_type": "code", "execution_count": 40, "id": "163d70f3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note the stop value is also inclusive, which is unlike what we expect in Python\n", "import random\n", "random.randint(0, 5)" ] }, { "cell_type": "code", "execution_count": 41, "id": "2433e606", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This fixes the problem and does not include the endpoint\n", "random.randrange(0, 5)" ] }, { "cell_type": "markdown", "id": "9c923557", "metadata": {}, "source": [ "### d. The `random.choice()` Function\n", "- This function is passed a non-empty sequence and returns a random element from that sequence\n", "```\n", "random.choice(seq)\n", "```" ] }, { "cell_type": "code", "execution_count": 42, "id": "033d79e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Random element from list: Arif\n", "Fetching Random item from string: y\n", "Fetching Random element from Tuple: 1\n" ] } ], "source": [ "import random\n", "\n", "# select a random element from a list\n", "list1 = ['Arif', 'Rauf', 'Mujahid']\n", "print(\"Random element from list: \", random.choice(list1))\n", " \n", "# select a random character from a string\n", "string = \"HappyLearning\"\n", "print(\"Fetching Random item from string: \", random.choice(string))\n", " \n", "# select a random item from a tuple\n", "tuple1 = (1, 2, 3, 4, 5)\n", "print(\"Fetching Random element from Tuple: \",random.choice(tuple1))" ] }, { "cell_type": "markdown", "id": "f3716f78", "metadata": {}, "source": [ "**With this background, one should be able explore other methods as and when required**" ] }, { "cell_type": "code", "execution_count": null, "id": "0fab8214", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c5a57ae0", "metadata": {}, "source": [ "## Bonus Info:\n", "- Before, we discuss Python's `time`, `datetime`, and `calendar` modules, let me put the stage right by having a brief discussion on the concept of time and time zones:\n", "\n", "### Calendar Time:\n", "- The time measured from some fixed/reference point is called real time and once category of it is calendar time. Some famous reference points and their corresponding calendars are:\n", " - **Hijri Calendar** (AH), measures time from the year of Hijrat, when prophet Muhammad (Peace be upon Him) migrated from Mecca to Madina\n", " - **Gregorian Calendar** (AD), measures time from birth year of Jesus Christ. AD stands for Anno Domini in Latin, means \"In the year of Jesus Christ\"\n", " - **UNIX Calendar**, measures time from birth year of UNIX called UNIX epoch (00:00:00 UTC on 1 January 1970)\n", "\n", "### Time Zones:\n", "- Since noon happens at different times in different parts of the world, therefore, we have divided the world in different time zones.\n", "- On Mac, Linux, and Windows operating systems, the information about these time zones is kept in files.\n", "- Let me show you the contents of these files on my Mac system" ] }, { "cell_type": "code", "execution_count": 43, "id": "2a0ab37c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+VERSION \u001b[34mCanada\u001b[m\u001b[m GB Iran NZ-CHAT UCT\r\n", "\u001b[34mAfrica\u001b[m\u001b[m \u001b[34mChile\u001b[m\u001b[m GB-Eire Israel Navajo \u001b[34mUS\u001b[m\u001b[m\r\n", "\u001b[34mAmerica\u001b[m\u001b[m Cuba GMT Jamaica PRC UTC\r\n", "\u001b[34mAntarctica\u001b[m\u001b[m EET GMT+0 Japan PST8PDT Universal\r\n", "\u001b[34mArctic\u001b[m\u001b[m EST GMT-0 Kwajalein \u001b[34mPacific\u001b[m\u001b[m W-SU\r\n", "\u001b[34mAsia\u001b[m\u001b[m EST5EDT GMT0 Libya Poland WET\r\n", "\u001b[34mAtlantic\u001b[m\u001b[m Egypt Greenwich MET Portugal Zulu\r\n", "\u001b[34mAustralia\u001b[m\u001b[m Eire HST MST ROC iso3166.tab\r\n", "\u001b[34mBrazil\u001b[m\u001b[m \u001b[34mEtc\u001b[m\u001b[m Hongkong MST7MDT ROK leapseconds\r\n", "CET \u001b[34mEurope\u001b[m\u001b[m Iceland \u001b[34mMexico\u001b[m\u001b[m Singapore posixrules\r\n", "CST6CDT Factory \u001b[34mIndian\u001b[m\u001b[m NZ Turkey zone.tab\r\n" ] } ], "source": [ "!ls /usr/share/zoneinfo/" ] }, { "cell_type": "code", "execution_count": 44, "id": "d0646e28", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Aden Chongqing Jerusalem Novokuznetsk Tashkent\r\n", "Almaty Chungking Kabul Novosibirsk Tbilisi\r\n", "Amman Colombo Kamchatka Omsk Tehran\r\n", "Anadyr Dacca Karachi Oral Tel_Aviv\r\n", "Aqtau Damascus Kashgar Phnom_Penh Thimbu\r\n", "Aqtobe Dhaka Kathmandu Pontianak Thimphu\r\n", "Ashgabat Dili Katmandu Pyongyang Tokyo\r\n", "Ashkhabad Dubai Khandyga Qatar Tomsk\r\n", "Atyrau Dushanbe Kolkata Qostanay Ujung_Pandang\r\n", "Baghdad Famagusta Krasnoyarsk Qyzylorda Ulaanbaatar\r\n", "Bahrain Gaza Kuala_Lumpur Rangoon Ulan_Bator\r\n", "Baku Harbin Kuching Riyadh Urumqi\r\n", "Bangkok Hebron Kuwait Saigon Ust-Nera\r\n", "Barnaul Ho_Chi_Minh Macao Sakhalin Vientiane\r\n", "Beirut Hong_Kong Macau Samarkand Vladivostok\r\n", "Bishkek Hovd Magadan Seoul Yakutsk\r\n", "Brunei Irkutsk Makassar Shanghai Yangon\r\n", "Calcutta Istanbul Manila Singapore Yekaterinburg\r\n", "Chita Jakarta Muscat Srednekolymsk Yerevan\r\n", "Choibalsan Jayapura Nicosia Taipei\r\n" ] } ], "source": [ "!ls /usr/share/zoneinfo/Asia" ] }, { "cell_type": "markdown", "id": "9bc0e988", "metadata": {}, "source": [ "**On all UNIX based systems (Mac, Linux), `TZ` is an environment variable that can be set to any of the above files to get the date of that appropriate zone. By default the system is configured to set it to the local time of the country**" ] }, { "cell_type": "code", "execution_count": 45, "id": "db47b9a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sun Dec 26 11:36:12 PKT 2021\r\n" ] } ], "source": [ "! date" ] }, { "cell_type": "code", "execution_count": 46, "id": "eee78fba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sun Dec 26 11:36:13 PKT 2021\r\n" ] } ], "source": [ "! TZ=Asia/Karachi date" ] }, { "cell_type": "code", "execution_count": 47, "id": "0679cd8c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sun Dec 26 12:06:13 IST 2021\r\n" ] } ], "source": [ "! TZ=Asia/Calcutta date" ] }, { "cell_type": "code", "execution_count": 48, "id": "fc2895c2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sun Dec 26 06:36:14 UTC 2021\r\n" ] } ], "source": [ "! TZ=America/Los-Angeles date" ] }, { "cell_type": "markdown", "id": "1a058d60", "metadata": {}, "source": [ "## 5. The `time` Module\n", "- Python Time module is principally for working with UNIX time stamps; expressed as a floating point number taken to be seconds since the unix epoch (00:00:00 UTC on 1 January 1970)\n", "#### [Read Python Documentation for details about `time` module](https://docs.python.org/3/library/time.html#module-time)" ] }, { "cell_type": "code", "execution_count": 49, "id": "64a13665", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Existing functions in time module: \n", "\n", " ['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'time_ns', 'timezone', 'tzname', 'tzset']\n" ] } ], "source": [ "import time\n", "\n", "# use dir() to get the list of complete functions in time module\n", "print(\"Existing functions in time module: \\n\\n\", dir(time))" ] }, { "cell_type": "markdown", "id": "31f713ec", "metadata": {}, "source": [ "**(i) The `time.sleep(seconds)` method is used to delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.**" ] }, { "cell_type": "code", "execution_count": 50, "id": "efe8425d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is printed immediately.\n", "This is printed after 4 seconds.\n" ] } ], "source": [ "import time\n", "print(\"This is printed immediately.\")\n", "time.sleep(4)\n", "print(\"This is printed after 4 seconds.\")" ] }, { "cell_type": "markdown", "id": "37f6391e", "metadata": {}, "source": [ "**(ii) The `time.time()` method returns the current time in seconds since UNIX Epoch (00:00:00 UTC on 1 January 1970).**" ] }, { "cell_type": "code", "execution_count": 51, "id": "d0fa8a98", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1640500598.971924" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "seconds = time.time()\n", "seconds" ] }, { "cell_type": "code", "execution_count": 52, "id": "2f1b9618", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sun Dec 26 11:36:39 PKT 2021\r\n" ] } ], "source": [ "#Get time using shell command\n", "!date" ] }, { "cell_type": "code", "execution_count": 53, "id": "12b148e9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1640500600\r\n" ] } ], "source": [ "!date +%s" ] }, { "cell_type": "markdown", "id": "8c1d1bb6", "metadata": {}, "source": [ "**(iii) The `time.ctime(seconds)` method takes seconds passed since epoch as argument and returns a string representing local time.**" ] }, { "cell_type": "code", "execution_count": 54, "id": "70d0bb12", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Thu Jan 1 05:00:00 1970'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "dtg1 = time.ctime(0)\n", "dtg1" ] }, { "cell_type": "code", "execution_count": 55, "id": "3e3bb56a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sun Dec 26 11:36:46 2021'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "seconds = time.time()\n", "dtg2 = time.ctime(seconds)\n", "dtg2" ] }, { "cell_type": "markdown", "id": "932503c0", "metadata": {}, "source": [ "**With this background, one should be able explore other methods as and when required**" ] }, { "cell_type": "code", "execution_count": null, "id": "707fe243", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9445c4b5", "metadata": {}, "source": [ "## 6. The `datetime` Module\n", "- The `datetime` module can support many of the same operations as `time` module, but provides a more object oriented set of types, and also has some limited support for time zones.\n", "#### [Read Python Documentation for details about `datetime` module](https://docs.python.org/3/library/datetime.html#module-datetime)" ] }, { "cell_type": "code", "execution_count": 56, "id": "8d44417f", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Existing functions in datetime module: \n", "\n", " ['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']\n" ] } ], "source": [ "# import datetime module\n", "import datetime\n", "\n", "# use dir() to get the list of complete functions in datetime module\n", "print(\"Existing functions in datetime module: \\n\\n\", dir(datetime))" ] }, { "cell_type": "code", "execution_count": 57, "id": "0e9a16a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#The smallest year number allowed in a date or datetime object. MINYEAR is 1.\n", "datetime.MINYEAR" ] }, { "cell_type": "code", "execution_count": 58, "id": "2783e15c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9999" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#The largest year number allowed in a date or datetime object. MAXYEAR is 9999.\n", "datetime.MAXYEAR" ] }, { "cell_type": "markdown", "id": "f4485572", "metadata": {}, "source": [ "**(i) The `datetime.datetime.today()` and `datetime.datetime.now()` methods return a datetime object as per the time zone of the system**" ] }, { "cell_type": "code", "execution_count": 59, "id": "a4b34e92", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 12, 26, 11, 36, 59, 584624)" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg = datetime.datetime.today()\n", "dtg" ] }, { "cell_type": "code", "execution_count": 60, "id": "79569129", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 12, 26, 11, 36, 59, 921566)" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg = datetime.datetime.now()\n", "dtg" ] }, { "cell_type": "markdown", "id": "0b16005d", "metadata": {}, "source": [ "**(ii) Let us explore some commonly used attributes related with the `datetime` object.**\n", "- `dtg.year:` returns the year\n", "- `dtg.month:` returns the month\n", "- `dtg.day:` returns the date\n", "- `dtg.hour:` returns the hour\n", "- `dtg.minute:` returns the minutes\n", "- `dtg.second:` returns the seconds\n", "- `dtg.microsecond:` returns the microseconds" ] }, { "cell_type": "code", "execution_count": 61, "id": "d96d7c8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2021" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg.year" ] }, { "cell_type": "code", "execution_count": 62, "id": "d4801e8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "26" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg.day" ] }, { "cell_type": "code", "execution_count": 63, "id": "71504a3f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg.hour" ] }, { "cell_type": "code", "execution_count": 64, "id": "a13162ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "921566" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg.microsecond" ] }, { "cell_type": "markdown", "id": "4889c135", "metadata": {}, "source": [ "**(iii) The `datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])` method is used to create a `datetime` object**" ] }, { "cell_type": "code", "execution_count": 65, "id": "d309a238", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2021-12-31 00:00:00\n", "\n" ] } ], "source": [ "import datetime\n", "dtg = datetime.datetime(2021,12,31)\n", "print(dtg)\n", "print(type(dtg))" ] }, { "cell_type": "code", "execution_count": 66, "id": "772d1470", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 12, 31, 4, 30, 54, 678)" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dtg = datetime.datetime(2021, 12, 31, 4, 30, 54, 678)\n", "dtg" ] }, { "cell_type": "markdown", "id": "cbf3ed6c", "metadata": {}, "source": [ "**(iv) The `datetime.time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) ` methods returns a `time` object.**" ] }, { "cell_type": "code", "execution_count": 67, "id": "f7922e9b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10:15:54.000247\n", "\n" ] } ], "source": [ "t1 = datetime.time(10, 15, 54, 247)\n", "print(t1)\n", "print(type(t1))" ] }, { "cell_type": "markdown", "id": "4d2112b0", "metadata": {}, "source": [ "**With this background, one should be able explore other methods as and when required**" ] }, { "cell_type": "markdown", "id": "39d5f5d6", "metadata": {}, "source": [ "## 7. The `calendar` Module\n", "- This module allows you to output calendars like the Unix `cal` program, and provides additional useful functions related to the calendar. By default, these calendars have Monday as the first day of the week, and Sunday as the last\n", "#### [Read Python Documentation for details about `calendar` module](https://docs.python.org/3/library/calendar.html#module-calendar)" ] }, { "cell_type": "code", "execution_count": 68, "id": "00c616ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Existing functions in calendar module: \n", "\n", " ['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError', 'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_colwidth', '_locale', '_localized_day', '_localized_month', '_monthlen', '_nextmonth', '_prevmonth', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name', 'different_locale', 'error', 'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays', 'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange', 'prcal', 'prmonth', 'prweek', 'repeat', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday', 'weekheader']\n" ] } ], "source": [ "import calendar\n", "\n", "# use dir() to get the list of complete functions in calendar module\n", "print(\"Existing functions in calendar module: \\n\\n\", dir(calendar))" ] }, { "cell_type": "code", "execution_count": 69, "id": "5205f543", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2021\n", "\n", " January February March\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7\n", " 4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14\n", "11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21\n", "18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28\n", "25 26 27 28 29 30 31 29 30 31\n", "\n", " April May June\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 4 1 2 1 2 3 4 5 6\n", " 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13\n", "12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20\n", "19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27\n", "26 27 28 29 30 24 25 26 27 28 29 30 28 29 30\n", " 31\n", "\n", " July August September\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 4 1 1 2 3 4 5\n", " 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12\n", "12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19\n", "19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26\n", "26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30\n", " 30 31\n", "\n", " October November December\n", "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", " 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5\n", " 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12\n", "11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19\n", "18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26\n", "25 26 27 28 29 30 31 29 30 27 28 29 30 31\n", "\n" ] } ], "source": [ "# calendar() method to print the calendar of whole year\n", "cy = calendar.calendar(2021) \n", "print(cy)" ] }, { "cell_type": "code", "execution_count": 70, "id": "04121b7c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " November 2021\n", "Mo Tu We Th Fr Sa Su\n", " 1 2 3 4 5 6 7\n", " 8 9 10 11 12 13 14\n", "15 16 17 18 19 20 21\n", "22 23 24 25 26 27 28\n", "29 30\n", "\n" ] } ], "source": [ "import calendar\n", "# month() method is used to print calendar of specific month\n", "\n", "#print calendar of November 2021\n", "c = calendar.month(2021,11) \n", "print(c)" ] }, { "cell_type": "code", "execution_count": 71, "id": "48d3e53f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2021 is leap year: False\n", "2020 was be leap year: True\n" ] } ], "source": [ "import calendar\n", "# can check wether the year is leap year or not\n", "print(\"2021 is leap year: \", calendar.isleap(2021))\n", "\n", "print(\"2020 was be leap year: \", calendar.isleap(2020))" ] }, { "cell_type": "markdown", "id": "d746cb9f", "metadata": {}, "source": [ "**With this background, one should be able explore other methods as and when required**" ] }, { "cell_type": "markdown", "id": "b07f1899", "metadata": {}, "source": [ "## 8. The `os` Module\n", "- This module provides a portable way of using operating system dependent functionality and provides dozens of functions for interacting with the operating system\n", "#### [Read Python Documentation for details about `os` module](https://docs.python.org/3/library/os.html#module-os)" ] }, { "cell_type": "code", "execution_count": 72, "id": "f02e0918", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Existing functions in OS module: \n", "\n", " ['CLD_CONTINUED', 'CLD_DUMPED', 'CLD_EXITED', 'CLD_TRAPPED', 'DirEntry', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_LOCK', 'F_OK', 'F_TEST', 'F_TLOCK', 'F_ULOCK', 'MutableMapping', 'NGROUPS_MAX', 'O_ACCMODE', 'O_APPEND', 'O_ASYNC', 'O_CLOEXEC', 'O_CREAT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_EXLOCK', 'O_NDELAY', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_SHLOCK', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'POSIX_SPAWN_CLOSE', 'POSIX_SPAWN_DUP2', 'POSIX_SPAWN_OPEN', 'PRIO_PGRP', 'PRIO_PROCESS', 'PRIO_USER', 'P_ALL', 'P_NOWAIT', 'P_NOWAITO', 'P_PGID', 'P_PID', 'P_WAIT', 'PathLike', 'RTLD_GLOBAL', 'RTLD_LAZY', 'RTLD_LOCAL', 'RTLD_NODELETE', 'RTLD_NOLOAD', 'RTLD_NOW', 'R_OK', 'SCHED_FIFO', 'SCHED_OTHER', 'SCHED_RR', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'ST_NOSUID', 'ST_RDONLY', 'TMP_MAX', 'WCONTINUED', 'WCOREDUMP', 'WEXITED', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WNOWAIT', 'WSTOPPED', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_spawnvef', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chflags', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'cpu_count', 'ctermid', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'environb', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'get_blocking', 'get_exec_path', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getegid', 'getenv', 'getenvb', 'geteuid', 'getgid', 'getgrouplist', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getpriority', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchflags', 'lchmod', 'lchown', 'linesep', 'link', 'listdir', 'lockf', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'posix_spawn', 'posix_spawnp', 'pread', 'putenv', 'pwrite', 'read', 'readlink', 'readv', 'register_at_fork', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sched_get_priority_max', 'sched_get_priority_min', 'sched_yield', 'sendfile', 'sep', 'set_blocking', 'set_inheritable', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setpriority', 'setregid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'st', 'stat', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sync', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'terminal_size', 'times', 'times_result', 'truncate', 'ttyname', 'umask', 'uname', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'write', 'writev']\n" ] } ], "source": [ "import os\n", "# to get the list of complete functions in OS module\n", "print(\"Existing functions in OS module: \\n\\n\", dir(os))\n" ] }, { "cell_type": "markdown", "id": "6352299e", "metadata": {}, "source": [ "### a. The `os.getcwd()` and `os.listdir()` Function\n", "- The `os.getcwd()` return a unicode string representing the current working directory.\n", "- The `os.listdir(path=None)` return a list containing the names of the files in the pwd in arbitrary order. Does not display '.' and '..' directories. An optional path can be specified\n" ] }, { "cell_type": "code", "execution_count": null, "id": "70c024c7", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "# getcwd() function is used to return the current working directory\n", "cwd = os.getcwd()\n", "print(\"Current working directory:\\n\", cwd )\n", "\n", "# lisdir() function is used to return the contents of current working directory\n", "mylist = os.listdir(os.getcwd())\n", "print(\"\\nContents of directory: \\n\", mylist )\n" ] }, { "cell_type": "markdown", "id": "7b6c18e2", "metadata": {}, "source": [ "### b. The `os.chdir()` Function\n", "- The `os.chdir(path)` function is used to change the current working directory to the specified path." ] }, { "cell_type": "code", "execution_count": null, "id": "967c1537", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "print(\"Get current working directory:\\n\", os.getcwd())\n", "\n", "os.chdir('/Users/arif/')\n", "#os.chdir('C:\\\\Users\\Arif\\Desktop')\n", "\n", "print(\"Get current working directory again:\\n\", os.getcwd())" ] }, { "cell_type": "markdown", "id": "881ddaca", "metadata": {}, "source": [ "### c. The `os.mkdir()` and `os.rmdir()`Function\n", "- The `os.mkdir(path)` funcion creates a new directory\n", "- The `os.rmdir(path)` funcion removes a directory\n" ] }, { "cell_type": "code", "execution_count": null, "id": "427a8ca3", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.chdir('/Users/arif/Documents/')\n", "\n", "list1 = os.listdir(os.getcwd())\n", "print(\"Contents of directory: \", list1)\n", "\n", "os.mkdir(\"ANewDir\")\n", "list2 = os.listdir(os.getcwd())\n", "print(\"Contents of directory: \", list2)\n", "\n", "os.rmdir(\"ANewDir\")\n", "list3 = os.listdir(os.getcwd())\n", "print(\"Contents of directory: \", list3)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5c2e9db6", "metadata": {}, "outputs": [], "source": [ "help(os.system)" ] }, { "cell_type": "markdown", "id": "591547b1", "metadata": {}, "source": [ "### d. The `os.system()` Function\n", "- The `os.system(command)` method is used to execute the command in a subshell" ] }, { "cell_type": "code", "execution_count": null, "id": "64fd97a4", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.system('ls -l /Users/')\n", "\n", "print(\"\\n\")\n", "os.system('echo \"This is getting more and more interesting\"')\n", "\n", "print(\"\\n\")\n", "os.system('date')" ] }, { "cell_type": "markdown", "id": "76b29ad9", "metadata": {}, "source": [ "**Students should explore other functions like `chmod()`, `chown()`, `fstat()`, `getpid()`, `getuid()`**" ] }, { "cell_type": "code", "execution_count": null, "id": "018d47d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3c729cea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "ff03acde", "metadata": {}, "source": [ "## 9. The `threading` Module\n", "- A thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an OS (Operating System).\n", "- In simple words, a thread is a sequence of such instructions within a program that can be executed independently of other code. For simplicity, you can assume that a thread is simply a subset of a process!\n", "- A thread contains all this information in a Thread Control Block (TCB):\n", " - Thread Identifier: Unique id (TID) is assigned to every new thread\n", " - Stack pointer: Points to thread’s stack in the process. Stack contains the local variables under thread’s scope.\n", " - Program counter: a register which stores the address of the instruction currently being executed by thread.\n", " - Thread state: can be running, ready, waiting, start or done.\n", " - Thread’s register set: registers assigned to thread for computations.\n", " - Parent process Pointer: A pointer to the Process control block (PCB) of the process that the thread lives on.\n", "#### [Read Python Documentation for details about `threading` module](https://docs.python.org/3/library/threading.html#module-threading)" ] }, { "cell_type": "code", "execution_count": 73, "id": "0d4f1b76", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "I am Thread1 and my PID is 28244\n", "I am Thread2 and my PID is 28244\n", "\n", "\n", "I am Thread1 and my PID is 28244\n", "I am Thread2 and my PID is 28244\n", "\n", "\n", "I am Thread2 and my PID is 28244\n", "\n", "I am Thread1 and my PID is 28244\n", "\n", "I am Thread2 and my PID is 28244\n", "\n", "I am Thread1 and my PID is 28244\n", "Done!\n" ] } ], "source": [ "# Python program to illustrate the concept of threading\n", "import threading\n", "import os\n", "import time\n", "\n", "def display1():\n", " for i in range(4):\n", " print(\"\\nI am Thread1 and my PID is {}\". format(os.getpid()))\n", " time.sleep(2)\n", "\n", "def display2():\n", " for i in range(4):\n", " print(\"\\nI am Thread2 and my PID is {}\". format(os.getpid()))\n", " time.sleep(2)\n", "\n", "# creating thread\n", "t1 = threading.Thread(target=display1)\n", "t2 = threading.Thread(target=display2)\n", "\n", "\n", "t1.start()\n", "t2.start()\n", "\n", "# wait until thread 1 is completely executed\n", "t1.join()\n", "# wait until thread 2 is completely executed\n", "t2.join()\n", "\n", "# both threads completely executed\n", "print(\"Done!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "aa32c200", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "87567943", "metadata": {}, "source": [ "## 10. The `urllib` Package\n", "- The `urllib` package in Python 3 is a collection of following Python modules used for working with Uniform Resource Locators:\n", " - `urllib.request` for opening and reading URLs, using variety of protocols\n", " - `urllib.error` containing the exceptions raised by urllib.request\n", " - `urllib.parse` for parsing URLs\n", " - `urllib.robotparser` for parsing robots.txt files\n", "\n", "#### [Read Python Documentation for details about `urllib` package](https://docs.python.org/3/library/urllib.html#module-urllib)" ] }, { "cell_type": "markdown", "id": "da51eeb9", "metadata": {}, "source": [ "## Create a GitHub Hist" ] }, { "cell_type": "code", "execution_count": null, "id": "c9c12e2a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "054ea853", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c040707d", "metadata": {}, "source": [ ">**The `urllib.request.urlopen()`, may return a URLError saying `SSL: CERTIFICATE_VERIFY_FAILED`. To handle this error set the `_create_default_https_context` attribute of `ssl` to `_create_unverified_context`**" ] }, { "cell_type": "code", "execution_count": 74, "id": "29a5f041", "metadata": {}, "outputs": [], "source": [ "import ssl\n", "ssl._create_default_https_context = ssl._create_unverified_context" ] }, { "cell_type": "code", "execution_count": null, "id": "6eea4c4f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "38075fc1", "metadata": {}, "source": [ "### b. The `urllib.request.urlretrieve()` Function\n", "- The `urllib.request.urlretrieve(url, filename=None)` method is used to retrieve a remote file into a temporary location on disk.\n", "- Let us download a public csv file from github gist" ] }, { "cell_type": "code", "execution_count": 75, "id": "87491b77", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('./downloads/family.csv', )" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import urllib\n", "\n", "#Get the raw data url from your github gist account\n", "myurl = 'https://gist.githubusercontent.com/arifpucit/bbcb0bba0b5c245585b375f273f17876/raw/6c64ac3e1d2ce9d91c5dfcc652387ad9c3fb6293/family.csv'\n", "\n", "\n", "urllib.request.urlretrieve(myurl, './downloads/family.csv')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6ae7b87f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 76, "id": "c2efa97e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.DS_Store', 'family.csv']" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "os.listdir('./downloads')" ] }, { "cell_type": "code", "execution_count": 77, "id": "bf48ce21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Names,Age,Addr\n", "Arif,50,Lahore\n", "Rauf,52,Islamabad\n", "Maaz,27,Peshawer\n", "Hadeed,22,Islamabad\n", "Mujahid,18,Karachi" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system('cat ./downloads/family.csv')" ] }, { "cell_type": "code", "execution_count": null, "id": "93266a78", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "aa105473", "metadata": {}, "source": [ "## Check your Concepts\n", "\n", "Try answering the following questions to test your understanding of the topics covered in this notebook:\n", "\n", "1. What are modules in Python?\n", "2. What is a Python library?\n", "3. What is the Python Standard Library?\n", "4. What are some popular Python libraries?\n", "5. Where can you learn about the modules and functions available in the Python standard library?\n", "6. How do you install a third-party library?\n", "7. What is a module namespace? How is it useful?\n", "8. What problems would you run into if Python modules did not provide namespaces?\n", "9. How do you import a module?\n", "10. How do you use a function from an imported module? Illustrate with an example.\n", "11. What are some popular Python libraries?\n", "12. What is the purpose of the `os` module in Python?\n", "13. How do you identify the current working directory in a Jupyter notebook?\n", "14. How do you retrieve the list of files within a directory using Python?\n", "15. How do you create a directory using Python?\n", "16. How do you check whether a file or directory exists on the filesystem? Hint: `os.path.exists`.\n", "17. Where can you find the full list of functions contained in the `os` module?\n", "18. Give examples of 5 useful functions from the `os` and `os.path` modules.\n", "19. What are some popular Python libraries?\n" ] }, { "cell_type": "code", "execution_count": null, "id": "117fab5e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }