{"id":"modules-from-import","slug":"from-import","title":"Importing names with from","text":"Writing `math.` in front of every name gets long when a program uses the same function many times. The `from` form of import brings chosen names from a module straight into your program:\n\n```python\nfrom math import sqrt\n\nprint(sqrt(81))\nprint(sqrt(2))\n```\n\n```output\n9.0\n1.4142135623730951\n```\n\nNow `sqrt` works on its own, without the prefix. To bring in several names, separate them with commas:\n\n```python\nfrom math import floor, ceil, pi\n\nprint(floor(pi))\nprint(ceil(pi))\nprint(f\"{pi:.2f}\")\n```\n\n```output\n3\n4\n3.14\n```\n\n### Only what you import\n\n`from math import sqrt` brings in `sqrt` and nothing else. The name `math` itself isn't defined, so `math.sqrt` fails, and so does any other name of the module that you didn't list:\n\n```python\nfrom math import sqrt\n\nprint(sqrt(25))\nprint(math.sqrt(36))\n```\n\n```output\n5.0\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 4, in <module>\n    print(math.sqrt(36))\n          ^^^^\nNameError: name 'math' is not defined. Did you forget to import 'math'?\n```\n\nPython's guess is one way to fix it: add `import math`. The other is to write `sqrt(36)`, the name you did import.\n\n### Which form to use\n\nBoth forms reach the same functions, so choose by what reads best:\n\n- `import math` keeps the module's name in every call. `math.floor(x)` tells the reader exactly where `floor` comes from.\n- `from math import floor` is shorter, which is handy when a program uses a function often.\n\nThere's a third form, `from math import *`, which imports every name the module has. It saves typing, but it hides where the names come from, and it can quietly replace a name of your own. The `math` module has a value called `e` (2.718...), so it overwrites a variable `e` of yours:\n\n```python\ne = \"Eve\"\nfrom math import *\n\nprint(e)\n```\n\n```output\n2.718281828459045\n```\n\nNo error, just a wrong value. Use the star form only for quick experiments, if at all.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"from math import floor, ceil\n\nprint(floor(4.9))\nprint(ceil(4.1))\nprint(floor(-2.5))\nprint(ceil(6))\n","solution":"from math import floor, ceil\n\nprint(floor(4.9))\nprint(ceil(4.1))\nprint(floor(-2.5))\nprint(ceil(6))\n","hints":["`floor` always rounds down and `ceil` always rounds up, however close the number is to the next whole number.","Rounding down means toward the smaller number. For a negative number, the smaller one is further from zero.","A whole number is already whole, so `ceil` leaves it as it is."],"tests":[{"name":"Output","stdin":"","expect":"4\n5\n-3\n6","hidden":false}],"require":[],"forbid":[],"lines":["4","5","-3","6"],"more":[{"task":"The program uses three names from the `math` module, but its import line brings in only one of them, so it stops with a `NameError`. Change the import line (keep it a `from` import) so the program works. For example:\n\n```\nArea of the square plot: 50\nSide: 7.07\nFence needed, in whole meters: 29\nRadius of a round plot of the same area: 3.99\n```\n","kind":"code","seed":"from math import sqrt\n\narea = float(input(\"Area of the square plot: \"))\nside = sqrt(area)\nprint(f\"Side: {side:.2f}\")\nprint(f\"Fence needed, in whole meters: {ceil(4 * side)}\")\nprint(f\"Radius of a round plot of the same area: {sqrt(area / pi):.2f}\")\n","solution":"from math import sqrt, ceil, pi\n\narea = float(input(\"Area of the square plot: \"))\nside = sqrt(area)\nprint(f\"Side: {side:.2f}\")\nprint(f\"Fence needed, in whole meters: {ceil(4 * side)}\")\nprint(f\"Radius of a round plot of the same area: {sqrt(area / pi):.2f}\")\n","hints":["Check it and read the `NameError`. Which name doesn't Python know?","The program uses `sqrt`, `ceil` and `pi`. List all three after `import`, separated by commas: `from math import sqrt, ceil, pi`."],"tests":[{"name":"Test 1","stdin":"50\n","expect":"Area of the square plot: 50\nSide: 7.07\nFence needed, in whole meters: 29\nRadius of a round plot of the same area: 3.99","hidden":false},{"name":"Test 2","stdin":"100\n","expect":"Area of the square plot: 100\nSide: 10.00\nFence needed, in whole meters: 40\nRadius of a round plot of the same area: 5.64","hidden":true},{"name":"Test 3","stdin":"2.25\n","expect":"Area of the square plot: 2.25\nSide: 1.50\nFence needed, in whole meters: 6\nRadius of a round plot of the same area: 0.85","hidden":true}],"require":[{"pattern":"from\\s+math\\s+import","message":"Keep the `from math import ...` line and add the missing names to it."}],"forbid":[{"pattern":"\\bmath\\.","message":"Import the names with `from`, so the program can use them without `math.` in front."}]},{"task":"The `math` module also has `gcd(a, b)`, the **greatest common divisor**: the largest whole number that divides both `a` and `b` evenly. `gcd(6, 8)` is 2. Dividing the top and the bottom of a fraction by it gives the fraction in its simplest form: 6/8 is 3/4.\n\nWrite a function `simplify(top, bottom)` that returns the simplest form of the fraction `top/bottom` as a string, such as `\"3/4\"`. Import `gcd` with `from`.\n","kind":"code","seed":"def simplify(top, bottom):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(simplify(6, 8))\n    print(simplify(10, 5))\n","solution":"from math import gcd\n\n\ndef simplify(top, bottom):\n    divisor = gcd(top, bottom)\n    return f\"{top // divisor}/{bottom // divisor}\"\n\n\nif __name__ == \"__main__\":\n    print(simplify(6, 8))\n    print(simplify(10, 5))\n","hints":["Start with `from math import gcd` at the top, then find the divisor with `gcd(top, bottom)`.","Divide both numbers by the divisor with `//`, so they stay whole numbers (`/` would give `3.0`).","Build the string with an f-string: `f\"{top // divisor}/{bottom // divisor}\"`."],"tests":[{"name":"simplify(6, 8)","stdin":"","call":"simplify(6, 8)\n","expect":"'3/4'","hidden":false,"returns":true},{"name":"simplify(10, 5)","stdin":"","call":"simplify(10, 5)\n","expect":"'2/1'","hidden":true,"returns":true},{"name":"simplify(21, 14)","stdin":"","call":"simplify(21, 14)\n","expect":"'3/2'","hidden":true,"returns":true},{"name":"simplify(7, 9)","stdin":"","call":"simplify(7, 9)\n","expect":"'7/9'","hidden":true,"returns":true}],"require":[{"pattern":"from\\s+math\\s+import[^\\n]*\\bgcd\\b","message":"Import `gcd` with `from math import gcd`."}],"forbid":[]}]}