When you have the Python interpreter installed on your computer, it comes with a lot of common functions already written so that you don’t have to do everything from scratch (unless you wanted to…). These functions are grouped together based on the things they have in common into “modules”.
import statement does is tell Python that you want to use some of this pre-built code, and which bits of it you want. There are a lot of “built-in” functions, like
print, which Python can use without needing to specifically import them, but some more specialised functions - like a lot of the mathematical functions - need to be imported first before you can use them. (In this example, from the
A “generic import” just means that you’re importing the entire module. So,
import math would import every function from the
math module regardless of whether you need or have used it.
Generally, it’s better to only import the specific functions you need from the module(s) that defines them. So, if say you needed to do some trigonometry in your program, you could import the sine function by using
from math import sin and this will only import the
sin function into your program. If we are doing some serious trigonometry, we’re gonna need the other functions too - so we can import multiple functions with one statement, like this:
from math import sinh, cosh, tanh.
Does that help?