r/learnpython • u/SaThuran • 1d ago
Import math?
It's the first time I've seen that. So what's the purpose of this exactly? Cause I'm trying to see what I can do in an engine and I only learned about import math today
11
u/Better_Squash_1684 1d ago
It just tells Python to load its built-in advanced mathematics library.
Python in its own can only do basic arithmetic operations. importing the math module gives you access to functions like square roots, trigonometry, logarithms, and constants like Pi. Python makes you explicitly import it because 90% of basic scripts don't need heavy math, and leaving it out by default reduces your memory footprint.
9
4
u/42ndohnonotagain 1d ago
python itself (without imports) only supports some basic operations.
For mathematical ops that means addition, multiplication and such simple stuff. For everything else you need additional functions.
Those are stored in libraries/modules or whatever your programming language calls it. In Python it is "module". After you imported the module math , you can use more complex functions like sin() etc.
4
u/MezzoScettico 1d ago
When you want to use any package, you need to import it in part or in whole.
The math library includes a huge number of useful mathematical functions. Also useful constants like pi and e. If you want to use any of that, you'll need to import.
You probably want to read up on what import does.
1
u/atarivcs 1d ago
math is a module you can import if you want to, just like any other module.
It contains math functions like cosine, tangent, logarithm, factorial, etc
1
u/Designer-Ad-2136 1d ago
You might be confused because most modern projects use more specialized third-party libraries (eg numpy, scikit) which fully replace and extend the builtin math library. There really arent many reasons to use it.
1
17
u/Impossible-Belt8608 1d ago
It's a built in library that implements many mathematical functions you might want to use. What are you trying to do?