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
Define a function which accepts n as an argument and prints Fibonacci series till n.
Consider the temperature given below for the month of June in North India. Calculate the average temperature and median value. This temperature gets dipped with a variation of 20°C in the month of December. Write a Python program to calculate the changed median value and average temperature.
Location Temperature (in °C) Delhi 41 Shimla 32 Chandigarh 43 Rohtak 40 Srinagar 28 Sri Ganganagar 45 Create a menu-driven program using user-defined functions to implement a calculator that performs the following:
(a) Basic arithmetic operations (+, −, *, /)
(b) log10(x), sin(x), cos(x)
Write a program that contains user-defined functions to calculate area, perimeter or surface area whichever is applicable, for various shapes like square, rectangle, triangle, circle and cylinder. The user-defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use appropriate functions.