What is the difference between import math and from math import *
What is the difference between
import math and from math import *
they both does the same thing don't they?
Hi Naga Say!
Thanks for reaching out!
In general, when you use import math
, you're importing the entire math module. To use a function or a constant from the math module, you need to prefix it with .math
, like math.sqrt(49)
.
On the other hand, from math import *
imports all functions, constants, and classes from the math module directly into your current namespace. This means you can use them without the math
prefix, like:
sqrt(49)
However, as mentioned in the lesson, this practice is generally discouraged because it can lead to conflicts with other functions or variables with the same names in your code, making it less clear where each function comes from.
Hope this helps.
Best,
Ivan