0

I was exploring through the maths function in python 3.0. I have written the following code for the following question. I am not sure if the answer is right. Write a code segment that only imports sqrt from the math module. The code then prints out the square root for 81 and 9 respectively using the sqrt function math.sqrt(81)

from math import.sqrt(81)
print(sqrt(81))

Please help me to rectify if not please post the solution for the question thanks

Shaido
  • 27,497
  • 23
  • 70
  • 73
HITJUNG
  • 1
  • 1

4 Answers4

2

You can either import the whole package as import math and do math.sqrt(81). But since you only need sqrt() function from math, you can just import that from math import sqrt

from math import sqrt
print('Square root of 81 is: ',sqrt(81))
Nandu Raj
  • 2,072
  • 9
  • 20
0

To import a specific function from a module, do something like

from math import sqrt

Then you can call the function as

print(sqrt(81))

Here is an informative discussion on this topic for your reference.

Jake Tae
  • 1,681
  • 1
  • 8
  • 11
0

It's almost right:

The way you import a module (which you can think of as a bundle of a lot of functions) is the import function e.g if you want to import the entire math module you write import math. Now if you want to use sqrt from Math you can do math.sqrt(81) but it might be annoying to write math all the time, thus you can just import the function by

from math import sqrt

and that way you can call it without writing math first such as

print(sqrt(81)).

EDIT:

Changed Math to math

CutePoison
  • 4,679
  • 5
  • 28
  • 63
0

Python have multiple simple ways to do,

from math import sqrt
print(sqrt(81))

If you don't want to use mat library, simply

print(81**.5) # used for square root

print(9**2)  # Used for squre the number
Wickkiey
  • 4,446
  • 2
  • 39
  • 46