logo

Kuinka kirjoittaa neliöjuuri Pythonissa?

Pythonilla on ennalta määritetty sqrt() funktio, joka palauttaa luvun neliöjuuren. Se määrittää arvon neliöjuuren, joka kertoo itsensä luvun. Funktiota sqrt() ei käytetä suoraan tietyn luvun neliöjuuren etsimiseen, joten meidän on käytettävä matematiikka moduuli sqrt()-funktion kutsumiseksi Python .

Esimerkiksi luvun 144 neliöjuuri on 12.

Katsotaanpa nyt neliöjuurifunktion syntaksia tietyn luvun neliöjuuren löytämiseksi Pythonissa:

Syntaksi:

 math.sqrt(x) 

Parametrit:

x : Se on numero. jossa luvun tulee olla suurempi kuin 0 ja se voi olla desimaali tai kokonaisluku.

Palata:

Tulos on neliöjuuren arvo.

Huomautus:

  • Sqrt()-menetelmän tulos on liukuluku.
  • Jos annettu syöte on negatiivinen luku, tulos on ValueError. ValueError palautetaan, koska negatiivisen luvun neliöjuuriarvoa ei pidetä reaalilukuna.
  • Jos syöte on kaikkea muuta kuin numero, sqrt()-funktio palauttaa NaN:n.

Esimerkki:

Esimerkki sqrt()-funktion käytöstä Pythonissa.

Koodi

 import math x = 16 y = math.sqrt(x) print(y) 

Lähtö:

 4.0 

Kuinka kirjoittaa neliöjuuri Pythonissa

1. Käyttämällä math.sqrt()-metodia

Funktio sqrt() on sisäänrakennettu funktio, joka palauttaa minkä tahansa luvun neliöjuuren. Seuraavassa on vaiheet luvun neliöjuuren löytämiseksi.

  1. Käynnistä ohjelma
  2. Määritä mikä tahansa luku, jonka neliöjuuri löytyy.
  3. Kutsu sqrt() funktio ja välitä vaiheessa 2 määrittämäsi arvo ja tallenna tulos muuttujaan.
  4. Tulosta neliöjuuri.
  5. Lopeta ohjelma.

Python math.sqrt() -metodi Esimerkki 1

Python-esimerkkiohjelma, joka etsii annetun kokonaisluvun neliöjuuren.

Koodi

 # import math module import math # define the integer value to the variable num1 num1 = 36 # use math.sqrt() function and pass the variable. result = math.sqrt(num1) # to print the square root of a given number n print(' Square root of number 36 is : ', result) # define the value num2 = 625 result = math.sqrt(num2) print(' Square root of value 625 is : ', result) # define the value num3 = 144 result = math.sqrt(num3) print(' Square root of number 144 is : ', result) # define the value num4 = 64 result = math.sqrt(num4) print(' Square root of number 64 is : ', result) 

Lähtö:

 Square root of number 36 is : 6.0 Square root of number 625 is : 25.0 Square root of number 144 is : 12.0 Square root of number 64 is : 8.0 

Python math.sqrt() -metodi Esimerkki 2

Luodaan python-ohjelma, joka löytää desimaalilukujen neliöjuuren.

Koodi

 # Import the math module import math # Calculate the square root of decimal numbers print(' The square root of 4.5 is ', math.sqrt(4.5)) # Pass the decimal number print(' The square root of 627 is ', math.sqrt(627)) # Pass the decimal number print(' The square root of 6.25 is ', math.sqrt(6.25)) # Pass the decimal number # Calculate the square root of 0 print(' The Square root of 0 is ', math.sqrt(0)) # Pass number as 0 

Lähtö:

 The Square root of 4.5 is 2.1213203435596424 The Square root of 627 is 25.039968051096054 The Square root of 6.25 is 2.5 The Square root of 0 is 0.0 

Python math.sqrt() -metodi Esimerkki 3

Seuraavassa ohjelmassa olemme lukeneet käyttäjältä luvun ja löytäneet neliöjuuren.

Koodi

 # import math module import math a = int(input(' Enter a number to get the Square root ')) # Use math.sqrt() function and pass the variable a. result = math.sqrt(a) print(' Square root of the number is ', result) 

Lähtö:

 Enter a number to get the Square root: 25 Square root of the number is: 5.0 

1. Math.pow()-funktion käyttäminen

