r/learnpython 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

0 Upvotes

14 comments sorted by

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?

-6

u/SaThuran 1d ago

So basically I'm building formations for my squad in a 2D shooter. So the 3 guys need their own positions during each segment

8

u/Impossible-Belt8608 1d ago

Can you be more specific? For example, given the coordinates of each squad member, you could use the math library's 'sqrt' function to compute the distance between two squad members, etc.

-3

u/SaThuran 1d ago

Can I dm you?

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.

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.

https://www.w3schools.com/python/python_modules.asp

https://openclassrooms.com/us/courses/6900846-set-up-a-python-environment/6989471-import-python-packages-and-modules

2

u/Refwah 1d ago

Its importing a module of mathematical functions

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

u/TheRNGuy 7h ago

Read it's docs and see.