KnowledgeBoat Logo
|

Computer Science

How are the following two statements different from each other?

(a) import math

(b) from math import*

Python Modules

2 Likes

Answer

(a) import math — This statement is used to import entire module math. All the functions are imported in a new namespace setup with same name as that of the module math. To access one of the functions, we have to specify the name of the module and the name of the function, separated by a dot. This format is called dot notation.

For example,

import math
print(math.sqrt(16))

(b) from math import * — This statement is used to import all the items from module math in the current namespace. We can use all defined functions, variables etc from math module, without having to prefix module's name to imported item name.

For example,

from math import *
print(sqrt(16))

Answered By

1 Like


Related Questions