{"id":17076,"date":"2022-05-04T18:36:01","date_gmt":"2022-05-04T18:36:01","guid":{"rendered":"https:\/\/linuxways.net\/?p=17076"},"modified":"2022-05-04T18:36:01","modified_gmt":"2022-05-04T18:36:01","slug":"how-to-square-a-number-in-python","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-square-a-number-in-python\/","title":{"rendered":"How to Square a Number in Python"},"content":{"rendered":"<p>Squaring a number means multiplying a number by itself. There are three ways to find the square of a number. Let\u2019s discuss them.<\/p>\n<ol>\n<li>Using multiplication<\/li>\n<li>Using the exponent operator<\/li>\n<li>Using the power function<\/li>\n<\/ol>\n<h2>Using multiplication<\/h2>\n<p>One of the easiest methods in which we use the multiplication operator.<\/p>\n<h3>Example:<\/h3>\n<pre>a= int(input(\"enter a number\"))\r\n\r\nprint(\"The square of a number is\", a*a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>enter a number 45<\/p>\n<p>The square of a number is 2025<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"329\" height=\"54\" class=\"wp-image-17077\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-358.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-358.png 329w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-358-300x49.png 300w\" sizes=\"auto, (max-width: 329px) 100vw, 329px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"261\" height=\"49\" class=\"wp-image-17078\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-359.png\" \/><\/p>\n<p>In example 1, we are taking a number as input from the user and then multiplying the number by the number itself. The output of the squared number is shown in figure2.<\/p>\n<p>To increase the reusability we can write it as a user-defined function<strong>.<\/strong><\/p>\n<h3>Example 2:<\/h3>\n<pre>def sqr(x):\r\n\r\nprint(\"The square of\", x, \"number is\", x * x)\r\n\r\nsqr(30)\r\n\r\nsqr(-66)\r\n\r\nsqr(0.75)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>The square of 30 number is 900<\/p>\n<p>The square of -66 number is 4356<\/p>\n<p>The square of 0.75 number is 0.5625<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"421\" height=\"112\" class=\"wp-image-17079\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-360.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-360.png 421w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-360-300x80.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"85\" class=\"wp-image-17080\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-361.png\" \/><\/p>\n<p>In example 2 we have defined an sqr(x) function that is printing the output. The function takes one parameter which is a number. A function is called with a positive integer, negative integer, and float and we get the output accordingly, as shown in figure 4.<\/p>\n<h3>Example 3:<\/h3>\n<pre>def sqr(x):\r\n\r\nreturn x * x\r\n\r\nprint(\"The square of number 30 is\", sqr(30))\r\n\r\nprint(\"The square of number -22 is\", sqr(-22))\r\n\r\nprint(\"The square of number 6.4 is\", sqr(6.4))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>The square of number 30 is 900<\/p>\n<p>The square of number -22 is 484<\/p>\n<p>The square of number 6.4 is 40.96000000000001<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"422\" height=\"145\" class=\"wp-image-17081\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-362.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-362.png 422w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-362-300x103.png 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"387\" height=\"67\" class=\"wp-image-17082\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-363.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-363.png 387w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-363-300x52.png 300w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/p>\n<p>In example 3 we have defined a function sqr(x) that is returning a squared number. This function takes one parameter, which is a number, and the return keyword returns the calculated value, which can be used for further calculations.<\/p>\n<h2>Using Exponent operator<\/h2>\n<p>The second method to get a square is by using the exponent operator (**). For the exponent operator, we need two values: one is base and the second is power. To find a square we use the value of power 2 which is fixed.<\/p>\n<h3>Example 1:<\/h3>\n<pre>a=int(input(\"enter a number\"))\r\n\r\nprint(\"The square of a number is\", a**2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>enter a number 9<\/p>\n<p>The square of a number is 81<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"348\" height=\"51\" class=\"wp-image-17083\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-364.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-364.png 348w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-364-300x44.png 300w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"237\" height=\"60\" class=\"wp-image-17084\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-365.png\" \/><\/p>\n<p>In example 1, we are inputting a number from the user, which will become the base for the exponent operator, and the power is 2, which is fixed in our case. The output of the number is shown in figure 8.<\/p>\n<p>We can define it as a function that returns the squared number<strong>.<\/strong><\/p>\n<h3>Example 2:<\/h3>\n<pre>def sqr_number(a):\r\n\r\nreturn a ** 2\r\n\r\nprint(sqr_number(16))\r\n\r\nprint(sqr_number(3.3))\r\n\r\nprint(sqr_number(-8))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>256<\/p>\n<p>10.889999999999999<\/p>\n<p>64<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"199\" height=\"138\" class=\"wp-image-17085\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-366.png\" \/><\/p>\n<p><strong>Output<\/strong>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"163\" height=\"72\" class=\"wp-image-17086\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-367.png\" \/><\/p>\n<p>In example 2, we have defined the function sqr_number(a). It takes one parameter and returns the output as shown in figure 10.<\/p>\n<h2>Using power function<\/h2>\n<p>A power function is a built-in number in Python. It takes two parameters: base and power. To calculate power value is 2.<\/p>\n<h3>Example 1:<\/h3>\n<pre>a=int(input(\"enter a number\"))\r\n\r\nprint(\"The square of a number is\", pow(a,2))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>enter a number 5<\/p>\n<p>The square of a number is 25<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"48\" class=\"wp-image-17087\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-368.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-368.png 363w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-368-300x40.png 300w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"246\" height=\"46\" class=\"wp-image-17088\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/04\/word-image-369.png\" \/><\/p>\n<p>In example 1, we take input from the user and calculate the square of the number using the power function. We get output as shown in figure 12.<\/p>","protected":false},"excerpt":{"rendered":"<p>Squaring a number means multiplying a number by itself. There are three ways to find the square of a number. Let\u2019s discuss them. Using multiplication Using the exponent&hellip;<\/p>","protected":false},"author":1,"featured_media":17121,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[10,999],"class_list":["post-17076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-python","tag-square"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17076","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=17076"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17076\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17121"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}