Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Importing a Module

The functions you've used so far, such as print(), len() and sorted(), are in every Python program from the start. Python has many more, but most of them wait in modules until a program asks for them. A module is a file of Python code with functions and values for one subject. Python comes with a lot of modules, together called its standard library: math for mathematics, random for random numbers, statistics for averages, and many more.

To use a module, you import it: write the keyword import and the module's name. Then you reach what's in the module with its name, a dot and the name of the function:

import math

print(math.sqrt(16))
print(math.sqrt(2))
print(math.pi)
4.0
1.4142135623730951
3.141592653589793

math.sqrt() gives the square root of a number: the number that, multiplied by itself, gives the original one. 4 times 4 is 16, so the square root of 16 is 4. The result is always a decimal number, 4.0. math.pi isn't a function but a value stored in the module: the number π (pi), which you need for circles. That's why it has no parentheses.

The dot works as it does with methods: math.sqrt means “the function sqrt from the module math”. With the module's name in front, anyone who reads your code sees where each function comes from.

Imports go at the top of the program, each module once, however many times you use it. Then whoever reads the program sees at once which modules it needs.

Larger programs are split into several files, and each file is a module too: a program imports its own file shop.py with import shop, just as it imports math. In these exercises, a program is always one file, so you'll use the modules that come with Python.

Watch out for these mistakes:

  • A missing import. The program doesn't know the name math, and the error message even says what you forgot: NameError: name 'math' is not defined. Did you forget to import 'math'?
  • A typo in the module's name. The program ends right at the import: import maths gives ModuleNotFoundError: No module named 'maths'.
  • A typo in the function's name. math.Sqrt(16) gives AttributeError: module 'math' has no attribute 'Sqrt'. Did you mean: 'sqrt'? The module has nothing called Sqrt (capital letters matter here too), and the message suggests the right name.

Instructions

  1. 1

    The program ends with a NameError. Read the error message and add the line that is missing at the top of the program. The program then prints Side: 12.0: each side of the playground is 12 meters long.

  2. 2

    Below, print the circumference of the sandpit, the length of its edge: 2 * math.pi * radius, with the label Circumference:. The output should be Circumference: 12.566370614359172. Calculate it from radius: the exercise also tries your program with another radius.

Back

1/8

  1. 1. Importing a Module
  2. 2. Rounding
  3. 3. Quiz: Rounding
  4. 4. Importing with from
  5. 5. Quiz: Imports
  6. 6. Random Numbers
  7. 7. Average and Median
  8. 8. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.