{"id":17016,"date":"2022-04-26T11:56:40","date_gmt":"2022-04-26T11:56:40","guid":{"rendered":"https:\/\/linuxways.net\/?p=17016"},"modified":"2022-04-26T11:56:40","modified_gmt":"2022-04-26T11:56:40","slug":"how-to-calculate-the-square-root-in-python","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-calculate-the-square-root-in-python\/","title":{"rendered":"How to Calculate the Square Root in Python"},"content":{"rendered":"<p>Python provides us with the built-in function of the square root which is a part of the math module. To find the square root of a number, we import the math module first then we can use the square root function.<\/p>\n<h2>Square root function:<\/h2>\n<p>This function takes one argument and returns the square root which is a float point number. The argument for the square root function should be a positive integer.<\/p>\n<h3>Syntax:<\/h3>\n<p>The syntax for the square root function is as follows:<\/p>\n<pre>math. sqrt(n)<\/pre>\n<p>Firstly, we write the module name \u201cmath\u201d then use dot notation (.) and then write function name \u201csqrt\u201d with one argument enclosed in round brackets. Let\u2019s understand this by a few examples.<\/p>\n<h3>Example 1:<\/h3>\n<pre>import math\r\n\r\nprint(\"The square root of 36 is:\",math.sqrt(36))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>The square root of 36 is: 6.0<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"410\" height=\"72\" class=\"wp-image-17017\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/a-screenshot-of-a-computer-description-automatica-1.png\" alt=\"A screenshot of a computer Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/a-screenshot-of-a-computer-description-automatica-1.png 410w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/a-screenshot-of-a-computer-description-automatica-1-300x53.png 300w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"35\" class=\"wp-image-17018\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-353.png\" \/><\/p>\n<p>In example 1, we imported the math library by using the import keyword. As we can see in Figure 1 we calculated the square root of 36. To display the result we used a print statement. The result is 6.0 a floating-point in figure2.<\/p>\n<h3><strong>Example 2:<\/strong><\/h3>\n<pre>import math\r\n\r\nn=eval(input(\"Enter a number to calculate square root\"))\r\n\r\nprint(\"The square root of number is:\",math.sqrt(n))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Enter a number to calculate square root 45<\/p>\n<p>The square root of the number is: 6.708203932499369<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"76\" class=\"wp-image-17019\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/a-screenshot-of-a-computer-description-automatica-2.png\" alt=\"A screenshot of a computer Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/a-screenshot-of-a-computer-description-automatica-2.png 470w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/a-screenshot-of-a-computer-description-automatica-2-300x49.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"392\" height=\"47\" class=\"wp-image-17020\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-354.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-354.png 392w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-354-300x36.png 300w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/p>\n<p>We can make the function dynamic by asking the user to enter a number of their choice. In Figure 3 we used the eval function to convert input into a number. A message will appear on the screen as shown in Figure 4. After entering the desired number we&#8217;ll get the result.<\/p>\n<h3><strong>Example 3:<\/strong><\/h3>\n<pre>from math import sqrt\r\n\r\ndef pythagoras(x, y):\r\n\r\nif x&lt;= 0 or y &lt;= 0:\r\n\r\nreturn\r\n\r\nreturn sqrt(x * x + y * y)\r\n\r\nprint(pythagoras(7, 4))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>8.06225774829855<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"230\" class=\"wp-image-17021\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/text-description-automatically-generated-35.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"148\" height=\"40\" class=\"wp-image-17022\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-355.png\" \/><\/strong><\/p>\n<p>In example 3, we have just imported only the sqrt function from the math module. In Figure 5, we defined a function Pythagoras to calculate the square root of the sum of the square of its two parameters. If the condition has been used to check that both numbers should be greater than zero. If this condition is true then the square root will be calculated. If any parameter is less than zero then the function will exit as the return keyword is used here. In Figure 5 we have called a function with two positive integer arguments. So we get the output of a floating integer as shown in Figure 6.<\/p>\n<h3><strong>Example 4:<\/strong><\/h3>\n<pre>from math import sqrt\r\n\r\ndef pythagoras(x, y):\r\n\r\nif x&lt;= 0 or y &lt;= 0:\r\n\r\nreturn\r\n\r\nreturn sqrt(x * x + y * y)\r\n\r\nprint(pythagoras(-7, 4))<\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<p>None<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"267\" height=\"221\" class=\"wp-image-17023\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/text-description-automatically-generated-36.png\" alt=\"Text Description automatically generated\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"127\" height=\"28\" class=\"wp-image-17024\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-356.png\" \/><\/p>\n<p>In Figure 7, we are calling the same Pythagoras function using a negative argument. The variable x value is -7 and the variable y value is 4. So as the function will be called it will check if the condition is true or not. Here in example 4, if the condition is true so first return statement will be executed. As we cannot find the square root of negative integers, so the function will output none.<\/p>","protected":false},"excerpt":{"rendered":"<p>Python provides us with the built-in function of the square root which is a part of the math module. To find the square root of a number, we&hellip;<\/p>","protected":false},"author":1,"featured_media":17054,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[10,994],"class_list":["post-17016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-python","tag-square-root"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17016"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17016\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17054"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}