logo

Ako napísať druhú odmocninu v Pythone?

Python má preddefinované sqrt() funkcia, ktorá vracia druhú odmocninu čísla. Definuje druhú odmocninu hodnoty, ktorá sa vynásobí, čím sa získa číslo. Funkcia sqrt() sa nepoužíva priamo na nájdenie druhej odmocniny daného čísla, takže musíme použiť a matematika modul na volanie funkcie sqrt(). Python .

Napríklad druhá odmocnina zo 144 je 12.

Teraz sa pozrime na syntax funkcie druhej odmocniny na nájdenie druhej odmocniny daného čísla v Pythone:

Syntax:

 math.sqrt(x) 

Parametre:

X : Je to číslo. v ktorom by číslo malo byť väčšie ako 0 a môže byť desatinné alebo celé číslo.

Návrat:

Výstupom je druhá odmocnina.

Poznámka:

  • Výstupom metódy sqrt() bude hodnota s pohyblivou rádovou čiarkou.
  • Ak je daný vstup záporné číslo, výstupom bude hodnota ValueError. Hodnota ValueError sa vráti, pretože hodnota druhej odmocniny záporného čísla sa nepovažuje za skutočné číslo.
  • Ak je vstupom čokoľvek iné ako číslo, funkcia sqrt() vráti NaN.

Príklad:

Príklad použitia funkcie sqrt() v Pythone.

kód

príklady nfa
 import math x = 16 y = math.sqrt(x) print(y) 

Výkon:

 4.0 

Ako napísať druhú odmocninu v Pythone

1. Pomocou metódy math.sqrt().

Funkcia sqrt() je vstavaná funkcia, ktorá vracia druhú odmocninu ľubovoľného čísla. Nasledujú kroky na nájdenie druhej odmocniny čísla.

  1. Spustite program
  2. Definujte ľubovoľné číslo, ktorého druhá odmocnina sa má nájsť.
  3. Vyvolajte sqrt() funkciu a odovzdať hodnotu, ktorú ste definovali v kroku 2 a uložiť výsledok do premennej.
  4. Vytlačte druhú odmocninu.
  5. Ukončite program.

Metóda Python math.sqrt() Príklad 1

Príklad programu v Pythone na nájdenie druhej odmocniny daného celého čísla.

kód

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

Výkon:

 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 

Metóda Python math.sqrt() Príklad 2

Vytvorme program python, ktorý nájde druhú odmocninu desatinných čísel.

kód

 # 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 

Výkon:

 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 

Metóda Python math.sqrt() Príklad 3

V nasledujúcom programe sme prečítali číslo od používateľa a našli druhú odmocninu.

kód

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

Výkon:

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

1. Použitie funkcie math.pow().

Pow() je vstavaná funkcia, ktorá sa používa v Pythone na vrátenie sily čísla. Má dva parametre. Prvý parameter definuje číslo a druhý parameter určuje zvýšenie výkonu na toto číslo.

Metóda Python math.pow() Príklad

Pozrime sa na príklad programu pre funkciu math.pow():

kód

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

Výkon:

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

3. Použitie modulu Numpy

Modul NumPy je tiež možnosťou nájsť druhú odmocninu v pythone.

Príklad Python Numpy

Pozrime sa na príklad programu na nájdenie druhej odmocniny daného zoznamu čísel v poli.

kód

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

Výkon:

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

4. Použitie operátora **

Na zistenie druhej odmocniny čísla môžeme použiť aj operátor exponent. Operátor možno použiť medzi dvoma operandmi. Napríklad x**y. Znamená to, že ľavý operand sa zvýšil na pravý.

Nasledujú kroky na nájdenie druhej odmocniny čísla.

Krok 1. Definujte funkciu a odovzdajte hodnotu ako argument.

Krok 2. Ak je definované číslo menšie ako 0 alebo záporné, nevracia nič.

Krok 3. Pomocou exponenciálneho znamienka ** nájdite mocninu čísla.

Krok 4. Prevezmite číselnú hodnotu od používateľa.

Krok 5. Zavolajte funkciu a uložte jej výstup do premennej.

Krok 6. Zobrazte druhú odmocninu čísla v Pythone.

Krok 7. Ukončite program.

Príklad operátora Python ** 1

Implementujme vyššie uvedené kroky v programe Python a vypočítajme druhú odmocninu čísla.

kód

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

Vysvetlenie:

Ako vidíme vo vyššie uvedenom príklade, najprv zoberieme vstup (číslo) od používateľa a potom pomocou operátora exponent ** zistíme mocninu čísla. Kde 0,5 sa rovná √ (koreňový symbol), aby sa zvýšila mocnina daného čísla. Nakoniec kód vypíše hodnotu num a porovnávaciu hodnotu druhej odmocniny pomocou funkcie format(). V prípade, že klient zadá záporné číslo, funkcia nevráti nič a výsledok bude jasný.

Príklad operátora Python ** 2

Vytvorme program Python, ktorý nájde druhú odmocninu medzi zadaným rozsahom. V nasledujúcom programe sme našli druhú odmocninu všetkých čísel od 0 do 30.

kód

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

Výkon:

 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 

Záver:

Celkovo vzaté, existuje viacero spôsobov, ako v Pythone vystopovať druhú odmocninu daného čísla. Môžeme použiť matematický modul súvisiaci s číslom, operátor **, metódu pow() alebo modul NumPy, v závislosti od našich predpokladov.