Pow() on sisäänrakennettu funktio, jota käytetään Pythonissa palauttamaan luvun potenssi. Siinä on kaksi parametria. Ensimmäinen parametri määrittää luvun ja toinen parametri määrittelee tehonkorotuksen tähän numeroon.

Python math.pow() -menetelmä Esimerkki

Katsotaanpa esimerkkiohjelma math.pow()-funktiolle:

Koodi

 # import the math module import math # take an input from the user num = float(input(' Enter the number : ')) # Use the math.pow() function and pass the value and 0.5 (which is equal to ?) as an parameters SquareRoot = math.pow( num, 0.5 ) print(' The Square Root of the given number {0} = {1}' .format( num, SquareRoot )) 

Lähtö:

 Enter the number :628 The Square Root of the given number 628.0 = 25.059928172283335 

3. Numpy-moduulin käyttö

NumPy-moduuli on myös mahdollisuus löytää neliöjuuri pythonista.

Python Numpy esimerkki

Katsotaanpa esimerkkiohjelmaa taulukon tietyn numeroluettelon neliöjuuren löytämiseksi.

Koodi

 # import NumPy module import numpy as np # define an array of numbers array_nums = np.array([ 1, 4, 9, 16, 25 ]) # use np.sqrt() function and pass the array result = np.sqrt(array_nums) print(' Square roots of the given array are: ', result) 

Lähtö:

 Square roots of the given array are: [ 1. 2. 3. 4. 5. ] 

4. Käytä ** Operaattoria

Voimme myös käyttää eksponenttioperaattoria löytääksemme luvun neliöjuuren. Operaattoria voidaan käyttää kahden operandin välissä. Esimerkiksi x**y. Se tarkoittaa, että vasen operandi on korotettu oikean potenssiin.

Seuraavassa on vaiheet luvun neliöjuuren löytämiseksi.

Vaihe 1. Määritä funktio ja välitä arvo argumenttina.

vlc lataa videoita youtubesta

Vaihe 2. Jos määritetty luku on pienempi kuin 0 tai negatiivinen, se ei palauta mitään.

Vaihe 3. Käytä eksponentiaalista **-merkkiä löytääksesi luvun potenssin.

Vaihe 4. Ota numeroarvo käyttäjältä.

Vaihe 5. Kutsu funktio ja tallenna sen tulos muuttujaan.

Vaihe 6. Näytä luvun neliöjuuri Pythonissa.

Vaihe 7. Poistu ohjelmasta.

Python ** Operaattoriesimerkki 1

Toteutetaan yllä olevat vaiheet Python-ohjelmassa ja lasketaan luvun neliöjuuri.

Koodi

 # import the math package or module import math # define the sqrt_fun() and pass the num as an argument def sqrt_fun(num): if num <0: 0 # if num is less than or negative, it returns nothing return else: ** 0.5 use the exponent operator (' enter a numeric value: ') ) take an input from user call sqrt_fun() to find result res="sqrt_fun(num)" print square root of variable print(' {0}="{1}" '.format(num, res)) < pre> <p> <strong>Output:</strong> </p> <pre> Enter a numeric value: 256 Square Root of the 256 = 16.0 </pre> <p> <strong>Explanation:</strong> </p> <p>As we can see in the above example, first we take an input (number) from the user and then use the exponent ** operator to find out the power of a number. Where 0.5 is equal to &#x221A; (root symbol) to raise the power of a given number. At last, the code prints the value of the num and the comparing square root esteem utilizing the format() function. On the off chance that the client inputs a negative number, the capability will not return anything and the result will be clear.</p> <h3>Python ** Operator Example 2</h3> <p>Let&apos;s create a Python program that finds the square root of between the specified range. In the following program, we have found the square root of all the number between 0 to 30.</p> <p> <strong>Code</strong> </p> <pre> # Import math module import math # Iterate through numbers from 0 to 29 and print their square roots for i in range(30): # Use the format() method to insert the values of i and its square root into the string print(&apos; Square root of a number {0} = {1} &apos;.format( i, math.sqrt(i))) </pre> <p> <strong>Output:</strong> </p> <pre> Square root of a number 0 = 0.0 Square root of a number 1 = 1.0 Square root of a number 2 = 1.4142135623730951 Square root of a number 3 = 1.7320508075688772 Square root of a number 4 = 2.0 Square root of a number 5 = 2.23606797749979 Square root of a number 6 = 2.449489742783178 Square root of a number 7 = 2.6457513110645907 Square root of a number 8 = 2.8284271247461903 Square root of a number 9 = 3.0 Square root of a number 10 = 3.1622776601683795 Square root of a number 11 = 3.3166247903554 Square root of a number 12 = 3.4641016151377544 Square root of a number 13 = 3.605551275463989 Square root of a number 14 = 3.7416573867739413 Square root of a number 15 = 3.872983346207417 Square root of a number 16 = 4.0 Square root of a number 17 = 4.123105625617661 Square root of a number 18 = 4.242640687119285 Square root of a number 19 = 4.358898943540674 Square root of a number 20 = 4.47213595499958 Square root of a number 21 = 4.58257569495584 Square root of a number 22 = 4.69041575982343 Square root of a number 23 = 4.795831523312719 Square root of a number 24 = 4.898979485566356 Square root of a number 25 = 5.0 Square root of a number 26 = 5.0990195135927845 Square root of a number 27 = 5.196152422706632 Square root of a number 28 = 5.291502622129181 Square root of a number 29 = 5.385164807134504 Square root of a number 30 = 5.477225575051661 </pre> <h2>Conclusion:</h2> <p>All in all, there are multiple ways of tracking down the square root value of a given number in Python. We can utilize the number related math module, the ** operator, the pow() method, or the NumPy module, contingent upon our prerequisites.</p> <hr></0:>

