{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3ffe5dfc-2e81-4427-87ba-48b6cdf12c16", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pi is 3.141592653589793\n", "cos(pi) is -1.0\n" ] } ], "source": [ "import math\n", "\n", "print('pi is', math.pi)\n", "print('cos(pi) is', math.cos(math.pi))" ] }, { "cell_type": "code", "execution_count": 2, "id": "3d150286-becb-4bca-9d5a-05ec50f2877f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on module math:\n", "\n", "NAME\n", " math\n", "\n", "MODULE REFERENCE\n", " https://docs.python.org/3.12/library/math.html\n", "\n", " The following documentation is automatically generated from the Python\n", " source files. It may be incomplete, incorrect or include features that\n", " are considered implementation detail and may vary between Python\n", " implementations. When in doubt, consult the module reference at the\n", " location listed above.\n", "\n", "DESCRIPTION\n", " This module provides access to the mathematical functions\n", " defined by the C standard.\n", "\n", "FUNCTIONS\n", " acos(x, /)\n", " Return the arc cosine (measured in radians) of x.\n", "\n", " The result is between 0 and pi.\n", "\n", " acosh(x, /)\n", " Return the inverse hyperbolic cosine of x.\n", "\n", " asin(x, /)\n", " Return the arc sine (measured in radians) of x.\n", "\n", " The result is between -pi/2 and pi/2.\n", "\n", " asinh(x, /)\n", " Return the inverse hyperbolic sine of x.\n", "\n", " atan(x, /)\n", " Return the arc tangent (measured in radians) of x.\n", "\n", " The result is between -pi/2 and pi/2.\n", "\n", " atan2(y, x, /)\n", " Return the arc tangent (measured in radians) of y/x.\n", "\n", " Unlike atan(y/x), the signs of both x and y are considered.\n", "\n", " atanh(x, /)\n", " Return the inverse hyperbolic tangent of x.\n", "\n", " cbrt(x, /)\n", " Return the cube root of x.\n", "\n", " ceil(x, /)\n", " Return the ceiling of x as an Integral.\n", "\n", " This is the smallest integer >= x.\n", "\n", " comb(n, k, /)\n", " Number of ways to choose k items from n items without repetition and without order.\n", "\n", " Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n", " to zero when k > n.\n", "\n", " Also called the binomial coefficient because it is equivalent\n", " to the coefficient of k-th term in polynomial expansion of the\n", " expression (1 + x)**n.\n", "\n", " Raises TypeError if either of the arguments are not integers.\n", " Raises ValueError if either of the arguments are negative.\n", "\n", " copysign(x, y, /)\n", " Return a float with the magnitude (absolute value) of x but the sign of y.\n", "\n", " On platforms that support signed zeros, copysign(1.0, -0.0)\n", " returns -1.0.\n", "\n", " cos(x, /)\n", " Return the cosine of x (measured in radians).\n", "\n", " cosh(x, /)\n", " Return the hyperbolic cosine of x.\n", "\n", " degrees(x, /)\n", " Convert angle x from radians to degrees.\n", "\n", " dist(p, q, /)\n", " Return the Euclidean distance between two points p and q.\n", "\n", " The points should be specified as sequences (or iterables) of\n", " coordinates. Both inputs must have the same dimension.\n", "\n", " Roughly equivalent to:\n", " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))\n", "\n", " erf(x, /)\n", " Error function at x.\n", "\n", " erfc(x, /)\n", " Complementary error function at x.\n", "\n", " exp(x, /)\n", " Return e raised to the power of x.\n", "\n", " exp2(x, /)\n", " Return 2 raised to the power of x.\n", "\n", " expm1(x, /)\n", " Return exp(x)-1.\n", "\n", " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", "\n", " fabs(x, /)\n", " Return the absolute value of the float x.\n", "\n", " factorial(n, /)\n", " Find n!.\n", "\n", " Raise a ValueError if x is negative or non-integral.\n", "\n", " floor(x, /)\n", " Return the floor of x as an Integral.\n", "\n", " This is the largest integer <= x.\n", "\n", " fmod(x, y, /)\n", " Return fmod(x, y), according to platform C.\n", "\n", " x % y may differ.\n", "\n", " frexp(x, /)\n", " Return the mantissa and exponent of x, as pair (m, e).\n", "\n", " m is a float and e is an int, such that x = m * 2.**e.\n", " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", "\n", " fsum(seq, /)\n", " Return an accurate floating point sum of values in the iterable seq.\n", "\n", " Assumes IEEE-754 floating point arithmetic.\n", "\n", " gamma(x, /)\n", " Gamma function at x.\n", "\n", " gcd(*integers)\n", " Greatest Common Divisor.\n", "\n", " hypot(...)\n", " hypot(*coordinates) -> value\n", "\n", " Multidimensional Euclidean distance from the origin to a point.\n", "\n", " Roughly equivalent to:\n", " sqrt(sum(x**2 for x in coordinates))\n", "\n", " For a two dimensional point (x, y), gives the hypotenuse\n", " using the Pythagorean theorem: sqrt(x*x + y*y).\n", "\n", " For example, the hypotenuse of a 3/4/5 right triangle is:\n", "\n", " >>> hypot(3.0, 4.0)\n", " 5.0\n", "\n", " isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)\n", " Determine whether two floating point numbers are close in value.\n", "\n", " rel_tol\n", " maximum difference for being considered \"close\", relative to the\n", " magnitude of the input values\n", " abs_tol\n", " maximum difference for being considered \"close\", regardless of the\n", " magnitude of the input values\n", "\n", " Return True if a is close in value to b, and False otherwise.\n", "\n", " For the values to be considered close, the difference between them\n", " must be smaller than at least one of the tolerances.\n", "\n", " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", " is, NaN is not close to anything, even itself. inf and -inf are\n", " only close to themselves.\n", "\n", " isfinite(x, /)\n", " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", "\n", " isinf(x, /)\n", " Return True if x is a positive or negative infinity, and False otherwise.\n", "\n", " isnan(x, /)\n", " Return True if x is a NaN (not a number), and False otherwise.\n", "\n", " isqrt(n, /)\n", " Return the integer part of the square root of the input.\n", "\n", " lcm(*integers)\n", " Least Common Multiple.\n", "\n", " ldexp(x, i, /)\n", " Return x * (2**i).\n", "\n", " This is essentially the inverse of frexp().\n", "\n", " lgamma(x, /)\n", " Natural logarithm of absolute value of Gamma function at x.\n", "\n", " log(...)\n", " log(x, [base=math.e])\n", " Return the logarithm of x to the given base.\n", "\n", " If the base is not specified, returns the natural logarithm (base e) of x.\n", "\n", " log10(x, /)\n", " Return the base 10 logarithm of x.\n", "\n", " log1p(x, /)\n", " Return the natural logarithm of 1+x (base e).\n", "\n", " The result is computed in a way which is accurate for x near zero.\n", "\n", " log2(x, /)\n", " Return the base 2 logarithm of x.\n", "\n", " modf(x, /)\n", " Return the fractional and integer parts of x.\n", "\n", " Both results carry the sign of x and are floats.\n", "\n", " nextafter(x, y, /, *, steps=None)\n", " Return the floating-point value the given number of steps after x towards y.\n", "\n", " If steps is not specified or is None, it defaults to 1.\n", "\n", " Raises a TypeError, if x or y is not a double, or if steps is not an integer.\n", " Raises ValueError if steps is negative.\n", "\n", " perm(n, k=None, /)\n", " Number of ways to choose k items from n items without repetition and with order.\n", "\n", " Evaluates to n! / (n - k)! when k <= n and evaluates\n", " to zero when k > n.\n", "\n", " If k is not specified or is None, then k defaults to n\n", " and the function returns n!.\n", "\n", " Raises TypeError if either of the arguments are not integers.\n", " Raises ValueError if either of the arguments are negative.\n", "\n", " pow(x, y, /)\n", " Return x**y (x to the power of y).\n", "\n", " prod(iterable, /, *, start=1)\n", " Calculate the product of all the elements in the input iterable.\n", "\n", " The default start value for the product is 1.\n", "\n", " When the iterable is empty, return the start value. This function is\n", " intended specifically for use with numeric values and may reject\n", " non-numeric types.\n", "\n", " radians(x, /)\n", " Convert angle x from degrees to radians.\n", "\n", " remainder(x, y, /)\n", " Difference between x and the closest integer multiple of y.\n", "\n", " Return x - n*y where n*y is the closest integer multiple of y.\n", " In the case where x is exactly halfway between two multiples of\n", " y, the nearest even value of n is used. The result is always exact.\n", "\n", " sin(x, /)\n", " Return the sine of x (measured in radians).\n", "\n", " sinh(x, /)\n", " Return the hyperbolic sine of x.\n", "\n", " sqrt(x, /)\n", " Return the square root of x.\n", "\n", " sumprod(p, q, /)\n", " Return the sum of products of values from two iterables p and q.\n", "\n", " Roughly equivalent to:\n", "\n", " sum(itertools.starmap(operator.mul, zip(p, q, strict=True)))\n", "\n", " For float and mixed int/float inputs, the intermediate products\n", " and sums are computed with extended precision.\n", "\n", " tan(x, /)\n", " Return the tangent of x (measured in radians).\n", "\n", " tanh(x, /)\n", " Return the hyperbolic tangent of x.\n", "\n", " trunc(x, /)\n", " Truncates the Real x to the nearest Integral toward 0.\n", "\n", " Uses the __trunc__ magic method.\n", "\n", " ulp(x, /)\n", " Return the value of the least significant bit of the float x.\n", "\n", "DATA\n", " e = 2.718281828459045\n", " inf = inf\n", " nan = nan\n", " pi = 3.141592653589793\n", " tau = 6.283185307179586\n", "\n", "FILE\n", " /opt/conda/envs/und-fall-2024/lib/python3.12/lib-dynload/math.cpython-312-x86_64-linux-gnu.so\n", "\n", "\n" ] } ], "source": [ "help(math)" ] }, { "cell_type": "code", "execution_count": 3, "id": "fa5c5b1c-b34e-46d9-959f-d499b9b3b3d0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cos(pi) is -1.0\n" ] } ], "source": [ "from math import cos, pi\n", "print('cos(pi) is', cos(pi))" ] }, { "cell_type": "code", "execution_count": 4, "id": "e172a4c7-34d2-4825-9aaa-0ca17d50cc27", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cos(pi) is -1.0\n" ] } ], "source": [ "import math as m\n", "print('cos(pi) is', m.cos(m.pi))" ] }, { "cell_type": "raw", "id": "aa2742b9-8a68-45e1-8015-f9168ce4a2d7", "metadata": {}, "source": [ " What function from the math module can you use to calculate a square root without using sqrt?\n", "\n", "Since the library contains this function, why does sqrt exist?\n" ] }, { "cell_type": "raw", "id": "05b65b92-91c7-43cc-aa74-1f6cce3ffe2c", "metadata": {}, "source": [ "x=9\n", "pow(9,0.5)" ] }, { "cell_type": "code", "execution_count": 5, "id": "77373d65-d784-4bf5-aa8f-927bb68a05d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.0\n" ] } ], "source": [ "print(pow(9,0.5))" ] }, { "cell_type": "code", "execution_count": 7, "id": "d2cfa6c4-56b4-49d8-b80e-fc17229641c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.0\n" ] } ], "source": [ "print(math.sqrt(9.0))" ] }, { "cell_type": "raw", "id": "6d33c64b-dd1e-46a6-a83f-491fe393e3b4", "metadata": {}, "source": [ "You want to select a random character from a string:" ] }, { "cell_type": "code", "execution_count": 8, "id": "fb358592-36e5-4d0e-af51-912d0c6fd212", "metadata": {}, "outputs": [], "source": [ "bases = 'ACTTGCTTGAC'" ] }, { "cell_type": "raw", "id": "0101a5df-76b8-49b4-960e-e420669a77d1", "metadata": {}, "source": [ " Which standard library module could help you?\n", " Which function would you select from that module? Are there alternatives?\n", " Try to write a program that uses the function." ] }, { "cell_type": "code", "execution_count": 16, "id": "665b22b1-464f-4b5d-9dbc-bf09425754b3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n" ] } ], "source": [ "from random import randrange\n", "\n", "random_index = randrange(len(bases))\n", "print(bases[random_index])" ] }, { "cell_type": "code", "execution_count": 18, "id": "6fc1ce1f-b719-4ea2-b05b-2b82af349fb9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C\n" ] } ], "source": [ "print(bases[randrange(len(bases))])" ] }, { "cell_type": "raw", "id": "5fad1346-0513-47a5-a367-86865915a6cc", "metadata": {}, "source": [ "Rearrange the following statements so that a random DNA base is printed and its index in the string." ] }, { "cell_type": "raw", "id": "ca30a2a3-58c9-41bb-9d83-cb201332a7cc", "metadata": {}, "source": [ "Not all statements may be needed. Feel free to use/add intermediate variables." ] }, { "cell_type": "code", "execution_count": 21, "id": "9e68782c-adf3-4e51-a327-779ffba705b1", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'n_bases' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[21], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmath\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrandom\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m ___ \u001b[38;5;241m=\u001b[39m random\u001b[38;5;241m.\u001b[39mrandrange(\u001b[43mn_bases\u001b[49m)\n\u001b[1;32m 5\u001b[0m ___ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlen\u001b[39m(bases)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrandom base \u001b[39m\u001b[38;5;124m\"\u001b[39m, bases[___], \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbase index\u001b[39m\u001b[38;5;124m\"\u001b[39m, ___)\n", "\u001b[0;31mNameError\u001b[0m: name 'n_bases' is not defined" ] } ], "source": [ "bases=\"ACTTGCTTGAC\"\n", "import math\n", "import random\n", "___ = random.randrange(n_bases)\n", "___ = len(bases)\n", "print(\"random base \", bases[___], \"base index\", ___)" ] }, { "cell_type": "code", "execution_count": 26, "id": "2e2cef1c-416a-4205-a2a4-0e1c6d9c8821", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "random base T base index 2\n" ] } ], "source": [ "import math\n", "import random\n", "bases = \"ACTTGCTTGAC\"\n", "n_bases = len(bases)\n", "idx = random.randrange(n_bases)\n", "print(\"random base\", bases[idx], \"base index\", idx)\n" ] }, { "cell_type": "raw", "id": "13970cda-b332-4d98-87cd-7be5a4523207", "metadata": {}, "source": [ "When a colleague of yours types help(math), Python reports an error:\n", "ERROR\n", "\n", "NameError: name 'math' is not defined\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "090ed299-23d6-4801-afbd-749a026a0a08", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "math domain error", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[27], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmath\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m log\n\u001b[0;32m----> 2\u001b[0m \u001b[43mlog\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mValueError\u001b[0m: math domain error" ] } ], "source": [ "from math import log\n", "log(0)" ] }, { "cell_type": "code", "execution_count": null, "id": "664887ca-e1aa-4eef-b5aa-d541605db91d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:und-fall-2024]", "language": "python", "name": "conda-env-und-fall-2024-py" }, "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.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }