quadrada() retorna la funció arrel quadrada de qualsevol nombre . És una funció integrada en el llenguatge de programació Python.
En aquest article, aprendrem més sobre el programa Python per trobar l'arrel quadrada.
Funció sqrt().
Podem calcular l'arrel quadrada en Python mitjançant la funció sqrt() del mòdul matemàtic. En aquest exemple, estem calculant l'arrel quadrada de diferents nombres mitjançant la funció sqrt().
Python 3
# Python3 program to demonstrate the> # sqrt() method> # import the math module> import> math> # print the square root of 0> print> (math.sqrt(> 0> ))> # print the square root of 4> print> (math.sqrt(> 4> ))> # print the square root of 3.5> print> (math.sqrt(> 3.5> ))> |
>
>Sortida
edat de sara ali khan
0.0 2.0 1.8708286933869707>
Definició de la funció math.sqrt().
La funció sqrt() a Python és una funció integrada i està present a la biblioteca de matemàtiques.
Podeu utilitzar la funció sqrt després d'importar la biblioteca de matemàtiques.
import math>
La funció sqrt() només pren un valor superior o igual a 0.
math.sqrt() Sintaxi del mètode
math.sqrt(x)
Paràmetre
x: és qualsevol nombre tal que x>=0
Devolucions: I t retorna l'arrel quadrada del nombre passat al paràmetre.
Exemples de funcions sqrt().
Vegem alguns usos diferents de la funció math.sqrt().
Exemple 1: comproveu si és primer o no
En aquest exemple, ens dóna un nombre i estem comprovant si un nombre és primer o no. Aquí, executeu un bucle de 2 a sqrt(n) i comproveu si algun nombre del rang (2-sqrt(n)) divideix n.
Python 3
# Python program for practical application of sqrt() function> # import math module> import> math> # function to check if prime or not> def> check(n):> > if> n> => => 1> :> > return> False> > > # from 1 to sqrt(n)> > for> x> in> range> (> 2> , (> int> )(math.sqrt(n))> +> 1> ):> > if> n> %> x> => => 0> :> > return> False> > return> True> # driver code> n> => 23> if> check(n):> > print> (> 'prime'> )> else> :> > print> (> 'not prime'> )> |
>
exemples d'arbres binaris
>Sortida
prime>
Exemple 2: Trobar la hipotenusa d'un triangle
En aquest exemple, estem utilitzant la funció sqrt() per trobar la hipotenusa d'un triangle.
Python 3
a> => 10> b> => 23> import> math> # importing the math module> c> => math.sqrt(a> *> *> 2> +> b> *> *> 2> )> print> (> 'The value for the hypotenuse would be '> , c)> |
>
>Sortida
The value for the hypotenuse would be 25.079872407968907>
Error de funció sqrt().
Quan x<0 no s'executa a causa d'un error d'execució. En aquest exemple, podem veure que no podem calcular l'arrel quadrada de Python si el nombre és menor que zero.
Python 3
# Python3 program to demonstrate the error in> # sqrt() method> # import the math module> import> math> # print the error when x<0> print> (math.sqrt(> -> 1> ))> |
>
exemples de programació de Python
>
Sortida
Traceback (most recent call last): File '/home/67438f8df14f0e41df1b55c6c21499ef.py', line 8, in print(math.sqrt(-1)) ValueError: math domain error>
Es tractava de la funció sqrt() que s'utilitza per trobar arrel quadrada a Python. Trobar arrel quadrada a Python és molt fàcil amb aquesta funció integrada.
Per obtenir més funcions de la biblioteca matemàtica: Mòdul matemàtic de Python