{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNfYI0F51irrAR80F+1P5N9", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# **Performance Considerations**\n", "The math module is implemented in C and is highly optimized for performance. However, keep these points in mind:\n", "\n", "math functions work only with individual numbers, not sequences\n", "For array operations, consider using NumPy instead\n", "math.fsum() is more accurate but slower than sum() for floating-point addition\n", "Use math.isclose() for floating-point comparisons instead of ==" ], "metadata": { "id": "syBUFz-fgQ8V" } }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "QSJVmVLif6st", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5b23c844-ca10-4f22-baf7-e1016983a6b8" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pi Constant - Pi = 3.141592653589793\n", "pi Constant - Degrees of Pi = 180.0\n", "\n", "e Constant - e = 3.141592653589793\n", "e Constant - Degrees of e = 155.74607629780772\n", "\n", "tau Constant - tau = 6.283185307179586\n", "tau Constant - Degrees of tau = 360.0\n", "\n", "inf Constant - Positive Infinity = inf\n", "inf Constant - Negative Infinity = -inf\n", "\n", "NaN Constant - Not a Number = nan\n", "FABS - Absolute Value of z = 40.95\n", "FABS - Absolute Value of -124.897 = 124.897\n", "\n", "copysign of x, z = -10.98\n", "copysign of z, x = 40.95\n", "\n", "CEIL - Ceiling of x = 11\n", "CEIL - Ceiling of y = 31\n", "\n", "FLOOR - Floor of x = 10\n", "FLOOR - Floor of y = 30\n", "\n", "Factorial of 3 = 6\n", "Factorial of 5 = 120\n", "FMOD - Mod of 2 and 3 = 2.0\n", "FMOD - Mod of 225.55 and 5.5 = 2.5500000000000114\n", "\n", "FREXP - Mantissa and Exponent Value of 5 = (0.625, 3)\n", "FREXP - Mantissa and Exponent Value of -9 = (-0.5625, 4)\n", "\n", "FSUM - Sum of Tuple Items = 100.0\n", "FSUM - Sum of List Items = 71.0\n", "\n", "GCD of two 10 and 2 = 2\n", "GCD of two 100 and 15 = 5\n", "ROUND - Rounded Number 100.98763 = 100.99\n", "ROUND - Rounded Number 125.932832 = 125.933\n", "\n", "LDEXP - LDEXP (FREXP inverse) Number of 4, 5 = 128.0\n", "LDEXP - LDEXP (FREXP inverse) Number of -9, 2 = -36.0\n", "\n", "MODF - Modf (Divided 1 to 2) Number of 100 = (0.0, 100.0)\n", "MODF - Modf (Divided 1 to 2) Number of 120.98 = (0.980000000000004, 120.0)\n", "\n", "TRUNC - Truncated Number 100.98763 = 100\n", "ROUND - Truncated Number 125.932832 = -125\n", "\n", "Remainder of 29 and 5 = 0.0\n", "Remainder of 10 and 3 = 1.0\n", "exp of 5 = 148.4131591025766\n", "exp of -3 = 0.049787068367863944\n", "\n", "expm1 of 8 = 2979.9579870417283\n", "expm1 of -5 = -0.9932620530009145\n", "\n", "LOG - logarithmic of 5 = 1.6094379124341003\n", "LOG - logarithmic of 100 Base 2 = 6.643856189774725\n", "\n", "LOG2 - logarithmic of 120 Base 2 = 6.906890595608519\n", "\n", "LOG10 - logarithmic of 150 Base 10 = 7.22881869049588\n", "\n", "POW - 2 Power 3 = 8.0\n", "POW - 5 Power 4 = 625.0\n", "\n", "SQRT - Square Root of 25 = 5.0\n", "SQRT - Square Root of 19 = 4.358898943540674\n", "COS - Cosine of 10 = -0.8390715290764524\n", "COS - Cosine of -15 = -0.7596879128588213\n", "\n", "SIN - Sine of 3 = 0.1411200080598672\n", "SIN - Sine of -5 = 0.9589242746631385\n", "\n", "TAN - Tangent of 9 = -0.45231565944180985\n", "TAN - Tangent of -3 = 0.1425465430742778\n", "\n", "ACOS - Arc Cosine of 1 = 0.0\n", "ACOS - Arc Cosine of -0.78 = 2.4654621440291318\n", "\n", "ASIN - Arc Sine of 1 = 1.5707963267948966\n", "ASIN - Arc Sine of -2 = -0.43344532006988595\n", "\n", "ATAN - Arc Tangent of 0.72 = 0.6240230529767569\n", "ATAN - Arc Tangent of -2.71 = -1.2172930308235297\n", "\n", "ATAN2 - Tangent of 2, 5 = 0.3805063771123649\n", "\n", "HYPOT - Hypot Value of 2, 3 = 3.605551275463989\n", "COSH - Hyperbolic Cosine of 2 = 3.7621956910836314\n", "COSH - Hyperbolic Cosine of -1 = 1.5430806348152437\n", "\n", "SINH - Hyperbolic Sine of 3 = 10.017874927409903\n", "SINH - Hyperbolic Sine of -5 = -74.20321057778875\n", "\n", "TANH - Hyperbolic Tangent of 1 = 0.7615941559557649\n", "TANH - Hyperbolic Tangent of -3 = -0.9950547536867305\n", "\n", "ACOSH - Hyperbolic Arc Cosine of 10 = 2.993222846126381\n", "ACOSH - Hyperbolic Arc Cosine of 30.78 = 4.119748326708938\n", "\n", "ASINH - Hyperbolic Arc Sine of 15 = 3.4023066454805946\n", "ASINH - Hyperbolic Arc Sine of -25 = -3.9124227656412556\n", "\n", "ATANH - Hyperbolic Arc Tangent of 0.57 = 0.6475228448273728\n", "ATANH - Hyperbolic Arc Tangent of -0.71 = -0.8871838632580928\n", "DEGREES Function - Degrees Value of 6 = 286.4788975654116\n", "DEGREES Function - Degrees Value of 12 = 687.5493541569879\n", "\n", "RADIANS Function - Radians Value of 350 = 6.1086523819801535\n", "\n", "RADIANS Function - Radians Value of 680 = 11.868238913561441\n", "\n", "GAMMA Function - Gamma Value of 8 = 5040.0\n", "LGAMMA Function - LGamma Value of 9 = 10.604602902745249\n", "Select operation.\n", "1.Add\n", "2.Subtract\n", "3.Multiply\n", "4.Divide\n", "Enter choice(1/2/3/4): 4\n", "Enter first number: 22\n", "Enter second number: 3\n", "22.0 / 3.0 = 7.333333333333333\n", "Let's do next calculation? (yes/no): yes\n", "Enter choice(1/2/3/4): 4\n", "Enter first number: 3\n", "Enter second number: 4\n", "3.0 / 4.0 = 0.75\n", "Let's do next calculation? (yes/no): no\n" ] } ], "source": [ "import math as td\n", "\n", "print('pi Constant - Pi = ', td.pi)\n", "print('pi Constant - Degrees of Pi = ', td.degrees(td.pi))\n", "\n", "print('\\ne Constant - e = ', td.pi)\n", "print('e Constant - Degrees of e = ', td.degrees(td.e))\n", "\n", "print('\\ntau Constant - tau = ', td.tau)\n", "print('tau Constant - Degrees of tau = ', td.degrees(td.tau))\n", "\n", "print('\\ninf Constant - Positive Infinity = ', td.inf)\n", "print('inf Constant - Negative Infinity = ', -td.inf)\n", "\n", "print('\\nNaN Constant - Not a Number = ', td.nan)\n", "\n", "\n", "import math as mh\n", "\n", "x = 10.98\n", "y = 30.22\n", "z = -40.95\n", "\n", "print('FABS - Absolute Value of z = ', mh.fabs(z))\n", "print('FABS - Absolute Value of -124.897 = ', mh.fabs(-124.897))\n", "\n", "print('\\ncopysign of x, z = ', mh.copysign(x, z))\n", "print('copysign of z, x = ', mh.copysign(z, x))\n", "\n", "print('\\nCEIL - Ceiling of x = ', mh.ceil(x))\n", "print('CEIL - Ceiling of y = ', mh.ceil(y))\n", "\n", "print('\\nFLOOR - Floor of x = ', mh.floor(x))\n", "print('FLOOR - Floor of y = ', mh.floor(y))\n", "\n", "print('\\nFactorial of 3 = ', mh.factorial(3))\n", "print('Factorial of 5 = ', mh.factorial(5))\n", "\n", "import math as gm\n", "\n", "print('FMOD - Mod of 2 and 3 = ', gm.fmod(2, 3))\n", "print('FMOD - Mod of 225.55 and 5.5 = ', gm.fmod(222.55, 5.5))\n", "\n", "print('\\nFREXP - Mantissa and Exponent Value of 5 = ', gm.frexp(5))\n", "print('FREXP - Mantissa and Exponent Value of -9 = ', gm.frexp(-9))\n", "\n", "print('\\nFSUM - Sum of Tuple Items = ', gm.fsum((10, 20, 30, 40)))\n", "print('FSUM - Sum of List Items = ', gm.fsum([5, 22, 35, 9]))\n", "\n", "print('\\nGCD of two 10 and 2 = ', gm.gcd(10, 2))\n", "print('GCD of two 100 and 15 = ', gm.gcd(100, 15))\n", "\n", "\n", "import math as at\n", "\n", "print('ROUND - Rounded Number 100.98763 = ', round(100.9876, 2))\n", "print('ROUND - Rounded Number 125.932832 = ', round(125.932832, 3))\n", "\n", "print('\\nLDEXP - LDEXP (FREXP inverse) Number of 4, 5 = ', at.ldexp(4, 5))\n", "print('LDEXP - LDEXP (FREXP inverse) Number of -9, 2 = ', at.ldexp(-9, 2))\n", "\n", "print('\\nMODF - Modf (Divided 1 to 2) Number of 100 = ', at.modf(100))\n", "print('MODF - Modf (Divided 1 to 2) Number of 120.98 = ', at.modf(120.98))\n", "\n", "print('\\nTRUNC - Truncated Number 100.98763 = ', at.trunc(100.9876))\n", "print('ROUND - Truncated Number 125.932832 = ', at.trunc(-125.932832))\n", "\n", "print('\\nRemainder of 29 and 5 = ', at.remainder(20, 5))\n", "print('Remainder of 10 and 3 = ', at.remainder(10, 3))\n", "\n", "import math as th\n", "\n", "print('exp of 5 = ', th.exp(5))\n", "print('exp of -3 = ', th.exp(-3))\n", "\n", "print('\\nexpm1 of 8 = ', th.expm1(8))\n", "print('expm1 of -5 = ', th.expm1(-5))\n", "\n", "print('\\nLOG - logarithmic of 5 = ', th.log(5))\n", "print('LOG - logarithmic of 100 Base 2 = ', th.log(100, 2))\n", "\n", "print('\\nLOG2 - logarithmic of 120 Base 2 = ', th.log2(120))\n", "\n", "print('\\nLOG10 - logarithmic of 150 Base 10 = ', th.log2(150))\n", "\n", "print('\\nPOW - 2 Power 3 = ', th.pow(2, 3))\n", "print('POW - 5 Power 4 = ', th.pow(5, 4))\n", "\n", "print('\\nSQRT - Square Root of 25 = ', th.sqrt(25))\n", "print('SQRT - Square Root of 19 = ', th.sqrt(19))\n", "\n", "import math as mt\n", "\n", "print('COS - Cosine of 10 = ', mt.cos(10))\n", "print('COS - Cosine of -15 = ', mt.cos(-15))\n", "\n", "print('\\nSIN - Sine of 3 = ', mt.sin(3))\n", "print('SIN - Sine of -5 = ', mt.sin(-5))\n", "\n", "print('\\nTAN - Tangent of 9 = ', mt.tan(9))\n", "print('TAN - Tangent of -3 = ', mt.tan(-3))\n", "\n", "print('\\nACOS - Arc Cosine of 1 = ', mt.acos(1))\n", "print('ACOS - Arc Cosine of -0.78 = ', mt.acos(-0.78))\n", "\n", "print('\\nASIN - Arc Sine of 1 = ', mt.asin(1))\n", "print('ASIN - Arc Sine of -2 = ', mt.asin(-0.42))\n", "\n", "print('\\nATAN - Arc Tangent of 0.72 = ', mt.atan(0.72))\n", "print('ATAN - Arc Tangent of -2.71 = ', mt.atan(-2.71))\n", "\n", "print('\\nATAN2 - Tangent of 2, 5 = ', mt.atan2(2, 5))\n", "\n", "print('\\nHYPOT - Hypot Value of 2, 3 = ', mt.hypot(2, 3))\n", "\n", "\n", "import math as ma\n", "\n", "print('COSH - Hyperbolic Cosine of 2 = ', ma.cosh(2))\n", "print('COSH - Hyperbolic Cosine of -1 = ', ma.cosh(-1))\n", "\n", "print('\\nSINH - Hyperbolic Sine of 3 = ', ma.sinh(3))\n", "print('SINH - Hyperbolic Sine of -5 = ', ma.sinh(-5))\n", "\n", "print('\\nTANH - Hyperbolic Tangent of 1 = ', ma.tanh(1))\n", "print('TANH - Hyperbolic Tangent of -3 = ', ma.tanh(-3))\n", "\n", "print('\\nACOSH - Hyperbolic Arc Cosine of 10 = ', ma.acosh(10))\n", "print('ACOSH - Hyperbolic Arc Cosine of 30.78 = ', ma.acosh(30.78))\n", "\n", "print('\\nASINH - Hyperbolic Arc Sine of 15 = ', ma.asinh(15))\n", "print('ASINH - Hyperbolic Arc Sine of -25 = ', ma.asinh(-25))\n", "\n", "print('\\nATANH - Hyperbolic Arc Tangent of 0.57 = ', ma.atanh(0.57))\n", "print('ATANH - Hyperbolic Arc Tangent of -0.71 = ', ma.atanh(-0.71))\n", "\n", "import math as gd\n", "\n", "print('DEGREES Function - Degrees Value of 6 = ', gd.degrees(5))\n", "print('DEGREES Function - Degrees Value of 12 = ', gd.degrees(12))\n", "\n", "print('\\nRADIANS Function - Radians Value of 350 = ', gd.radians(350))\n", "print('\\nRADIANS Function - Radians Value of 680 = ', gd.radians(680))\n", "\n", "print('\\nGAMMA Function - Gamma Value of 8 = ', gd.gamma(8))\n", "print('LGAMMA Function - LGamma Value of 9 = ', gd.lgamma(9))\n", "\n", "# This function adds two numbers\n", "def add(x, y):\n", " return x + y\n", "\n", "# This function subtracts two numbers\n", "def subtract(x, y):\n", " return x - y\n", "\n", "# This function multiplies two numbers\n", "def multiply(x, y):\n", " return x * y\n", "\n", "# This function divides two numbers\n", "def divide(x, y):\n", " return x / y\n", "\n", "\n", "print(\"Select operation.\")\n", "print(\"1.Add\")\n", "print(\"2.Subtract\")\n", "print(\"3.Multiply\")\n", "print(\"4.Divide\")\n", "\n", "while True:\n", " # take input from the user\n", " choice = input(\"Enter choice(1/2/3/4): \")\n", "\n", " # check if choice is one of the four options\n", " if choice in ('1', '2', '3', '4'):\n", " try:\n", " num1 = float(input(\"Enter first number: \"))\n", " num2 = float(input(\"Enter second number: \"))\n", " except ValueError:\n", " print(\"Invalid input. Please enter a number.\")\n", " continue\n", "\n", " if choice == '1':\n", " print(num1, \"+\", num2, \"=\", add(num1, num2))\n", "\n", " elif choice == '2':\n", " print(num1, \"-\", num2, \"=\", subtract(num1, num2))\n", "\n", " elif choice == '3':\n", " print(num1, \"*\", num2, \"=\", multiply(num1, num2))\n", "\n", " elif choice == '4':\n", " print(num1, \"/\", num2, \"=\", divide(num1, num2))\n", "\n", " # check if user wants another calculation\n", " # break the while loop if answer is no\n", " next_calculation = input(\"Let's do next calculation? (yes/no): \")\n", " if next_calculation == \"no\":\n", " break\n", " else:\n", " print(\"Invalid Input\")\n", "\n", "\n" ] }, { "cell_type": "markdown", "source": [ "# **Key takeaways for using the math module effectively:**\n", "\n", "Import only the functions you need for better code readability\n", "Be mindful of angle units (radians vs degrees) in trigonometric functions\n", "Use appropriate functions for floating-point comparisons and summation\n", "Handle edge cases and domain errors appropriately\n", "Consider NumPy for array-based mathematical operations\n", "Leverage the module’s accuracy and performance optimizations" ], "metadata": { "id": "T5NvCUaPgRpl" } }, { "cell_type": "code", "source": [ "import time\n", "\n", "big_list = list(range(1000000))\n", "big_set = set(big_list)\n", "start = time.time()\n", "print(999999 in big_list)\n", "print(f\"List lookup: {time.time() - start:.6f}s\")\n", "\n", "start = time.time()\n", "print(999999 in big_set)\n", "print(f\"Set lookup: {time.time() - start:.6f}s\")\n", "\n", "\n", "numbers = list(range(1000000))\n", "def modify_list(lst):\n", " lst[0] = 999\n", " return lst\n", "start = time.time()\n", "result = modify_list(numbers)\n", "print(f\"In-place: {time.time() - start:.4f}s\")\n", "\n", "def copy_list(lst):\n", " new_lst = lst.copy()\n", " new_lst[0] = 999\n", " return new_lst\n", "start = time.time()\n", "result = copy_list(numbers)\n", "print(f\"Copy: {time.time() - start:.4f}s\")\n", "\n", "class Point:\n", " __slots__ = ('x', 'y')\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", "start = time.time()\n", "points = [Point(i, i+1) for i in range(1000000)]\n", "print(f\"With slots: {time.time() - start:.4f}s\")\n", "\n", "import math\n", "numbers = list(range(10000000))\n", "start = time.time()\n", "roots = [math.sqrt(n) for n in numbers]\n", "print(f\"Math sqrt: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "roots = [n ** 0.5 for n in numbers]\n", "print(f\"Operator: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "result = [0] * 1000000\n", "for i in range(1000000):\n", " result[i] = i\n", "print(f\"Pre-allocated: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "result = []\n", "for i in range(1000000):\n", " result.append(i)\n", "print(f\"Dynamic: {time.time() - start:.4f}s\")\n", "\n", "numbers = list(range(10000000))\n", "start = time.time()\n", "total = 0\n", "for i in numbers:\n", " if i % 2 != 0:\n", " total += i // 2\n", " else:\n", " total += i\n", "print(f\"Conditional: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "total = 0\n", "for i in numbers:\n", " try:\n", " total += i / (i % 2)\n", " except ZeroDivisionError:\n", " total += i\n", "print(f\"Exception: {time.time() - start:.4f}s\")\n", "\n", "def outer():\n", " def add_pair(a, b):\n", " return a + b\n", " result = 0\n", " for i in range(10000000):\n", " result = add_pair(result, i)\n", " return result\n", "start = time.time()\n", "result = outer()\n", "print(f\"Local function: {time.time() - start:.4f}s\")\n", "\n", "def add_pair(a, b):\n", " return a + b\n", "start = time.time()\n", "result = 0\n", "for i in range(10000000):\n", " result = add_pair(result, i)\n", "print(f\"Global function: {time.time() - start:.4f}s\")\n", "\n", "from itertools import product\n", "items = [1, 2, 3] * 10\n", "start = time.time()\n", "result = list(product(items, repeat=2))\n", "print(f\"Itertools: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "result = []\n", "for x in items:\n", " for y in items:\n", " result.append((x, y))\n", "print(f\"Loops: {time.time() - start:.4f}s\")\n", "\n", "import bisect\n", "numbers = sorted(list(range(0, 1000000, 2)))\n", "start = time.time()\n", "bisect.insort(numbers, 75432)\n", "print(f\"Bisect: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "for i, num in enumerate(numbers):\n", " if num > 75432:\n", " numbers.insert(i, 75432)\n", " break\n", "print(f\"Loop: {time.time() - start:.4f}s\")\n", "\n", "def expensive_operation():\n", " time.sleep(0.001)\n", " return 42\n", "start = time.time()\n", "cached_value = expensive_operation()\n", "result = 0\n", "for i in range(1000):\n", " result += cached_value\n", "print(f\"Cached: {time.time() - start:.4f}s\")\n", "\n", "start = time.time()\n", "result = 0\n", "for i in range(1000):\n", " result += expensive_operation()\n", "print(f\"Repeated: {time.time() - start:.4f}s\")\n", "\n", "from statistics import median\n", "from math import isnan\n", "from itertools import filterfalse\n", "\n", "data = [20.7, float('NaN'),19.2, 18.3, float('NaN'), 14.4]\n", "sorted(data) # This has surprising behavior\n", "\n", "median(data) # This result is unexpected\n", "\n", "\n", "sum(map(isnan, data)) # Number of missing values\n", "\n", "clean = list(filterfalse(isnan, data)) # Strip NaN values\n", "clean\n", "\n", "sorted(clean) # Sorting now works as expected\n", "\n", "median(clean) # This result is now well defined\n", "\n", "from statistics import mean\n", "mean([1, 2, 3, 4, 4])\n", "\n", "mean([-1.0, 2.5, 3.25, 5.75])\n", "\n", "\n", "from fractions import Fraction as F\n", "mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])\n", "\n", "\n", "from decimal import Decimal as D\n", "mean([D(\"0.5\"), D(\"0.75\"), D(\"0.625\"), D(\"0.375\")])\n", "'''\n", "data = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]\n", "rand = kde_random(data, h=1.5, seed=8675309)\n", "new_selections = [rand() for i in range(10)]\n", "[round(x, 1) for x in new_selections]\n", "\n", "\n", "import statistics\n", "from decimal import Decimal as D\n", "pvariance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n", "\n", "\n", "from fractions import Fraction as F\n", "pvariance([F(1, 4), F(5, 4), F(1, 2)])\n", "\n", "from decimal import Decimal as D\n", "variance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n", "\n", "\n", "from fractions import Fraction as F\n", "variance([F(1, 6), F(1, 2), F(5, 3)])\n", "\n", "\n", "data = [105, 129, 87, 86, 111, 111, 89, 81, 108, 92, 110,\n", " 100, 75, 105, 103, 109, 76, 119, 99, 91, 103, 129,\n", " 106, 101, 84, 111, 74, 87, 86, 103, 103, 106, 86,\n", " 111, 75, 87, 102, 121, 111, 88, 89, 101, 106, 95,\n", " 103, 107, 101, 81, 109, 104]\n", "[round(q, 1) for q in quantiles(data, n=10)]\n", "\n", "x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "y = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n", "covariance(x, y)\n", "\n", "z = [9, 8, 7, 6, 5, 4, 3, 2, 1]\n", "covariance(x, z)\n", "\n", "covariance(z, x)\n", "'''" ], "metadata": { "id": "oC3RVxjigRzE", "colab": { "base_uri": "https://localhost:8080/", "height": 487 }, "outputId": "ca054be3-0c07-433e-82f6-8b2493b484fe" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "True\n", "List lookup: 0.011541s\n", "True\n", "Set lookup: 0.000092s\n", "In-place: 0.0001s\n", "Copy: 0.0048s\n", "With slots: 1.8380s\n", "Math sqrt: 1.9008s\n", "Operator: 1.2152s\n", "Pre-allocated: 0.1133s\n", "Dynamic: 0.1003s\n", "Conditional: 1.6772s\n", "Exception: 3.0192s\n", "Local function: 1.0596s\n", "Global function: 1.7677s\n", "Itertools: 0.1660s\n", "Loops: 0.0002s\n", "Bisect: 0.0009s\n", "Loop: 0.0089s\n", "Cached: 0.0018s\n", "Repeated: 1.0945s\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "'\\ndata = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]\\nrand = kde_random(data, h=1.5, seed=8675309)\\nnew_selections = [rand() for i in range(10)]\\n[round(x, 1) for x in new_selections]\\n\\n\\nimport statistics\\nfrom decimal import Decimal as D\\npvariance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\\n\\n\\nfrom fractions import Fraction as F\\npvariance([F(1, 4), F(5, 4), F(1, 2)])\\n\\nfrom decimal import Decimal as D\\nvariance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\\n\\n\\nfrom fractions import Fraction as F\\nvariance([F(1, 6), F(1, 2), F(5, 3)])\\n\\n\\ndata = [105, 129, 87, 86, 111, 111, 89, 81, 108, 92, 110,\\n 100, 75, 105, 103, 109, 76, 119, 99, 91, 103, 129,\\n 106, 101, 84, 111, 74, 87, 86, 103, 103, 106, 86,\\n 111, 75, 87, 102, 121, 111, 88, 89, 101, 106, 95,\\n 103, 107, 101, 81, 109, 104]\\n[round(q, 1) for q in quantiles(data, n=10)]\\n\\nx = [1, 2, 3, 4, 5, 6, 7, 8, 9]\\ny = [1, 2, 3, 1, 2, 3, 1, 2, 3]\\ncovariance(x, y)\\n\\nz = [9, 8, 7, 6, 5, 4, 3, 2, 1]\\ncovariance(x, z)\\n\\ncovariance(z, x)\\n'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 3 } ] }, { "cell_type": "markdown", "source": [ "# **What is the math module?**\n", "It is the C standard defined math module. The math module provides access to mathematical functions.\n", "\n", "I know that I have used some jargon in the above statement. Let me clear the clutter. The important terms in the above statement are module, C standard, mathematical functions.\n", "\n", "Module: A file containing a set of statements and definitions is called a module. For example, the file program.py is called a module and the module name would be program. You will know how to use a module in the later tutorial." ], "metadata": { "id": "inkM95iQgR6t" } }, { "cell_type": "code", "source": [ "import math\n", "\n", "a = math.sin(math.pi / 2)\n", "print(a)\n", "\n", "b = math.factorial(5)\n", "print(b)\n", "\n", "c = math.floor(12.6567)\n", "print(c)\n", "\n", "d = math.ceil(12.4567)\n", "print(d)\n", "\n", "e = math.exp(5)\n", "print(e)\n", "\n", "f = math.log(5)\n", "print(f)\n", "\n", "g = math.log10(5)\n", "print(g)\n", "\n", "h = math.pow(2,6)\n", "print(h)\n", "\n", "i = math.sqrt(625)\n", "print(i)\n", "\n", "#j = math.abs(-12)\n", "#print(j)\n", "\n", "\n", "import math\n", "\n", "p = 15\n", "q = 20\n", "\n", "gcd = math.gcd(p, q)\n", "print(gcd)\n", "\n", "import math\n", "\n", "n = 5\n", "r = 2\n", "\n", "num_permutations = math.perm(n, r)\n", "print(num_permutations)\n", "\n", "import math\n", "\n", "print(math.factorial(5))\n", "\n", "import math\n", "\n", "print(math.sqrt(25)) ## Output: 5.0\n", "print(math.sqrt(9)) ## Output: 3.0\n", "\n", "import math\n", "\n", "print(math.pow(2, 3)) ## Output: 8.0\n", "print(math.pow(5, 2)) ## Output: 25.0\n", "\n", "import math\n", "\n", "#print(math.abs(-10)) ## Output: 10\n", "#print(math.abs(7.5)) ## Output: 7.5\n", "\n", "import math\n", "\n", "print(math.sin(math.pi/2)) ## Output: 1.0\n", "print(math.cos(math.pi)) ## Output: -1.0\n", "print(math.tan(math.pi/4)) ## Output: 0.9999999999999999\n", "\n", "import math\n", "\n", "print(math.asin(1)) ## Output: 1.5707963267948966\n", "print(math.acos(0)) ## Output: 1.5707963267948966\n", "print(math.atan(1)) ## Output: 0.7853981633974483\n", "\n", "import math\n", "\n", "print(math.log(math.e)) ## Output: 1.0\n", "print(math.log10(100)) ## Output: 2.0\n", "\n", "import math\n", "\n", "print(math.ceil(3.2)) ## Output: 4\n", "print(math.floor(3.2)) ## Output: 3\n", "print(math.trunc(3.2)) ## Output: 3\n", "\n", "import math\n", "\n", "print(math.pi) ## Output: 3.141592653589793\n", "print(math.e) ## Output: 2.718281828459045\n", "import math\n", "\n", "radius = 5\n", "area = math.pi * math.pow(radius, 2)\n", "print(f\"The area of the circle with radius {radius} is {area:.2f} square units.\")\n", "\n", "import math\n", "\n", "angle_degrees = 45\n", "angle_radians = math.radians(angle_degrees)\n", "\n", "sine = math.sin(angle_radians)\n", "cosine = math.cos(angle_radians)\n", "\n", "print(f\"The sine of {angle_degrees} degrees is {sine:.2f}\")\n", "print(f\"The cosine of {angle_degrees} degrees is {cosine:.2f}\")\n", "\n", "import math\n", "\n", "number = 100\n", "natural_log = math.log(number)\n", "base10_log = math.log10(number)\n", "\n", "print(f\"The natural logarithm of {number} is {natural_log:.2f}\")\n", "print(f\"The base-10 logarithm of {number} is {base10_log:.2f}\")\n", "\n", "import math\n", "\n", "number = 3.7\n", "\n", "ceil_result = math.ceil(number)\n", "floor_result = math.floor(number)\n", "trunc_result = math.trunc(number)\n", "\n", "print(f\"Ceiling of {number} is {ceil_result}\")\n", "print(f\"Floor of {number} is {floor_result}\")\n", "print(f\"Truncated value of {number} is {trunc_result}\")\n", "\n", "import math\n", "\n", "n=int(input('Enter an integer number '))\n", "x=math.fabs(n)\n", "print('Absolute value of %d is %d' %(n,x))\n", "\n", "import math\n", "\n", "n=int(input('Enter an integer number '))\n", "x=math.sqrt(n)\n", "print('Square root of %d is %d' %(n,x))\n", "\n", "import math\n", "\n", "n=float(input('Enter a floating point (decimal) number '))\n", "x=math.ceil(n)\n", "print('Ceil value of %f is %f' %(n,x))\n", "\n", "import math\n", "\n", "n=float(input('Enter a floating point (decimal) number '))\n", "x=math.floor(n)\n", "print('Floor value of %f is %f' %(n,x))\n", "\n", "import math\n", "\n", "print('Enter two floating point (decimal) numbers')\n", "x=float(input())\n", "y=float(input())\n", "z=math.fmod(x,y)\n", "print('Remainder = %f' %(z))\n", "\n", "import math\n", "\n", "print('Enter two integer numbers')\n", "a=int(input())\n", "b=int(input())\n", "c=math.pow(a,b)\n", "print('Power = %d' %(c))\n", "\n", "import math\n", "\n", "x = 10.346\n", "print(\"ceil\", math.ceil(x))\n", "print(\"floor\", math.floor(x))\n", "\n", "print(\"factorial\", math.factorial(9))\n", "print(\"absolute value: \", math.fabs(-10))\n", "\n", "print(\"exponenetial value\", math.exp(9))\n", "print(\"power \", math.pow(2, 3))\n", "\n", "angle = math.pi / 4\n", "print(\"sine\", math.sin(angle))\n", "print(\"cosine\", math.cos(angle))\n", "print(\"tangent\", math.tan(angle))\n", "\n", "functions = dir(math)\n", "\n", "\n", "import math\n", "for method in dir(math):\n", " print(method)\n", "\n", "\n" ], "metadata": { "id": "3ums-7fggSDF", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "98e8cc45-4f58-4867-8eea-0d9e53db175e" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1.0\n", "120\n", "12\n", "13\n", "148.4131591025766\n", "1.6094379124341003\n", "0.6989700043360189\n", "64.0\n", "25.0\n", "5\n", "20\n", "120\n", "5.0\n", "3.0\n", "8.0\n", "25.0\n", "1.0\n", "-1.0\n", "0.9999999999999999\n", "1.5707963267948966\n", "1.5707963267948966\n", "0.7853981633974483\n", "1.0\n", "2.0\n", "4\n", "3\n", "3\n", "3.141592653589793\n", "2.718281828459045\n", "The area of the circle with radius 5 is 78.54 square units.\n", "The sine of 45 degrees is 0.71\n", "The cosine of 45 degrees is 0.71\n", "The natural logarithm of 100 is 4.61\n", "The base-10 logarithm of 100 is 2.00\n", "Ceiling of 3.7 is 4\n", "Floor of 3.7 is 3\n", "Truncated value of 3.7 is 3\n", "Enter an integer number 2\n", "Absolute value of 2 is 2\n", "Enter an integer number 3\n", "Square root of 3 is 1\n", "Enter a floating point (decimal) number 4\n", "Ceil value of 4.000000 is 4.000000\n", "Enter a floating point (decimal) number 3\n", "Floor value of 3.000000 is 3.000000\n", "Enter two floating point (decimal) numbers\n", "4\n", "5\n", "Remainder = 4.000000\n", "Enter two integer numbers\n", "4\n", "5\n", "Power = 1024\n", "ceil 11\n", "floor 10\n", "factorial 362880\n", "absolute value: 10.0\n", "exponenetial value 8103.083927575384\n", "power 8.0\n", "sine 0.7071067811865475\n", "cosine 0.7071067811865476\n", "tangent 0.9999999999999999\n", "__doc__\n", "__loader__\n", "__name__\n", "__package__\n", "__spec__\n", "acos\n", "acosh\n", "asin\n", "asinh\n", "atan\n", "atan2\n", "atanh\n", "cbrt\n", "ceil\n", "comb\n", "copysign\n", "cos\n", "cosh\n", "degrees\n", "dist\n", "e\n", "erf\n", "erfc\n", "exp\n", "exp2\n", "expm1\n", "fabs\n", "factorial\n", "floor\n", "fmod\n", "frexp\n", "fsum\n", "gamma\n", "gcd\n", "hypot\n", "inf\n", "isclose\n", "isfinite\n", "isinf\n", "isnan\n", "isqrt\n", "lcm\n", "ldexp\n", "lgamma\n", "log\n", "log10\n", "log1p\n", "log2\n", "modf\n", "nan\n", "nextafter\n", "perm\n", "pi\n", "pow\n", "prod\n", "radians\n", "remainder\n", "sin\n", "sinh\n", "sqrt\n", "sumprod\n", "tan\n", "tanh\n", "tau\n", "trunc\n", "ulp\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **The Integer, Floating-Point, and String Data Types**\n", "Remember that expressions are just values combined with operators, and they always evaluate to a single value. A data type is a category for values, and every value belongs to exactly one data type. The most common data types in Python are listed in Table 1-2. The values -2 and 30, for example, are said to be integer values. The integer (or int) data type indicates values that are whole numbers. Numbers with a decimal point, such as 3.14, are called floating-point numbers (or floats). Note that even though the value 42 is an integer, the value 42.0 would be a floating-point number. Programmers often use number to refer to ints and floats collectively, although number itself is not a Python data type.\n", "\n", "One subtle detail about Python is that any math performed using an int and a float results in a float, not an int. While 3 + 4 evaluates to the integer 7, the expression 3 + 4.0 evaluates to the floating-point number 7.0. Any division between two integers with the / division operator results in a float as well. For example, 16 / 4 evaluates to 4.0 and not 4. Most of the time, this information doesn’t matter for your program, but knowing it will explain why your numbers may suddenly gain a decimal point." ], "metadata": { "id": "CeWadqU5gSL0" } } ] }