You import the math module by putting import math at the top of your script, then calling its functions with a math. prefix, for example math.sqrt(16) returns 4.0. That single line gives you Python's built-in library of mathematical functions: square roots, trigonometry, logarithms, rounding helpers and constants such as pi and e. It is part of the standard library, so there is nothing to install and no separate package to download, as long as you already have Python itself working on your machine.

What You'll Need

  • A working Python installation (version 3.x is the norm everywhere now), which you can confirm by running python --version or python3 --version in a terminal.
  • Somewhere to write and run code: a script file with .py extension, or any REPL such as IDLE, a terminal session, or a notebook.
  • A basic understanding of calling functions and of how modules are referenced, though the steps below cover the essentials.
  • No internet connection is required: math ships with Python, so it works offline and on locked-down machines.

Step-by-Step

  1. Open your file or interpreter. Create a new file called something like example.py, or launch a Python REPL by typing python or python3 in a terminal. Both work identically for importing modules; the REPL just runs each line as you press Enter.
  2. Put the import at the top. On the first line of the file, write import math. By convention imports go at the top of the file, before any other code, so anyone reading the script can see its dependencies immediately. Python does not require that position, but it is the standard style and worth keeping.
  3. Run the file or press Enter in the REPL. If nothing appears, the import succeeded. Python only raises an error at import time if the module name is wrong or the module is missing from the installation.
  4. Call a function with the math. prefix. Add a line such as print(math.sqrt(25)) and run it. You should see 5.0. The prefix is not optional with a plain import math: writing sqrt(25) on its own raises NameError: name 'sqrt' is not defined.
  5. Try the constants. math.pi gives you pi to the precision Python stores, and math.e gives Euler's number. Note there are no parentheses after constants, because they are values, not functions:

Putting the first few steps together, a working script looks like this:

import math

radius = 3
area = math.pi * radius ** 2
hypotenuse = math.sqrt(3 ** 2 + 4 ** 2)

print(area)
print(hypotenuse)

Running it prints the area of a circle with radius 3 and then 5.0.

  1. Decide whether to import the whole module or specific names. import math keeps every function namespaced under math, which is the clearest option and what most code uses. from math import sqrt, pi pulls individual names into your script so you can write sqrt(25) directly. Use the second form sparingly, because bare names like pi can collide with your own variables and make code harder to follow.
  2. Give the module a shorter alias if you prefer. import math as m lets you write m.sqrt(25). This is legal Python but unusual for the math module; short aliases matter more for long third-party package names.
  3. Look up what is available. In a REPL, run dir(math) to print a list of every name the module provides. For a description of one function, use help(math.sqrt) or help(math) for the whole module. This is the fastest way to check whether a particular function exists rather than guessing.
  4. Pick the right function for the job. Common ones worth knowing: math.sqrt for square roots, math.pow for powers, math.floor and math.ceil for rounding down and up, math.factorial for factorials, math.log for natural logarithms, math.log10 for base-10 logarithms, and math.sin, math.cos, math.tan for trigonometry. The trigonometric functions take angles in radians, not degrees, so convert first with math.radians(45).
  5. Handle the errors the functions can raise. Functions check their inputs. math.sqrt(-1) raises ValueError: math domain error, and math.log(0) does the same. If your input comes from user data or a calculation, validate it before calling, or wrap the call in a try/except ValueError block and supply a sensible fallback.
  6. Keep working in the REPL or rerun the script. When you change a script, save it and run it again. Inside a REPL, a module already imported stays as it was until you restart the session, which matters if you are debugging.

Common Mistakes

  • Forgetting the prefix. Writing sqrt(16) after import math fails with a NameError. Either use math.sqrt(16) or import the name explicitly.
  • Naming your own file math.py. Python searches the folder containing your script before the standard library, so a file with that name shadows the real module and produces confusing errors about missing functions. Rename your file and delete any leftover math.pyc in the same folder.
  • Using degrees with trigonometric functions. math.sin(90) does not return 1.0; it treats 90 as radians. Convert with math.radians or math.degrees as appropriate.
  • Expecting integers. math.sqrt(16) returns 4.0, a float, not 4. Most math functions return floats, which is fine for printing but can matter for exact comparisons and for indexing.
  • Calling constants as functions. math.pi() raises TypeError because pi is a value. Drop the parentheses.
  • Using ^ for powers. The caret is bitwise XOR in Python, not exponentiation. Use ** or math.pow.

Tips

  • Reach for math when you want plain numbers back. When you need arrays for many values at once, NumPy is the usual choice; math works one value at a time.
  • math.isclose(a, b) is a safer way to compare floating-point results than ==, because rounding makes exact equality unreliable.
  • Some names also exist as built-ins, such as round, abs and min, so you do not need the module for those. math.floor and math.ceil have no built-in equivalent.
  • For very large integers, Python's own ** operator handles arbitrary sizes, while math.pow converts to floats and can overflow.
  • Keep the import list alphabetical and grouped at the top; math sits with the standard-library imports, before any installed packages.

Troubleshooting

  • ModuleNotFoundError or ImportError on import math: this points to a broken or unusual Python installation rather than your code, since math is part of the standard library. Check that you are running the interpreter you expect (some systems have several), and look for a file or folder in your working directory named math.py or math that is shadowing it. If the installation itself is damaged, reinstall Python from the official source.
  • NameError for a function: you either called it without the math. prefix or imported it with a different alias. Check how you imported at the top of the file.
  • ValueError: math domain error: the function received a value it cannot handle, such as a negative square root or the logarithm of zero. Inspect the input just before the call, for example by printing it.
  • TypeError about the number of arguments: you have called a function with the wrong count, or treated a constant as a function. Call help(math.name) in the REPL to see the expected signature.
  • The result looks wrong but nothing errors: check the unit. Degrees-versus-radians and integer-versus-float confusion account for most surprising results.
  • Changes to a script do not appear: you may be running a different file path or an open REPL session that still holds the old code. Restart the interpreter and run the saved file again.

Frequently Asked Questions

Do I need to install anything to use math in Python?

No. math is part of the Python standard library and comes with every normal installation. It works offline and needs no package manager.

What is the difference between import math and from math import sqrt?

import math loads the module and you reach names through the math. prefix. from math import sqrt copies just that name into your script so you can call it directly. The first is clearer in larger programs.

Why does math.sqrt return a decimal instead of a whole number?

Most math functions return floats by design. If you need an integer, convert explicitly, for example with int(math.sqrt(16)), but be aware that conversion truncates rather than rounds.

Does the math module work with complex numbers?

No. Functions like math.sqrt raise an error for negative inputs. Use the separate cmath module when you need complex-number results.

How do I find all the functions in the math module?

Run dir(math) in a REPL to list the names, or help(math) to read a summary of each one. The official Python documentation also lists them all.

Can I rename the module when I import it?

Yes, with import math as m, after which you write m.pi and m.sqrt(). It is allowed but uncommon for a short name like math.