{"id":"modules-import","slug":"import","title":"Importing a module","text":"Python has a few dozen functions built in, such as `print`, `len` and `sum`. Everything else lives in **modules**: files of ready-made functions and values, grouped by theme. Python comes with a large collection of them, called the **standard library**: a module for mathematics, one for random numbers, one for dates, one for reading CSV files, and hundreds more. This part of the course takes a tour of the most useful ones, and ends with modules you write yourself.\n\n### import\n\nA module's contents aren't available until you ask for them. The `import` command loads a module and gives your program access to it. The `math` module has mathematical functions and constants:\n\n```python\nimport math\n\nprint(math.sqrt(49))\nprint(math.pi)\nprint(math.floor(7.8))\nprint(math.ceil(7.2))\n```\n\n```output\n7.0\n3.141592653589793\n7\n8\n```\n\nAfter `import math`, the program reaches everything in the module through the module's name and a dot: `math.sqrt` is the function `sqrt` of the module `math`.\n\n- `sqrt` gives the square root of a number, always as a float.\n- `pi` isn't a function but a value stored in the module, so it has no parentheses.\n- `floor` rounds down to a whole number, and `ceil` (short for ceiling) rounds up.\n\nImports go at the top of a program, one module per line, so a reader sees at a glance which modules the program uses.\n\n### When rounding up is right\n\nRounding up is what you need whenever the leftovers still need a place of their own. Ten people in cars that seat four need three cars, not 2.5:\n\n```python\nimport math\n\npeople = 10\nseats = 4\nprint(\"Cars needed:\", math.ceil(people / seats))\n```\n\n```output\nCars needed: 3\n```\n\n### The module's name is part of the name\n\nWith `import math`, the names in the module are reached only through `math.` in front of them. Writing just `sqrt` doesn't work:\n\n```python\nimport math\n\nprint(sqrt(16))\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 3, in <module>\n    print(sqrt(16))\n          ^^^^\nNameError: name 'sqrt' is not defined\n```\n\nAnd if the `import` line is missing, Python doesn't know the name `math` at all. Its message then suggests the fix itself: `NameError: name 'math' is not defined. Did you forget to import 'math'?`\n","task":"Fill in the blanks so the program imports the `math` module and prints the square root of `3 * 3 + 4 * 4` (the long side of a right triangle whose short sides are 3 and 4).\n","kind":"fill","seed":"[[import]] math\n\nprint(math.[[sqrt]](3 * 3 + 4 * 4))\n","solution":"import math\n\nprint(math.sqrt(3 * 3 + 4 * 4))\n","hints":["The command that loads a module is the same word as in the lesson's examples.","Type `import` into the first blank. The square root function of `math` is `sqrt`, which goes into the second."],"tests":[{"name":"Output","stdin":"","expect":"5.0","hidden":false}],"require":[],"forbid":[],"more":[{"task":"An egg carton holds 12 eggs. Write a program that asks how many eggs there are and prints how many cartons are needed to pack them all. A carton that is only partly full still counts, so round up with `math.ceil`. For example:\n\n```\nEggs: 30\nCartons: 3\n```\n","kind":"code","seed":"import math\n\n# Write your program here.\n","solution":"import math\n\neggs = int(input(\"Eggs: \"))\nprint(\"Cartons:\", math.ceil(eggs / 12))\n","hints":["Read the number with `int(input(\"Eggs: \"))`, and divide it by 12.","30 / 12 is 2.5, and `math.ceil` rounds that up to 3: `math.ceil(eggs / 12)`."],"tests":[{"name":"Test 1","stdin":"30\n","expect":"Eggs: 30\nCartons: 3","hidden":false},{"name":"Test 2","stdin":"24\n","expect":"Eggs: 24\nCartons: 2","hidden":true},{"name":"Test 3","stdin":"1\n","expect":"Eggs: 1\nCartons: 1","hidden":true},{"name":"Test 4","stdin":"0\n","expect":"Eggs: 0\nCartons: 0","hidden":true}],"require":[{"pattern":"math\\.ceil\\s*\\(","message":"Round up with `math.ceil`."}],"forbid":[]},{"task":"Write a function `circle_area(radius)` that returns the area of a circle: pi times the radius squared. Use `math.pi`, not a value of pi you type yourself.\n\n`circle_area(1)` returns `3.141592653589793`.\n","kind":"code","seed":"import math\n\n\ndef circle_area(radius):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(circle_area(1))\n    print(circle_area(2.5))\n","solution":"import math\n\n\ndef circle_area(radius):\n    return math.pi * radius ** 2\n\n\nif __name__ == \"__main__\":\n    print(circle_area(1))\n    print(circle_area(2.5))\n","hints":["The area of a circle is pi times the radius times the radius.","The value of pi is `math.pi`, and the radius squared is `radius ** 2`. Return the result with `return`."],"tests":[{"name":"circle_area(1)","stdin":"","call":"circle_area(1)\n","expect":"3.141592653589793","hidden":false,"returns":true},{"name":"print(f\"{circle_area(2.5):.4f}\")","stdin":"","call":"print(f\"{circle_area(2.5):.4f}\")\n","expect":"19.6350","hidden":true},{"name":"print(f\"{circle_area(10):.4f}\")","stdin":"","call":"print(f\"{circle_area(10):.4f}\")\n","expect":"314.1593","hidden":true},{"name":"circle_area(0)","stdin":"","call":"circle_area(0)\n","expect":"0.0","hidden":true,"returns":true}],"require":[],"forbid":[{"pattern":"\\b3\\.14","message":"Use `math.pi` instead of typing the value of pi."}]}]}