"from math import sqrt" works, but "imported math" doesn't work. What is the reason?
I'm new to programming just learning python.
I am using Komodo Edit 9.0 for writing codes. So when I write "from math import sqrt" I can use the "sqrt" function without any problem. But if I only write "import math" then the "sqrt" function of this module does not work. What is the reason for this? Can I fix this?
source to share
You have two options:
import math
math.sqrt()
imports the module math
into its own namespace. This means that function names must be prefixed math
. This is a good practice because it avoids conflicts and will not overwrite a function that has already been imported into the current namespace.
As an alternative:
from math import *
sqrt()
will import everything from the module math
into the current namespace. This can be problematic .
source to share