logo

Kaip rašyti kvadratinę šaknį Python?

Python turi iš anksto nustatytą sqrt () funkcija, kuri grąžina skaičiaus kvadratinę šaknį. Tai apibrėžia kvadratinę šaknį iš reikšmės, kuri padaugina save, kad gautų skaičių. Funkcija sqrt() nenaudojama tiesiogiai norint rasti nurodyto skaičiaus kvadratinę šaknį, todėl turime naudoti matematika modulis funkcijai sqrt() iškviesti Python .

Pavyzdžiui, 144 kvadratinė šaknis yra 12.

Dabar pažiūrėkime kvadratinės šaknies funkcijos sintaksę, kad surastume nurodyto skaičiaus kvadratinę šaknį Python:

Sintaksė:

 math.sqrt(x) 

Parametrai:

x : Tai yra skaičius. kurioje skaičius turėtų būti didesnis nei 0 ir gali būti dešimtainis arba sveikasis skaičius.

Grąžinti:

Išvestis yra kvadratinės šaknies vertė.

siūlas.sunaikinti

Pastaba:

  • Metodo sqrt() išvestis bus slankiojo kablelio reikšmė.
  • Jei nurodyta įvestis yra neigiamas skaičius, tada išvestis bus ValueError. Grąžinama ValueError, nes bet kokia neigiama skaičiaus kvadratinės šaknies reikšmė nelaikoma tikruoju skaičiumi.
  • Jei įvestis yra ne kas kita, o skaičius, tada funkcija sqrt() grąžina NaN.

Pavyzdys:

Funkcijos sqrt() naudojimo Python pavyzdyje.

Kodas

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

Išvestis:

 4.0 

Kaip rašyti kvadratinę šaknį Python

1. Naudojant math.sqrt() metodą

Funkcija sqrt () yra integruota funkcija, kuri grąžina bet kurio skaičiaus kvadratinę šaknį. Toliau pateikiami žingsniai, kaip rasti skaičiaus kvadratinę šaknį.

  1. Paleiskite programą
  2. Apibrėžkite bet kurį skaičių, kurio kvadratinę šaknį reikia rasti.
  3. Iškvieskite sqrt () funkciją ir perduoti reikšmę, kurią apibrėžėte 2 veiksme, ir išsaugokite rezultatą kintamajame.
  4. Atspausdinkite kvadratinę šaknį.
  5. Nutraukite programą.

Python math.sqrt() metodas 1 pavyzdys

Python pavyzdinė programa, skirta rasti duoto sveikojo skaičiaus kvadratinę šaknį.

greitas rūšiavimas

Kodas

 # 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) 

Išvestis:

 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() metodas 2 pavyzdys

Sukurkime python programą, kuri surastų dešimtainių skaičių kvadratinę šaknį.

Kodas

 # 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 

Išvestis:

 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() metodas 3 pavyzdys

Šioje programoje mes perskaitėme skaičių iš vartotojo ir radome kvadratinę šaknį.

gimė Freddie Mercury

Kodas

 # 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) 

Išvestis:

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

1. Funkcijos math.pow() naudojimas

Pow () yra integruota funkcija, naudojama Python, norint grąžinti skaičiaus galią. Jis turi du parametrus. Pirmasis parametras apibrėžia skaičių, o antrasis parametras apibrėžia galios padidėjimą iki to skaičiaus.

Python math.pow() metodas Pavyzdys

Pažiūrėkime pavyzdinę programos math.pow() funkciją:

Kodas

 # 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 )) 

Išvestis:

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

3. Numpy modulio naudojimas

NumPy modulis taip pat yra galimybė rasti kvadratinę šaknį python.

Python Numpy pavyzdys

Pažiūrėkime pavyzdinę programą, kaip rasti kvadratinę šaknį duotame skaičių sąraše masyve.

knn algoritmas

Kodas

 # 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) 

Išvestis:

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

4. ** Operatoriaus naudojimas

Taip pat galime naudoti eksponentinį operatorių, kad surastume skaičiaus kvadratinę šaknį. Operatorius gali būti taikomas tarp dviejų operandų. Pavyzdžiui, x**y. Tai reiškia, kad kairysis operandas pakeltas iki dešinės laipsnio.

Toliau pateikiami žingsniai, kaip rasti skaičiaus kvadratinę šaknį.

1 žingsnis. Apibrėžkite funkciją ir nurodykite reikšmę kaip argumentą.

2 žingsnis. Jei apibrėžtas skaičius yra mažesnis nei 0 arba neigiamas, jis nieko nepateikia.

3 veiksmas. Norėdami rasti skaičiaus laipsnį, naudokite eksponentinį ** ženklą.

4 veiksmas. Paimkite skaitinę reikšmę iš vartotojo.

5 veiksmas. Iškvieskite funkciją ir išsaugokite jos išvestį kintamajame.

6 veiksmas. Rodyti skaičiaus kvadratinę šaknį Python.

7 veiksmas. Išeikite iš programos.

Python ** Operatoriaus 1 pavyzdys

Įgyvendinkime aukščiau nurodytus veiksmus Python programoje ir apskaičiuokime skaičiaus kvadratinę šaknį.

Kodas

Linux kuris
 # 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:>

Paaiškinimas:

Kaip matome aukščiau pateiktame pavyzdyje, pirmiausia paimame įvestį (skaičių) iš vartotojo, o tada naudojame eksponento ** operatorių, kad sužinotume skaičiaus galią. Kur 0,5 yra lygus √ (šakninis simbolis), kad padidėtų nurodyto skaičiaus galia. Pagaliau kodas išspausdina skaičiaus reikšmę ir lyginančią kvadratinę šaknį, naudodamas format() funkciją. Atsiradus tikimybei, kad klientas įves neigiamą skaičių, galimybė nieko negrąžins ir rezultatas bus aiškus.

Python ** Operatoriaus 2 pavyzdys

Sukurkime Python programą, kuri surastų kvadratinę šaknį tarp nurodyto diapazono. Šioje programoje radome visų skaičių nuo 0 iki 30 kvadratinę šaknį.

Kodas

 # 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))) 

Išvestis:

 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 

Išvada:

Apskritai, yra keletas būdų, kaip atsekti nurodyto skaičiaus kvadratinės šaknies reikšmę Python. Galime naudoti su skaičiais susijusį matematikos modulį, ** operatorių, pow() metodą arba NumPy modulį, atsižvelgiant į mūsų išankstines sąlygas.