I'm currently learning Python and I'm at the point in the book about using the Math library. I looked on the Python website and noticed the library was a bit scarce and am writing some more useful functions. For example, I went ahead and wrote a function to take coefficients and return the roots of the equation. Essentially a quadratic formula function. I'm wondering if it's possible to add this to the python Math library. If it isn't, how do I save it such that I can use that function in other Python programs I write just by calling it?

  • 1
    There is an answer below by Michael0x2a that covers the fine points, but the simple version is: don't do that. If I am reading your code and see a call to math.polynomial_roots() me and my interpreter will become confused. That's a Bad Thing.
    – msw
    Commented May 21, 2015 at 17:36

1 Answer 1


The math module is a builtin, so short of modifying the Python interpreter itself, I don't think you can modify it. However, writing a module is definitely something you can do.

If you structure your files like this:


...you could simply do import mymath inside myprogram.py, and use any functions or classes inside mymath.py as normal.

So if mymath.py looks like this:

def quadratic(a, b, c):
    # blah blah blah

You could do the below inside myprogram.py

import mymath

print mymath.quadratic(1, 2, 3)

If you want the module you've written available for any program, you could either copy-and-paste it into the folder of any project you're working on, or add it to your PATH. (For example, you could include mymath.py inside the site-packages folder, which is located at C:\Python27\Lib\site-packages on my computer). Once you do that, you should be able to do import mymath without ever having to copy-and-paste anything.

As a side-note, numpy has a pretty comprehensive set of math and science related functions that you could check out. It's pretty much the de-facto standard for numerical computation in Python, afaik.

  • 2
    Exactly the answer I would've written. Python is designed to be modded, quite literally. Commented Aug 1, 2012 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.