Selitys:

Kuten yllä olevassa esimerkissä nähdään, otamme ensin syötteen (luvun) käyttäjältä ja käytämme sitten eksponentti ** -operaattoria selvittääksemme luvun potenssin. Missä 0,5 on yhtä suuri kuin √ (juurisymboli) nostaaksesi tietyn luvun tehoa. Lopulta koodi tulostaa numeron arvon ja vertailevan neliöjuuren arvosanan käyttämällä format()-funktiota. Siinä tapauksessa, että asiakas syöttää negatiivisen luvun, ominaisuus ei palauta mitään ja tulos on selvä.

Python ** Operaattoriesimerkki 2

Luodaan Python-ohjelma, joka löytää neliöjuuren määritetyn alueen väliltä. Seuraavassa ohjelmassa olemme löytäneet neliöjuuren kaikista numeroista välillä 0-30.

Koodi

 # Import math module import math # Iterate through numbers from 0 to 29 and print their square roots for i in range(30): # Use the format() method to insert the values of i and its square root into the string print(&apos; Square root of a number {0} = {1} &apos;.format( i, math.sqrt(i))) 

Lähtö:

 Square root of a number 0 = 0.0 Square root of a number 1 = 1.0 Square root of a number 2 = 1.4142135623730951 Square root of a number 3 = 1.7320508075688772 Square root of a number 4 = 2.0 Square root of a number 5 = 2.23606797749979 Square root of a number 6 = 2.449489742783178 Square root of a number 7 = 2.6457513110645907 Square root of a number 8 = 2.8284271247461903 Square root of a number 9 = 3.0 Square root of a number 10 = 3.1622776601683795 Square root of a number 11 = 3.3166247903554 Square root of a number 12 = 3.4641016151377544 Square root of a number 13 = 3.605551275463989 Square root of a number 14 = 3.7416573867739413 Square root of a number 15 = 3.872983346207417 Square root of a number 16 = 4.0 Square root of a number 17 = 4.123105625617661 Square root of a number 18 = 4.242640687119285 Square root of a number 19 = 4.358898943540674 Square root of a number 20 = 4.47213595499958 Square root of a number 21 = 4.58257569495584 Square root of a number 22 = 4.69041575982343 Square root of a number 23 = 4.795831523312719 Square root of a number 24 = 4.898979485566356 Square root of a number 25 = 5.0 Square root of a number 26 = 5.0990195135927845 Square root of a number 27 = 5.196152422706632 Square root of a number 28 = 5.291502622129181 Square root of a number 29 = 5.385164807134504 Square root of a number 30 = 5.477225575051661 

Johtopäätös:

Kaiken kaikkiaan Pythonissa on useita tapoja jäljittää tietyn luvun neliöjuuriarvo. Voimme käyttää numeroon liittyvää matemaattista moduulia, **-operaattoria, pow()-menetelmää tai NumPy-moduulia edellytyksistämme riippuen.