{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "import math" ], "metadata": { "id": "GLAbry69tEyt" }, "execution_count": 1, "outputs": [] }, { "cell_type": "code", "source": [ "import math\n", "print(dir(math))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "i6XpDQgdtFQt", "outputId": "238d2455-28a9-49d8-8013-e369a96a4518" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Number Functions\")\n", "\n", "print(\"math.ceil(4.3) =\", math.ceil(4.3)) # Smallest integer ≥ value\n", "print(\"math.floor(4.7) =\", math.floor(4.7)) # Largest integer ≤ value\n", "print(\"math.trunc(5.9) =\", math.trunc(5.9)) # Truncates decimal part\n", "print(\"math.fabs(-7.5) =\", math.fabs(-7.5)) # Absolute value\n", "print(\"math.factorial(5) =\", math.factorial(5)) # 5! = 120\n", "print(\"math.modf(5.75) =\", math.modf(5.75)) # (fractional, integer) parts\n", "print(\"math.copysign(3, -8) =\", math.copysign(3, -8)) # Copy sign of second value to first\n", "print(\"math.isfinite(10) =\", math.isfinite(10)) # True if finite\n", "print(\"math.isinf(math.inf) =\", math.isinf(math.inf)) # True for infinity\n", "print(\"math.isnan(math.nan) =\", math.isnan(math.nan)) # True for NaN" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2IHdJu7gtFTe", "outputId": "59769edd-e645-4603-8350-fbaa36c2098f" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Number Functions\n", "math.ceil(4.3) = 5\n", "math.floor(4.7) = 4\n", "math.trunc(5.9) = 5\n", "math.fabs(-7.5) = 7.5\n", "math.factorial(5) = 120\n", "math.modf(5.75) = (0.75, 5.0)\n", "math.copysign(3, -8) = -3.0\n", "math.isfinite(10) = True\n", "math.isinf(math.inf) = True\n", "math.isnan(math.nan) = True\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Power, Root, Exponential, Log Functions\")\n", "\n", "print(\"math.pow(2, 3) =\", math.pow(2, 3)) # 2^3 = 8.0\n", "print(\"math.sqrt(16) =\", math.sqrt(16)) # Square root\n", "print(\"math.exp(1) =\", math.exp(1)) # e^1\n", "print(\"math.expm1(1) =\", math.expm1(1)) # e^1 - 1\n", "print(\"math.log(8, 2) =\", math.log(8, 2)) # Log base 2\n", "print(\"math.log2(16) =\", math.log2(16)) # Log base 2\n", "print(\"math.log10(1000) =\", math.log10(1000)) # Log base 10\n", "print(\"math.log1p(1) =\", math.log1p(1)) # log(1+x) accurate for small x" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kEj9tffrtFWE", "outputId": "fc83336e-cc3b-458d-ffda-9679a1813f02" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Power, Root, Exponential, Log Functions\n", "math.pow(2, 3) = 8.0\n", "math.sqrt(16) = 4.0\n", "math.exp(1) = 2.718281828459045\n", "math.expm1(1) = 1.718281828459045\n", "math.log(8, 2) = 3.0\n", "math.log2(16) = 4.0\n", "math.log10(1000) = 3.0\n", "math.log1p(1) = 0.6931471805599453\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Combinatorics\")\n", "\n", "print(\"math.comb(5, 2) =\", math.comb(5, 2)) # 5 choose 2 = 10\n", "print(\"math.perm(5, 2) =\", math.perm(5, 2)) # Permutations of 5P2 = 20" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MFGdJgZctFY2", "outputId": "4b7e8d0b-f94b-4fc3-9fc1-522545d34a3f" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Combinatorics\n", "math.comb(5, 2) = 10\n", "math.perm(5, 2) = 20\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"GCD, LCM, Modulus\")\n", "\n", "print(\"math.gcd(20, 8) =\", math.gcd(20, 8)) # 4\n", "print(\"math.lcm(12, 15) =\", math.lcm(12, 15)) # 60\n", "print(\"math.fmod(20, 3) =\", math.fmod(20, 3)) # Remainder (modulus)\n", "print(\"math.remainder(20, 3) =\", math.remainder(20, 3)) # IEEE remainder" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_oOoxuLmu9fJ", "outputId": "b73137a9-931e-4dc7-e2cd-eb89dfa50ac8" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "GCD, LCM, Modulus\n", "math.gcd(20, 8) = 4\n", "math.lcm(12, 15) = 60\n", "math.fmod(20, 3) = 2.0\n", "math.remainder(20, 3) = -1.0\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Trigonometric\")\n", "\n", "print(\"math.sin(math.radians(90)) =\", math.sin(math.radians(90))) # sin 90°\n", "print(\"math.cos(math.radians(0)) =\", math.cos(math.radians(0))) # cos 0°\n", "print(\"math.tan(math.radians(45)) =\", math.tan(math.radians(45))) # tan 45°\n", "print(\"math.degrees(math.pi) =\", math.degrees(math.pi)) # Convert pi radians to degrees\n", "print(\"math.radians(180) =\", math.radians(180)) # 180° to radians" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0NdRBWSPu9hx", "outputId": "d1b3cb3e-4258-4a9b-d112-cda4fffc00e9" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Trigonometric\n", "math.sin(math.radians(90)) = 1.0\n", "math.cos(math.radians(0)) = 1.0\n", "math.tan(math.radians(45)) = 0.9999999999999999\n", "math.degrees(math.pi) = 180.0\n", "math.radians(180) = 3.141592653589793\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Inverse Trigonometric\")\n", "\n", "print(\"math.asin(1) =\", math.asin(1)) # arcsin(1)\n", "print(\"math.acos(1) =\", math.acos(1)) # arccos(1)\n", "print(\"math.atan(1) =\", math.atan(1)) # arctan(1)\n", "print(\"math.atan2(1, 1) =\", math.atan2(1, 1)) # arctan(y/x) handling quadrants" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GhNoEUOvu9kZ", "outputId": "f9ca0b72-e6e4-4a30-9cce-ecdce8fd3ab4" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Inverse Trigonometric\n", "math.asin(1) = 1.5707963267948966\n", "math.acos(1) = 0.0\n", "math.atan(1) = 0.7853981633974483\n", "math.atan2(1, 1) = 0.7853981633974483\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Hyperbolic\")\n", "\n", "print(\"math.sinh(0) =\", math.sinh(0)) # Hyperbolic sine\n", "print(\"math.cosh(0) =\", math.cosh(0)) # Hyperbolic cosine\n", "print(\"math.tanh(0) =\", math.tanh(0)) # Hyperbolic tangent\n", "print(\"math.asinh(1) =\", math.asinh(1)) # Inverse hyperbolic sine\n", "print(\"math.acosh(1) =\", math.acosh(1)) # Inverse hyperbolic cosine\n", "print(\"math.atanh(0.5) =\", math.atanh(0.5)) # Inverse hyperbolic tangent" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7Aql4TAuu9nZ", "outputId": "bd5f7331-ffc1-42bf-c80c-d6c097af7055" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hyperbolic\n", "math.sinh(0) = 0.0\n", "math.cosh(0) = 1.0\n", "math.tanh(0) = 0.0\n", "math.asinh(1) = 0.881373587019543\n", "math.acosh(1) = 0.0\n", "math.atanh(0.5) = 0.5493061443340548\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Exponent and Mantissa\")\n", "\n", "print(\"math.frexp(8) =\", math.frexp(8)) # (mantissa, exponent)\n", "print(\"math.ldexp(0.5, 4) =\", math.ldexp(0.5, 4)) # 0.5 × 2^4 = 8" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "n48cIfMbvDBB", "outputId": "b46834ee-ac7b-4e36-893e-47e32587392f" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Exponent and Mantissa\n", "math.frexp(8) = (0.5, 4)\n", "math.ldexp(0.5, 4) = 8.0\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Floating Point Summation and Precision Utilities\")\n", "\n", "print(\"math.fsum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) =\",math.fsum([0.1] * 10)) # Accurate sum of floats\n", "print(\"math.isclose(0.1 + 0.2, 0.3) =\", math.isclose(0.1 + 0.2, 0.3)) # Check closeness\n", "print(\"math.ulp(1.0) =\", math.ulp(1.0)) # Unit in last place (smallest diff representable)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CjDF8o_1vDDp", "outputId": "5baaaea1-9f70-4cc2-96fa-d3c0b660460f" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Floating Point Summation and Precision Utilities\n", "math.fsum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) = 1.0\n", "math.isclose(0.1 + 0.2, 0.3) = True\n", "math.ulp(1.0) = 2.220446049250313e-16\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Geometry & Distance Functions\")\n", "\n", "print(\"math.hypot(3, 4) =\", math.hypot(3, 4)) # sqrt(3^2 + 4^2)\n", "print(\"math.dist((0,0), (3,4)) =\", math.dist((0,0), (3,4))) # Euclidean distance\n", "print(\"math.prod([1, 2, 3, 4]) =\", math.prod([1, 2, 3, 4])) # Product of list\n", "print(\"math.isqrt(10) =\", math.isqrt(10)) # Integer square root" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HqcqZ9pTvDGa", "outputId": "6ee3ba7e-fab1-435c-8d0e-f90188133a0d" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Geometry & Distance Functions\n", "math.hypot(3, 4) = 5.0\n", "math.dist((0,0), (3,4)) = 5.0\n", "math.prod([1, 2, 3, 4]) = 24\n", "math.isqrt(10) = 3\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Special Functions\")\n", "\n", "print(\"math.gamma(5) =\", math.gamma(5)) # Gamma(5) = (5-1)!\n", "print(\"math.lgamma(5) =\", math.lgamma(5)) # log(gamma(5))\n", "print(\"math.erf(1) =\", math.erf(1)) # Error function value at 1\n", "print(\"math.erfc(1) =\", math.erfc(1)) # Complementary error function" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YSgWcmB3vDI4", "outputId": "6d7ed32e-07e2-45cc-b459-1a21647bda5e" }, "execution_count": 13, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Special Functions\n", "math.gamma(5) = 24.0\n", "math.lgamma(5) = 3.178053830347945\n", "math.erf(1) = 0.8427007929497149\n", "math.erfc(1) = 0.15729920705028513\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Constants\")\n", "\n", "print(\"math.pi =\", math.pi) # π\n", "print(\"math.e =\", math.e) # Euler's number e\n", "print(\"math.inf =\", math.inf) # Infinity\n", "print(\"math.nan =\", math.nan) # Not a number (NaN)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oN3lXBGvvDLh", "outputId": "b300e226-faed-4fb7-e9aa-d7b2f20264f7" }, "execution_count": 14, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Constants\n", "math.pi = 3.141592653589793\n", "math.e = 2.718281828459045\n", "math.inf = inf\n", "math.nan = nan\n" ] } ] }, { "cell_type": "code", "source": [ "# 👷‍♂️ Meet Bob the Builder: The Math-Savvy Architect!\n", "'''\n", "Bob is designing a pitched roof. He needs to figure out how long the sloping sides should be,\n", "how much area they'll cover, how steep the roof is, how many tiles he’ll need, and what the total cost will be.\n", "\n", "Math Concepts Used: Hypotenuse, area, angle, ceiling, degrees.\n", "math.hypot, math.atan, math.degrees, math.ceil, math.floor\n", "'''" ], "metadata": { "id": "um-pazPtu9qR" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# House dimensions\n", "base_width = 8 # in meters\n", "roof_height = 3 # in meters\n", "cost_per_sq_meter = 250 # ₹ per square meter\n", "tile_length = 0.5 # meters\n", "tile_width = 0.5 # meters\n", "\n", "# Calculations\n", "slope_length = math.hypot(base_width / 2, roof_height)\n", "roof_area = 2 * (slope_length * base_width / 2)\n", "angle_radians = math.atan(roof_height / (base_width / 2))\n", "angle_degrees = math.degrees(angle_radians)\n", "total_cost = math.ceil(roof_area) * cost_per_sq_meter\n", "tile_area = tile_length * tile_width\n", "num_tiles = math.ceil(roof_area / tile_area)\n", "\n", "# Output\n", "print(f\"Roof slope length: {slope_length:.2f} meters\")\n", "print(f\"Roof area: {roof_area:.2f} square meters\")\n", "print(f\"Roof angle: {angle_degrees:.2f} degrees\")\n", "print(f\"Estimated cost: ₹{total_cost}\")\n", "print(f\"Number of tiles needed: {num_tiles}\")\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8KL6rTIsvnba", "outputId": "47785279-8134-48e0-c858-055578bd2823" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Roof slope length: 5.00 meters\n", "Roof area: 40.00 square meters\n", "Roof angle: 36.87 degrees\n", "Estimated cost: ₹10000\n", "Number of tiles needed: 160\n" ] } ] }, { "cell_type": "code", "source": [ "'''\n", "Bob wants a beautiful round garden around the house. He calculates the area and\n", "boundary of the circle, how much topsoil is needed, fencing length, and cost estimates.\n", "\n", "Math Concepts Used: Area of a circle, circumference, volume, rounding up/down, constants.\n", "math.pi, math.pow, math.ceil, math.floor, math.isclose\n", "'''" ], "metadata": { "id": "WuCNbYGUvneT" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "radius = 7.5 # meters\n", "depth = 0.3 # meters (topsoil)\n", "cost_per_cubic_meter = 90 # ₹\n", "cost_per_meter_fence = 35 # ₹\n", "\n", "# Calculations\n", "area = math.pi * math.pow(radius, 2)\n", "circumference = 2 * math.pi * radius\n", "volume = area * depth\n", "rounded_area = math.ceil(area)\n", "rounded_circumference = math.floor(circumference)\n", "total_soil_cost = math.ceil(volume * cost_per_cubic_meter)\n", "fence_cost = math.ceil(circumference * cost_per_meter_fence)\n", "\n", "# Output\n", "print(f\"Garden area: {area:.2f} sq meters\")\n", "print(f\"Garden circumference: {circumference:.2f} meters\")\n", "print(f\"Topsoil volume: {volume:.2f} cubic meters\")\n", "print(f\"Total soil cost: ₹{total_soil_cost}\")\n", "print(f\"Fencing cost: ₹{fence_cost}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "P4n4Mrpnvnhi", "outputId": "0facaab3-0dbd-4986-9ebf-272624afc4fc" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Garden area: 176.71 sq meters\n", "Garden circumference: 47.12 meters\n", "Topsoil volume: 53.01 cubic meters\n", "Total soil cost: ₹4772\n", "Fencing cost: ₹1650\n" ] } ] }, { "cell_type": "code", "source": [ "'''\n", "Bob is building a staircase. He needs to know how many steps are required,\n", "how long the stairs will be, the angle for safety, the area for tiling, and if\n", "the slope is within safety limits.\n", "\n", "Math Concepts Used: Hypotenuse, tangent, angle, number of steps, area.\n", "math.ceil, math.hypot, math.atan, math.degrees, math.floor\n", "'''" ], "metadata": { "id": "X4LD9TN7vnkj" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "total_height = 3 # meters\n", "step_height = 0.15 # meters\n", "step_depth = 0.30 # meters\n", "\n", "# Calculations\n", "num_steps = math.ceil(total_height / step_height)\n", "actual_height = num_steps * step_height\n", "total_depth = num_steps * step_depth\n", "stair_length = math.hypot(actual_height, total_depth)\n", "angle_radians = math.atan(actual_height / total_depth)\n", "angle_degrees = math.degrees(angle_radians)\n", "tread_area = num_steps * step_depth * 1 # 1 meter width\n", "\n", "# Output\n", "print(f\"Total steps: {num_steps}\")\n", "print(f\"Actual height: {actual_height:.2f} meters\")\n", "print(f\"Staircase length: {stair_length:.2f} meters\")\n", "print(f\"Stair angle: {angle_degrees:.2f} degrees\")\n", "print(f\"Tread surface area: {tread_area:.2f} sq meters\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3AVTS4wsvnnb", "outputId": "dd82541e-5178-4deb-9758-e7b1f837f573" }, "execution_count": 17, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Total steps: 20\n", "Actual height: 3.00 meters\n", "Staircase length: 6.71 meters\n", "Stair angle: 26.57 degrees\n", "Tread surface area: 6.00 sq meters\n" ] } ] }, { "cell_type": "code", "source": [ "'''Bob is adding a semi-circular arch to the house entrance. He calculates the\n", "curve length, surface area, height needed for equal spacing, and how many bricks will form the arch.\n", "\n", "Math Concepts Used: Semi-circle arc, radius, angle, sine/cosine for placement.\n", "math.pi, math.sin, math.cos, math.radians, math.ceil'''" ], "metadata": { "id": "ctZBp4WpvnqE" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "radius = 2.5 # meters\n", "brick_width = 0.3 # meters\n", "thickness = 0.2 # meters\n", "\n", "# Calculations\n", "arc_length = math.pi * radius\n", "curve_area = arc_length * thickness\n", "height = radius\n", "num_bricks = math.ceil(arc_length / brick_width)\n", "angle_per_brick = 180 / num_bricks\n", "first_angle = math.radians(angle_per_brick)\n", "\n", "# Output\n", "print(f\"Arc length: {arc_length:.2f} meters\")\n", "print(f\"Curve surface area: {curve_area:.2f} sq meters\")\n", "print(f\"Arch height: {height} meters\")\n", "print(f\"Bricks needed: {num_bricks}\")\n", "print(f\"Angle between bricks: {angle_per_brick:.2f} degrees\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PuxsJBUYy4ip", "outputId": "83f9365b-a985-402e-f569-d94fcbb53dad" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Arc length: 7.85 meters\n", "Curve surface area: 1.57 sq meters\n", "Arch height: 2.5 meters\n", "Bricks needed: 27\n", "Angle between bricks: 6.67 degrees\n" ] } ] }, { "cell_type": "code", "source": [ "'''\n", "Bob is constructing a cylindrical water tank behind the house.\n", "He needs to know the tank’s surface area for painting, volume for\n", "water capacity, and cost estimation.\n", "\n", "Math Concepts Used: Cylinder volume, surface area, pi constant, rounding.\n", "math.pi, math.pow, math.ceil, math.floor, math.isclose\n", "'''" ], "metadata": { "id": "4bfWtePLy4nh" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "radius = 1.2 # meters\n", "height = 2.5 # meters\n", "paint_cost_per_sqm = 45 # ₹ per sq meter\n", "water_cost_per_litre = 0.07 # ₹ per litre\n", "\n", "# Calculations\n", "lateral_area = 2 * math.pi * radius * height\n", "circle_area = math.pi * math.pow(radius, 2)\n", "total_surface_area = lateral_area + 2 * circle_area\n", "volume_cubic_m = circle_area * height\n", "volume_litres = volume_cubic_m * 1000\n", "painting_cost = math.ceil(total_surface_area * paint_cost_per_sqm)\n", "water_cost = math.floor(volume_litres * water_cost_per_litre)\n", "\n", "# Output\n", "print(f\"Tank surface area: {total_surface_area:.2f} sq meters\")\n", "print(f\"Water volume: {volume_litres:.2f} litres\")\n", "print(f\"Painting cost: ₹{painting_cost}\")\n", "print(f\"Water fill cost: ₹{water_cost}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_3P9CfWgy4qI", "outputId": "55acda1d-7df4-4fa4-ee63-9d7399cde4c0" }, "execution_count": 19, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Tank surface area: 27.90 sq meters\n", "Water volume: 11309.73 litres\n", "Painting cost: ₹1256\n", "Water fill cost: ₹791\n" ] } ] }, { "cell_type": "code", "source": [ "# change the values and formulae and explore more!" ], "metadata": { "id": "N-bllycSy4sq" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Random\n", "import random" ], "metadata": { "id": "NvV25H4Sy4va" }, "execution_count": 20, "outputs": [] }, { "cell_type": "code", "source": [ "print(dir(random))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AbkCg0W40NBU", "outputId": "f41a3135-8cdc-46e6-dbd2-f991bee8546e" }, "execution_count": 21, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_ONE', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_index', '_inst', '_isfinite', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']\n" ] } ] }, { "cell_type": "code", "source": [ "# random(): random float in [0.0, 1.0)\n", "print(\"random.random() =\", random.random()) # Random float between 0.0 and 1.0\n", "\n", "# randint(a, b): integer in [a, b]\n", "print(\"random.randint(1, 6) =\", random.randint(1, 6)) # Random integer between 1 and 6 (inclusive)\n", "\n", "# uniform(a, b): float in [a, b]\n", "print(\"random.uniform(1.5, 10.5) =\", random.uniform(1.5, 10.5)) # Random float between 1.5 and 10.5\n", "\n", "# randrange(start, stop[, step])\n", "print(\"random.randrange(1, 10, 2) =\", random.randrange(1, 10, 2)) # Random odd number between 1 and 10\n", "\n", "# choice(seq): randomly select an element from a sequence\n", "print(\"random.choice(['apple', 'banana', 'cherry']) =\", random.choice(['apple', 'banana', 'cherry'])) # Random element from list\n", "\n", "# choices(population, weights=None, k=1): list of k elements with replacement\n", "print(\"random.choices(['red', 'blue', 'green'], weights=[10, 1, 1], k=3) =\", random.choices(['red', 'blue', 'green'], weights=[10, 1, 1], k=3)) # Randomly choose 3 items, biased to pick 'red'\n", "\n", "# sample(population, k): list of k unique elements from population\n", "print(\"random.sample([1, 2, 3, 4, 5], 2) =\", random.sample([1, 2, 3, 4, 5], 2)) # Random 2 items from list, without replacement\n", "\n", "# shuffle(x): shuffle a sequence in place\n", "lst = [1, 2, 3, 4]\n", "random.shuffle(lst)\n", "print(\"random.shuffle([1, 2, 3, 4]) =\", lst) # Shuffle list in place\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zvs8NE3h0NGb", "outputId": "b1f6f2ff-d0df-4e72-ecf6-782b6f8d6c12" }, "execution_count": 22, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "random.random() = 0.36140299680641375\n", "random.randint(1, 6) = 4\n", "random.uniform(1.5, 10.5) = 8.74802635459081\n", "random.randrange(1, 10, 2) = 9\n", "random.choice(['apple', 'banana', 'cherry']) = cherry\n", "random.choices(['red', 'blue', 'green'], weights=[10, 1, 1], k=3) = ['red', 'red', 'red']\n", "random.sample([1, 2, 3, 4, 5], 2) = [1, 5]\n", "random.shuffle([1, 2, 3, 4]) = [3, 1, 2, 4]\n" ] } ] }, { "cell_type": "code", "source": [ "# betavariate(alpha, beta): Beta distribution\n", "print(\"random.betavariate(2, 5) =\", random.betavariate(2, 5)) # Random float from Beta distribution\n", "\n", "# expovariate(lambd): Exponential distribution\n", "print(\"random.expovariate(1.5) =\", random.expovariate(1.5)) # Random float from Exponential distribution\n", "\n", "# gammavariate(alpha, beta): Gamma distribution\n", "print(\"random.gammavariate(2, 3) =\", random.gammavariate(2, 3)) # Random float from Gamma distribution\n", "\n", "# gauss(mu, sigma): Gaussian (normal) distribution\n", "print(\"random.gauss(0, 1) =\", random.gauss(0, 1)) # Random float from Normal distribution with mean 0 and std deviation 1\n", "\n", "# lognormvariate(mu, sigma): Log-normal distribution\n", "print(\"random.lognormvariate(0, 0.5) =\", random.lognormvariate(0, 0.5)) # Random float from Log-normal distribution\n", "\n", "# normalvariate(mu, sigma): Normal distribution\n", "print(\"random.normalvariate(100, 15) =\", random.normalvariate(100, 15)) # Random float from Normal distribution with mean 100 and std deviation 15\n", "\n", "# paretovariate(alpha): Pareto distribution\n", "print(\"random.paretovariate(2.5) =\", random.paretovariate(2.5)) # Random float from Pareto distribution\n", "\n", "# triangular(low, high, mode): Triangular distribution\n", "print(\"random.triangular(1, 10, 5) =\", random.triangular(1, 10, 5)) # Random float from Triangular distribution\n", "\n", "# vonmisesvariate(mu, kappa): Circular (Von Mises) distribution\n", "print(\"random.vonmisesvariate(0, 4) =\", random.vonmisesvariate(0, 4)) # Random float from Von Mises distribution\n", "\n", "# weibullvariate(alpha, beta): Weibull distribution\n", "print(\"random.weibullvariate(1, 1.5) =\", random.weibullvariate(1, 1.5)) # Random float from Weibull distribution\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uahy7qCq0NI-", "outputId": "b4dc34e5-8b9d-42b2-975f-aba5ce5aed02" }, "execution_count": 23, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "random.betavariate(2, 5) = 0.5484775678490088\n", "random.expovariate(1.5) = 0.1958462273940966\n", "random.gammavariate(2, 3) = 16.29316138540588\n", "random.gauss(0, 1) = -1.3421940643242969\n", "random.lognormvariate(0, 0.5) = 1.3598623435514137\n", "random.normalvariate(100, 15) = 106.76097954463624\n", "random.paretovariate(2.5) = 2.5492886907343095\n", "random.triangular(1, 10, 5) = 4.341399281789078\n", "random.vonmisesvariate(0, 4) = 6.073381104436034\n", "random.weibullvariate(1, 1.5) = 0.31110331885116\n" ] } ] }, { "cell_type": "code", "source": [ "# seed(a=None): Initializes the random number generator\n", "random.seed(42)\n", "print(\"random.seed(42)\")\n", "\n", "# getstate(): Get the current state of the RNG\n", "state = random.getstate()\n", "print(\"random.getstate() =\", state)\n", "\n", "# setstate(state): Set the state of the RNG\n", "random.setstate(state)\n", "print(\"random.setstate(state)\")\n", "\n", "# getrandbits(k): Returns a random integer with `k` random bits\n", "print(\"random.getrandbits(8) =\", random.getrandbits(8)) # Random integer with 8 bits\n", "\n", "# randbytes(n): Returns `n` random bytes\n", "print(\"random.randbytes(4) =\", random.randbytes(4)) # 4 random bytes" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vfNn3cBV0NLR", "outputId": "9d8a2a2b-fe13-4113-cedb-59c55592dbf2" }, "execution_count": 24, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "random.seed(42)\n", "random.getstate() = (3, (2147483648, 3564348608, 1266698288, 4212342371, 3595291661, 3180588708, 3037210256, 946923017, 2565409715, 2900535780, 924383152, 4180157270, 4230508198, 2039675917, 3755350407, 2362848650, 2818100609, 2097423432, 524478045, 540883378, 281170210, 1485176884, 1493190386, 1773214509, 380915208, 3667698522, 2648371337, 2961234806, 3857480267, 1582950522, 246289694, 3322185604, 1944574775, 302623699, 169865066, 1143540808, 3733177770, 513116636, 1411153081, 3205493053, 768926902, 549624109, 1470655403, 59539609, 3678480009, 3087139671, 1176835859, 2078491503, 2299934332, 1592059249, 1062716176, 2654193596, 3531838733, 2661260596, 3881209635, 2106865768, 4154287292, 2082185616, 2301197011, 2177349827, 3082181756, 1787663536, 3714670796, 3018262113, 1670056238, 1856738750, 99824592, 2279837081, 1414647942, 3416675731, 3458782472, 3997022236, 468762002, 2666158583, 953353270, 1788980658, 3802061067, 407586584, 1844776834, 1906917274, 3154715663, 3028370222, 4156024188, 3996363428, 80495456, 2659800972, 2005649973, 3818358673, 3952623596, 2506862371, 3282302532, 263923435, 3384662671, 3292439172, 3119957588, 1224426111, 899864150, 215262826, 1619647231, 3347694949, 3497868538, 2029552053, 2992804824, 4080010250, 2023513186, 1885979437, 3564622190, 3775424270, 2297810139, 3549449169, 2664856277, 3274801974, 2794883969, 980412666, 2980215653, 2794389321, 2816521934, 1266970739, 542306338, 3646225311, 3598997630, 2111980720, 2949252482, 2489027658, 352815024, 11610683, 1386663624, 2004196796, 1161461546, 1921293780, 2463949525, 1647009713, 3550093655, 2563894064, 3486310554, 1506105865, 243092931, 2659437476, 4200687059, 2284345122, 1974438610, 3591096528, 967119212, 3362401375, 140678365, 311602112, 2361740275, 2139598582, 3632873481, 2762232439, 4156482318, 381637792, 3253346525, 2492118775, 1502434558, 3164497290, 3550998357, 2412448305, 2223955385, 4122879535, 350121793, 1835149778, 2175117867, 989674750, 3178241202, 3553093569, 3470650311, 2829698151, 3209427769, 1779174943, 275388428, 4044574515, 715447260, 3180940440, 4020772289, 1322708567, 3189868792, 4250485633, 716970023, 2307550151, 1074996711, 1217573599, 197006094, 2178394212, 1255233746, 4164251484, 1405608772, 2808160475, 1304736088, 1796071066, 2761748078, 3570739698, 1616118556, 2232868135, 3567541936, 3470600401, 3031621994, 3351764214, 1359785149, 2617497797, 3340028190, 356162828, 2083806068, 2503635608, 4024838996, 2577080371, 2897993505, 3120733934, 905794891, 2506078507, 4211618666, 3777871979, 809751414, 4080874167, 1562977008, 3917373055, 2132779194, 4014249473, 4067327082, 2582869847, 1780081876, 1842619106, 3381761227, 921004274, 1393256920, 1883566732, 2702071861, 865327389, 1622085203, 3021825820, 2687061406, 1748902923, 689023977, 308399650, 2377287978, 1646969411, 1051806316, 4277884230, 2041056290, 101134519, 2032472116, 4112521069, 151202901, 2773743461, 551348559, 3476836808, 510935951, 625057077, 3757450756, 2977698135, 3027776859, 2616998041, 2773430005, 544190486, 2241368212, 1141105829, 1452816309, 4199229235, 3218013033, 4229475816, 1659576351, 3020348754, 1193400518, 3208584597, 1151197733, 2597187966, 503065140, 2421841572, 1437291709, 1909275895, 2872630545, 793588217, 3792934707, 1784451785, 2921385648, 1669902526, 4189978976, 1196986251, 434805516, 1907541826, 2624415034, 1687778718, 650746582, 1949153382, 4148493093, 841300520, 1164202054, 4203468658, 4106300911, 850346789, 1715730760, 3114661489, 2866524548, 1360448945, 3601318775, 1743078223, 2413855408, 1211895622, 325117146, 2721152875, 1284334485, 2446538832, 739014618, 2237045115, 842553465, 2538598293, 746460793, 4010387366, 2002655192, 4193733112, 1194380773, 3918217378, 1447487475, 5659228, 3408847694, 4190318700, 1862549564, 781683719, 1194618118, 755053413, 3436011942, 2885435303, 3081151348, 2017642831, 1053816502, 1086627485, 2157296554, 110650022, 965352898, 1003174194, 1288956241, 4057404871, 2965068465, 2897064481, 2457377317, 1879872545, 358455290, 375086701, 3015902095, 1676249984, 924455526, 2084169389, 1989014644, 1993749926, 2009424973, 2113340508, 3980883273, 2915977458, 203328382, 3020815229, 2415050113, 4103009585, 3700885489, 2916647550, 1523006503, 174302338, 2476909338, 1969322490, 4285741984, 1528449097, 3355315515, 4217241278, 599579127, 2572243673, 3035856735, 1539140489, 1782314913, 4238644287, 1746424142, 1978148312, 2380746849, 184941882, 1106717981, 1720750349, 981701307, 3953154731, 3257809181, 2892339376, 3339778166, 3676936849, 87425948, 3029257381, 2037942523, 3807628706, 2861474706, 1058852346, 1322765211, 2686046342, 2689342655, 2303436168, 2571627181, 1986057734, 1183564308, 2829677523, 1295563975, 503126586, 2025890348, 4179277821, 1735262467, 981331774, 1613447066, 1011606109, 2000062246, 3581448390, 3477731384, 3641307373, 3508544379, 2327233491, 3931944343, 4189052882, 2990416380, 422406169, 202291313, 2531006461, 4277024116, 3815144003, 821314585, 1344175168, 3562834071, 1339615445, 1831545190, 3115548822, 743512780, 4006999448, 3720181735, 1012033521, 919931041, 2628967879, 1151876565, 1268107129, 3674829936, 834977846, 743987006, 3947536548, 3706529695, 4121073678, 2507605742, 1595636918, 2708047833, 2427507331, 3868216331, 3254240010, 2097683411, 3279710596, 3686819053, 1843541720, 1683793619, 3245287285, 3571828776, 3733296431, 3806747478, 1390930605, 3860422228, 114397037, 1931519825, 2770684378, 1556101783, 1436111731, 4031950081, 562876656, 1775895782, 612364620, 1313509772, 4283410242, 3252958463, 2176555836, 3933073367, 3013277102, 1444071961, 3120949516, 2824578890, 325676929, 943677134, 1800649256, 1721927060, 347498719, 1435221321, 2623572981, 1408548470, 4145586315, 2901889237, 1849377952, 1239144551, 3382598266, 2992893897, 3738297588, 611280106, 3897415338, 2370299241, 1772308583, 3697465753, 354508058, 2702360134, 591308331, 3524072501, 976616000, 2563717192, 3078266097, 1376594703, 4209795919, 2454412767, 2712206031, 2963860163, 3734324882, 2248653800, 324872786, 3789837448, 3779000146, 527733939, 2844165793, 576499681, 1618787435, 2638888650, 57511068, 2804627518, 2993670030, 481402236, 2810124845, 1416045214, 1723694191, 1214944572, 3188123783, 1139185907, 3851015362, 1719652470, 1661343029, 3644307578, 3564178709, 1256656955, 46631590, 4231317929, 3098958589, 1834956625, 2206185428, 3695688374, 3647957317, 1064098871, 1739100906, 2579568980, 27974051, 2617466775, 964075233, 907049942, 4164146575, 3377168066, 2524828266, 1083546008, 2992960953, 2260789066, 1543742095, 2843842831, 1375722284, 3574521313, 110842534, 2310998251, 3076511734, 783145600, 1287776608, 3087144146, 305559823, 2356293719, 3228441476, 1678938122, 3775814061, 1620283952, 2512027726, 1031432407, 962295099, 3877418501, 968669928, 304126693, 3711291137, 3847527101, 494066767, 4050229756, 4169448589, 671763915, 1095747781, 4006132710, 394725957, 200521654, 2715998750, 1477567673, 895171901, 3370105999, 2684157455, 4153990023, 3966076501, 2043374409, 144443759, 6764556, 1611650045, 1480956755, 1388276468, 4136518438, 1538041336, 266773992, 1623357516, 2267298390, 3183919402, 1084292424, 2796136160, 2413448816, 2850375199, 3510894040, 2644778623, 3317288284, 3697317540, 1465776787, 1843489446, 1416711171, 744701117, 1286781349, 3748640476, 861982119, 2377742909, 1171768136, 2701877439, 3839724288, 2869791015, 2386067954, 2629214347, 955801623, 3831079317, 624), None)\n", "random.setstate(state)\n", "random.getrandbits(8) = 163\n", "random.randbytes(4) = b'\\x7f1\\x80\\x1c'\n" ] } ] }, { "cell_type": "code", "source": [ "# Dora's Magical Number Adventure\n", "'''\n", "Dora the Explorer is playing a fun number guessing game in a magical forest! In each round:\n", "A secret number is chosen randomly between 1 and 20.\n", "Dora makes a random guess.\n", "If she guesses the number correctly or gets lucky with a miracle, she wins magical gifts!\n", "The number of rounds, gifts, and points she earns are all randomly determined.\n", "\n", "Your task is to simulate this game using Python's random module to:\n", "Decide how many rounds Dora will play.\n", "Generate a random secret number and Dora’s guess in each round.\n", "Randomly decide if a miracle happens.\n", "Reward Dora with random gifts and points if she wins.\n", "Display Dora’s final magical score and total gifts.\n", "\n", "| Random Function | Purpose in the Game |\n", "|----------------------------------|--------------------------------------------------------------------|\n", "| random.randint(a, b) | Picks total rounds (5–10), secret number (1–20), gift count (1–3) |\n", "| random.randrange(a, b) | Dora's guess (1–20) |\n", "| random.triangular(0, 1, 0.7) | Simulates miracle chance (biased toward 0.7) |\n", "| random.betavariate(2, 5) | Used as a multiplier to vary Dora's magical score |\n", "| random.uniform(5, 15) | Determines random points earned when Dora wins |\n", "| random.choices() | Selects multiple gifts from the pool, allowing duplicates |\n", "| random.shuffle() | Randomizes the gift pool before selection |\n", "| random.choice() | Chooses a motivational quote randomly from Dora |\n", "'''" ], "metadata": { "id": "ttZXaend0NNr" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import random\n", "\n", "print(\"🌟 Dora the Explorer's Ultimate Number Adventure Game 🌟\")\n", "\n", "# Dora's magical gift collection\n", "gift_pool = [\n", " \"Magic Map 🗺️\", \"Backpack 🎒\", \"Flying Boots 👢\", \"Invisible Cloak 🕶️\",\n", " \"Talking Compass 🧭\", \"Rainbow Seeds 🌈\", \"Healing Potion 🧪\",\n", " \"Teleport Stone 💠\", \"Golden Banana 🍌\", \"Bouncing Berries 🍓\",\n", " \"Crystal Crown 👑\", \"Enchanted Whistle 🎵\"\n", "]\n", "\n", "# Dora’s motivational quotes\n", "quotes = [\n", " \"We did it, hooray! 🎉\", \"Let’s go explore! 🗺️\", \"Swiper, no swiping! 🙅\",\n", " \"Lo hicimos! 🎊\", \"Adventure awaits! 🚀\", \"Anything is possible! 🌈\"\n", "]\n", "\n", "# Total random number of rounds between 5 to 10\n", "total_rounds = random.randint(5, 10)\n", "print(f\"\\n🌀 Dora is playing {total_rounds} magical rounds!\\n\")\n", "\n", "# Dora’s magical score and gift tracker\n", "dora_score = 0.0\n", "total_gifts = 0\n", "\n", "for round_num in range(1, total_rounds + 1):\n", " print(f\"🔮 Round {round_num}:\")\n", "\n", " # Dora's guess and the secret number\n", " secret_number = random.randint(1, 20)\n", " dora_guess = random.randrange(1, 21)\n", "\n", " print(f\" Dora guessed: {dora_guess}\")\n", " print(f\" Secret number: {secret_number}\")\n", "\n", " # Add randomness to bonus logic\n", " miracle_chance = random.triangular(0, 1, 0.7)\n", " magic_multiplier = random.betavariate(2, 5)\n", "\n", " print(f\" Miracle Chance: {round(miracle_chance, 2)}\")\n", "\n", " if dora_guess == secret_number or miracle_chance > 0.6:\n", " # Shuffle and choose 1–3 random gifts\n", " random.shuffle(gift_pool)\n", " gift_count = random.randint(1, 3)\n", " gifts = random.choices(gift_pool, k=gift_count)\n", " score_add = random.uniform(5, 15) * magic_multiplier\n", " dora_score += score_add\n", " total_gifts += gift_count\n", "\n", " print(f\" 🎉 Dora wins! Received miracle gift(s): {', '.join(gifts)}\")\n", " print(f\" 🌟 {random.choice(quotes)} (+{round(score_add, 2)} points)\")\n", " else:\n", " print(\" 😢 Dora didn’t win this time...\")\n", "\n", " print(f\" ✨ Current magic score: {round(dora_score, 2)}\\n\")\n", "\n", "# Final magical summary\n", "print(\"🌈 Game Over!\")\n", "print(f\"🔢 Total Rounds Played: {total_rounds}\")\n", "print(f\"🌟 Dora's Final Magic Score: {round(dora_score, 2)}\")\n", "print(f\"📦 Total Miracle Gifts Received: {total_gifts} 🎁\")\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q0swBeLM0NP-", "outputId": "e4aaf6ab-12aa-4163-e085-a2b73d47cee5" }, "execution_count": 25, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "🌟 Dora the Explorer's Ultimate Number Adventure Game 🌟\n", "\n", "🌀 Dora is playing 5 magical rounds!\n", "\n", "🔮 Round 1:\n", " Dora guessed: 8\n", " Secret number: 9\n", " Miracle Chance: 0.4\n", " 😢 Dora didn’t win this time...\n", " ✨ Current magic score: 0.0\n", "\n", "🔮 Round 2:\n", " Dora guessed: 8\n", " Secret number: 7\n", " Miracle Chance: 0.59\n", " 😢 Dora didn’t win this time...\n", " ✨ Current magic score: 0.0\n", "\n", "🔮 Round 3:\n", " Dora guessed: 15\n", " Secret number: 8\n", " Miracle Chance: 0.64\n", " 🎉 Dora wins! Received miracle gift(s): Teleport Stone 💠, Rainbow Seeds 🌈, Invisible Cloak 🕶️\n", " 🌟 Anything is possible! 🌈 (+4.22 points)\n", " ✨ Current magic score: 4.22\n", "\n", "🔮 Round 4:\n", " Dora guessed: 12\n", " Secret number: 20\n", " Miracle Chance: 0.64\n", " 🎉 Dora wins! Received miracle gift(s): Teleport Stone 💠, Golden Banana 🍌, Golden Banana 🍌\n", " 🌟 Anything is possible! 🌈 (+2.07 points)\n", " ✨ Current magic score: 6.29\n", "\n", "🔮 Round 5:\n", " Dora guessed: 6\n", " Secret number: 8\n", " Miracle Chance: 0.57\n", " 😢 Dora didn’t win this time...\n", " ✨ Current magic score: 6.29\n", "\n", "🌈 Game Over!\n", "🔢 Total Rounds Played: 5\n", "🌟 Dora's Final Magic Score: 6.29\n", "📦 Total Miracle Gifts Received: 6 🎁\n" ] } ] } ] }