{"id":2629,"date":"2020-09-04T10:55:39","date_gmt":"2020-09-04T10:55:39","guid":{"rendered":"https:\/\/idiagress.com\/vinaaypatil\/?p=2629"},"modified":"2023-10-28T12:25:15","modified_gmt":"2023-10-28T12:25:15","slug":"1-6-loops-and-iterations","status":"publish","type":"post","link":"https:\/\/idiagress.com\/vinaaypatil\/2020\/09\/04\/1-6-loops-and-iterations\/","title":{"rendered":"1.6 Loops and Iterations"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Understanding Loops and Iterations in Python<\/h2>\n\n\n\n<p>Loops are a fundamental concept in programming that allows us to execute a set of instructions repeatedly until a specific condition is met. They are essential for automating repetitive tasks and solving complex problems more efficiently. In Python, loops are a crucial part of the language, and there are several ways to implement them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Need for Iterations<\/h3>\n\n\n\n<p>In mathematics and computer science, we often encounter problems that require a repetitive approach to reach a solution. This approach is known as &#8220;iteration.&#8221; It involves repeatedly applying a set of steps until a desired result is achieved. Let&#8217;s explore this concept further with an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Guessing Game: Finding the Square Root<\/h3>\n\n\n\n<p>Suppose we want to calculate the square root of a number, such as 196. We can represent this problem as an equation:Copy code<\/p>\n\n\n\n<p><code>x^2 = 196<\/code><\/p>\n\n\n\n<p>Our objective is to find the value of &#8216;x.&#8217; To keep things simple, we&#8217;ll focus on finding the positive square root.<\/p>\n\n\n\n<p>Two things are evident about the solution:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The square root is greater than 0.<\/li>\n\n\n\n<li>The square root is less than 196.<\/li>\n<\/ol>\n\n\n\n<p>This information defines the range of our solution, which is from 0 to 196. To find the square root, we could start at 0 and increment our guess step by step until we arrive at the correct value.<\/p>\n\n\n\n<p>A python loop for the solution will appear in the following manner:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><\/tr><tr><td><kbd><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">,<\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">196<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">,<\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">1<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">):<br><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x * _x) == <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">196<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">:<br><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">\"Square root of 196 is: \"<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _x) <\/span><\/kbd><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Now, let&#8217;s implement the square root guessing program in Python, which prompts the user to input a number and utilizes a loop to find the square root.<span class=\"Apple-converted-space\">\u00a0 <\/span>(<strong>sqaure_root.py<\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>square_root.py<\/b><\/span><\/td><\/tr><tr><td><kbd><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"># program to 'Guess' square root of a number<br># Ask for number for calculating square root<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_s = <\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">int<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">\"Enter number for calculating square root<\/span><\/kbd><kbd><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">: \"<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"># loop for guessing, range 0 to _s, with step increase of 1<br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(_s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x*_x) == _s:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">\"Square root of \"<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">\" is: \"<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _x)<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(_s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">\" is not a perfect square\"<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">)<\/span><\/kbd><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><br><\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>One key observation: the<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018range\u2019<\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>in the <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018for\u2019<\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>loop command is <b><i>shortened<span class=\"Apple-converted-space\">&nbsp;<\/span><\/i><\/b><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(_s): instead of <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(0, _s, 1):<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Simplifying the Range<\/h3>\n\n\n\n<p>You may have noticed that we use <code>for _x in range(_s)<\/code> instead of the more detailed <code>for _x in range(0, _s, 1)<\/code>.<\/p>\n\n\n\n<p>In Python, when you don&#8217;t specify the starting point and step size, the default values are assumed to be 0 and 1, respectively. So, we can simply use <code>range(_s)<\/code> to achieve the same result.<\/p>\n\n\n\n<p>The output for our program is as follows:<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"430\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/square_root_absurd_output_python.png\" alt=\"\" class=\"wp-image-2630\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/square_root_absurd_output_python.png 516w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/square_root_absurd_output_python-300x250.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/square_root_absurd_output_python-335x279.png 335w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output is quite\u00a0<b><i>absurd<\/i><\/b>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Analyzing the Output<\/h3>\n\n\n\n<p>Upon analyzing the output, we observe the following issues:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Every time the &#8216;guess&#8217; doesn&#8217;t match the solution, the program enters the &#8220;else&#8221; statement and prints that the input is not a perfect square. This occurs as we move from &#8216;0&#8217; upwards (1, 2, etc.).<\/li>\n\n\n\n<li>When the &#8216;guess&#8217; reaches the solution, the &#8220;if&#8221; condition is met, and it prints the square root. But the program doesn&#8217;t stop; it continues to the next step (15, 16, and so on) and once again enters the &#8220;else&#8221; statement.<\/li>\n<\/ul>\n\n\n\n<p>To stop a loop midway, we have the\u00a0 \u00a0<b>\u2018break\u2019<\/b>\u00a0\u00a0command. Incorporating the above observations, let\u2019s create an improved program. (<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">improved_sqrt.py<\/mark><\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>improved_sqrt.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># program to &#8216;Guess&#8217; square root of a number<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Ask for number for calculating square root<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_s = <\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">int<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;Enter number for calculating square root : &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># loop for guessing, range 0 to _s, step increase of 1<\/i><br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(_s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x*_x) == _s:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;Square root of &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; is: &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _x)<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">break<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x*_x != _s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(_s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; is not a perfect square&#8221;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><br><\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The\u00a0\u00a0<b>\u00a0\u2018break\u2019<\/b>\u00a0\u00a0command is a signal for the program to\u00a0<b>exit the loop<\/b>. We have given it within the \u2018if\u2019 condition. When the condition is met, i.e., we have our solution ( (_x*_x) == _s), then the \u2018<b>break<\/b>\u2019 command will ensure that further \u2018<b>guessing\u2019<\/b>\u00a0is stopped.<\/p>\n\n\n\n<p>The improvements we&#8217;ve made are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Moving the &#8216;else&#8217; statement:<\/strong> We correctly identified that the &#8216;else&#8217; statement should be executed after the &#8216;for&#8217; loop. This ensures that it is only executed once the loop has exhausted all possible guesses and not during each iteration.<\/li>\n\n\n\n<li><strong>Using the &#8216;break&#8217; statement:<\/strong> We&#8217;ve effectively used the &#8216;break&#8217; statement to exit the loop as soon as the solution is found. This prevents the loop from continuing unnecessarily.<\/li>\n<\/ol>\n\n\n\n<p>With these changes, the program now behaves as expected and provides accurate results. Here&#8217;s what the program outputs<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_square_root_python_program.png\"><img loading=\"lazy\" decoding=\"async\" width=\"555\" height=\"165\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_square_root_python_program.png\" alt=\"\" class=\"wp-image-2631\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_square_root_python_program.png 555w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_square_root_python_program-300x89.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_square_root_python_program-335x100.png 335w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>A much better execution.<\/p>\n\n\n\n<p><strong>Continue a loop until a specific condition is met.<\/strong><\/p>\n\n\n\n<p>In our previous program, we used a &#8216;for&#8217; loop to continue until a certain condition was met, at which point we used the &#8216;break&#8217; statement to exit the loop. This two-step process is effective, but Python offers a more concise alternative.<\/p>\n\n\n\n<div style=\"height:22px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">While Loop<\/h2>\n\n\n\n<p>Python provides us with another type of loop, the &#8216;while&#8217; loop, which combines both the looping condition and the exit condition into a single statement.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_conditions.png\"><img loading=\"lazy\" decoding=\"async\" width=\"715\" height=\"539\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_conditions.png\" alt=\"\" class=\"wp-image-2709\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_conditions.png 715w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_conditions-300x226.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_conditions-335x253.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_conditions-600x452.png 600w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>(! corresponds to &#8216;NOT&#8217;)<\/p>\n\n\n\n<p>Compared to the<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>\u2018<b>for<\/b>\u2019 loop<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 <\/span>the <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>\u2018<b>range<\/b>\u2019 <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>is not specified automatically. The<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>\u2018<b>while<\/b>\u2019 loop specifies only the<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 <\/span>\u2018<b>end<\/b>\u2019<span class=\"Apple-converted-space\">\u00a0 <\/span>point. <span class=\"Apple-converted-space\">\u00a0 <\/span>( _x &lt;= _s)<\/p>\n\n\n\n<p>Thus, we have to specify both, the <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<b>start<\/b>\u2019 <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>point and the <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<b>step size<\/b>\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>separately.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>While Loop Implementation<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><i># Start point for the while loop<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_x = <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">0<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><i># while loop<\/i><br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _x &lt;= _s <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x * _x != _s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>_x = _x + <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">1<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><i># Step size = increment of \u20181\u2019<\/i><\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As an alternative to the &#8216;for&#8217; loop, we can achieve the same goal using the &#8216;while&#8217; loop. The &#8216;while&#8217; loop is particularly helpful when we want to specify both the starting point and the step size within the loop. Let&#8217;s delve into how the &#8216;while&#8217; loop works and explore its advantages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding the &#8216;while&#8217; Loop<\/h3>\n\n\n\n<p>Compared to the &#8216;for&#8217; loop, the &#8216;while&#8217; loop differs in the way it handles the range. While the &#8216;for&#8217; loop automatically specifies the starting point, step size, and end point, the &#8216;while&#8217; loop only specifies the end point using a conditional statement. In other words, we need to define both the starting point and the step size explicitly.<\/p>\n\n\n\n<p>Let&#8217;s see how we can implement the &#8216;while&#8217; loop for our square root guessing program:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>while_sqrt.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># program to &#8216;Guess&#8217; square root of a number<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Ask for number for calculating square root<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_s = <\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">int<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;Enter number for calculating square root : &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Start point for the while loop<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_x = <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">0<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># while loop for guessing the square root<\/i><br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x * _x != _s) <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">and <\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_x &lt;= _s:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>_x = _x + <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x*_x == _s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;Square root of &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; is: &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _x)<br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(_s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; is not a perfect square&#8221;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">)<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/span><\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>At an initial glance, the program will seem slightly odd. Only one operation occurs within the\u00a0<b>while<\/b>\u00a0loop ( _x = _x + 1), whereas all the remaining conditionality statements are outside the loop.<\/em><\/p>\n\n\n\n<p>In this program, we set the start point for the &#8216;while&#8217; loop by initializing <code>_x<\/code> to 0 before entering the loop. The &#8216;while&#8217; loop is governed by the condition <code>(_x * _x != _s) and (_x &lt;= _s)<\/code>. As long as both conditions hold true, the loop continues to iterate, with each iteration incrementing <code>_x<\/code> by 1.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Algorithm<\/mark><\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p>Let&#8217;s break down how the &#8216;while&#8217; loop calculates the square root of 196:<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>STEP 1 : Declare and Initialize Variable<\/summary>\n<p>The variables have the following values: <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">_s = 196 ,<\/mark><\/strong> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>_x = 0<\/strong><\/mark><\/p>\n<\/details>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>STEP 2 : First Iteration, check our conditions for solution<\/summary>\n<p>In the first iteration of the &#8216;while&#8217; loop, we check the conditions:<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"> _x = 0<\/mark><\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Condition 1: _x * _x != _s (0 * 0 != 196)<\/li>\n\n\n\n<li>Condition 2: _x &lt;= _s (0 &lt;= 196)&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>Both conditions are satisfied, so we enter the loop, and _x is incremented to 1.<\/p>\n<\/details>\n\n\n\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-b5f14f7b\"><\/div>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>STEP 3 : While Loop Continues as long as conditions are met (till _x =13)<\/summary>\n<ul class=\"wp-block-list\">\n<li>The &#8216;while&#8217; loop continues in the same manner, and the value of _x increases (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>1, 2, 3, 4, 5, and so on<\/strong><\/mark>).<\/li>\n\n\n\n<li>After several iterations, we reach <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>_x = 13<\/strong><\/mark>. <\/li>\n\n\n\n<li>At this point, the conditions are still met, and we continue to increment _x.<\/li>\n<\/ul>\n\n\n\n<div style=\"height:41px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/details>\n\n\n\n<div style=\"height:44px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-442e7b1a\"><\/div>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>STEP 4 : While Loop Breaks ( at _x=14)<\/summary>\n<ul class=\"wp-block-list\">\n<li>Finally, after numerous repetitions, we reach <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">_x = 14<\/mark><\/strong>.<\/li>\n\n\n\n<li>In the next iteration:<\/li>\n\n\n\n<li>Condition 1:      _x * _x != _s      (<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">14 * 14 != 196)<\/mark><\/strong><\/li>\n\n\n\n<li>Condition 2:     _x &lt;= _s             (14 &lt;= 196) <\/li>\n\n\n\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Condition 1 is not satisfied, so we exit the &#8216;while&#8217; loop with the value of _x = 14.<\/mark><\/strong><\/li>\n<\/ul>\n\n\n\n<div style=\"height:37px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div style=\"height:37px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/details>\n\n\n\n<div style=\"height:41px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-29cb0303\"><\/div>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>STEP 5 : We exit the While Loop and Check which condition is met<\/summary>\n<ul class=\"wp-block-list\">\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">_x = 14<\/mark><\/strong>.<\/li>\n\n\n\n<li>IF:      _x * _x  == _s      (<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">14 * 14  = = 196<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">)<\/mark><\/strong>\n<ul class=\"wp-block-list\">\n<li>We have found our answer<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>ELSE:\n<ul class=\"wp-block-list\">\n<li> The number is not a perfect square<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<div style=\"height:37px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/details>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The &#8216;while&#8217; loop essentially keeps iterating as long as both conditions are met, and it stops as soon as one of the conditions fails. This behavior allows us to find the square root efficiently.<\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output for the program (while_sqrt.py) is as follows<\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_square_root_output_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"127\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_square_root_output_python.png\" alt=\"\" class=\"wp-image-2633\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_square_root_output_python.png 544w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_square_root_output_python-300x70.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/while_loop_square_root_output_python-335x78.png 335w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When we run the &#8216;while&#8217; loop program (<code>while_sqrt.py<\/code>), we observe no significant difference between the outputs from the &#8216;for&#8217; loop and the &#8216;while&#8217; loop. Both loops yield the same result.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><em><strong>(In case you are still not getting the feel on how the <span class=\"Apple-converted-space\">&nbsp; <\/span>\u2018while\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>loop is getting executed, it might be a good idea to explore the program in<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018Thonny\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>in step wise debug mode<\/strong><\/em>)<\/h5>\n\n\n<p><iframe loading=\"lazy\" title=\"Understanding While  Loop using Thonny\" width=\"1140\" height=\"641\" src=\"https:\/\/www.youtube.com\/embed\/V0g07AKcjDM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/p>\n\n\n\n<p>The Differences between the \u2018two\u2019 loops can be tabulated as below:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #333333; font-family: Avenir; font-size: medium;\">No<\/span><\/td><td><span style=\"color: #333333; font-family: Avenir; font-size: medium;\">For Loop with If<\/span><\/td><td><span style=\"color: #333333; font-family: Avenir; font-size: medium;\">While Loop<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">1<\/span><\/td><td><span style=\"color: #a0a1a7; font-family: Avenir; font-size: medium;\"><i># loop for guessing, range 0 to _s, step increase of 1<\/i><br><\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Avenir; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\">(_s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> (_x*_x) == _s:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Avenir; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Avenir; font-size: medium;\">&#8220;Square root of &#8220;<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\">, _s, <\/span><span style=\"color: #50a14f; font-family: Avenir; font-size: medium;\">&#8221; is: &#8220;<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\">, _x)<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">break <\/span><\/td><td><span style=\"color: #a0a1a7; font-family: Avenir; font-size: medium;\"><i># Initialize for while loop<\/i><br><\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\">_x = <\/span><span style=\"color: #986804; font-family: Avenir; font-size: medium;\">0<br><\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; <\/span><br><\/span><span style=\"color: #a0a1a7; font-family: Avenir; font-size: medium;\"><i># while loop for guessing the square root<\/i><br><\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">while<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> _x &lt;= _s <\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">and<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> (_x *_x != _s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>_x = _x + <\/span><span style=\"color: #986804; font-family: Avenir; font-size: medium;\">1 <\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">2<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">with range initial value of<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u20180\u2019<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">and step size of <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>\u20181\u2019<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">is automatically defined in <span class=\"Apple-converted-space\">&nbsp; <\/span>\u2018range\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>option of the<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<i>For loop<\/i>\u2019<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">initial value needs to be set before commencing the <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<i>while<\/i>\u2019 loop<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>_x = 0<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Step size needs to be defined within the<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>\u2018while\u2019 loop<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>_x = _x +1<\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">3<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">A<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018break\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>command is required to stop the loop when the \u2018conditionality\u2019 on \u2018if\u2019 is met<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Loop automatically breaks when <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018conditionality\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>within the<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018while\u2019 loop<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>is met<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">4<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">For each conditionality, separate \u2018if\u2019 commands are needed<\/span>\n<p><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">for<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> _x <\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">in<\/span> <span style=\"color: #1084bc; font-family: Avenir; font-size: medium;\">range<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\">(_s):<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> (_x*_x) == _s<\/span><\/p>\n<p><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2026\u2026.<\/span><\/p>\n<p><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> (_x*_x*_x) == _s<\/span><\/p>\n<p><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2026\u2026.<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Multiple conditionality statements can be added within \u2018while\u2019 loop by using \u2018and\u2019 \u2018or\u2019 statements<\/span>\n<p><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">while<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> _x &lt;= _s <\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">and<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> (_x *_x != _s) <\/span><span style=\"color: #a626a4; font-family: Avenir; font-size: medium;\">or<\/span><span style=\"color: #383a42; font-family: Avenir; font-size: medium;\"> (_x *_x*_x != _s) <\/span><\/p>\n<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">5<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">for [<\/span><span style=\"color: #fb0207; font-family: Avenir; font-size: medium;\">iterating variable<\/span><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">] in [<\/span><span style=\"color: #fb0207; font-family: Avenir; font-size: medium;\">sequence<\/span><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">]:<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #800080; font-family: Avenir; font-size: medium;\">[do something]<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">while [<\/span><span style=\"color: #fb0207; font-family: Avenir; font-size: medium;\">a condition is True<\/span><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">]:<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>[<\/span><span style=\"color: #400080; font-family: Avenir; font-size: medium;\">do something<\/span><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">]<\/span><\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Up to this point, our square root guessing program has employed a &#8216;brute force&#8217; approach. It systematically tries every available option one after the other until it reaches the answer. For instance, when finding the square root of &#8216;196,&#8217; it took &#8217;14&#8217; steps to arrive at the result (starting from 1 and incrementing).<\/p>\n\n\n\n<p>While this method may be convenient for smaller numbers, it becomes highly impractical when dealing with larger numbers. Consider an example like &#8216;19600,&#8217; where the square root is &#8216;140.&#8217; Using the &#8216;brute force&#8217; method would require &#8216;140&#8217; steps to reach the solution, making it significantly inefficient.<\/p>\n\n\n\n<p>To expedite the process, we need a more intelligent technique instead of relying on blind guesses. One such method is the <strong>Bisection Method<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enhancing Efficiency: The Bisection Method<\/h3>\n\n\n\n<p>In this method, the \u2018<b>guess\u2019<\/b>&nbsp;&nbsp;is calculated using a formula called the \u2018<b>bisection<\/b>\u2019 formula.<\/p>\n\n\n\n<p>The term &#8216;bisection&#8217; means &#8216;to bisect&#8217; or, in other words, &#8216;to split into two halves.&#8217; In this method, the &#8216;guess&#8217; is calculated using a formula known as the &#8216;bisection&#8217; formula. Instead of a linear search as in the &#8216;brute force&#8217; approach, the &#8216;bisection&#8217; method narrows down the possibilities more efficiently.<\/p>\n\n\n\n<p>Here&#8217;s how the Bisection Method works:<\/p>\n\n\n\n<p>Our <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>current Guess<\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>has divided our solution interval in two parts. We shall select a<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<b>New Interval<\/b>\u2019 <span class=\"Apple-converted-space\">&nbsp; <\/span>for our next guess.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Interval 01<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Guess<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Interval 02<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">x<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">0 &#8211; 9800<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">9800<\/mark><\/strong><\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">9800 &#8211; 19600<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">x<\/span><span style=\"color: #000000; font-family: Iowan Old Style; font-size: xx-medium;\"><sup>2<\/sup><\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">96,040,000<\/span><\/td><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Comparing to solution<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">96,040,000<span class=\"Apple-converted-space\">&nbsp; <\/span>&gt;<span class=\"Apple-converted-space\">&nbsp; <\/span>19600<\/span><\/td><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Observation<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\">Lower than current guess<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"> \u2018New Guess\u2019 <span class=\"Apple-converted-space\">&nbsp; <\/span>should be less than our current \u2018Guess\u2019<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Higher than current guess <\/mark><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: medium;\">New Interval<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: medium;\">0 &#8211; 9800<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Futura; font-size: medium;\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Eliminated<\/mark><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Futura; font-size: medium;\">New Guess<\/span><\/td><td><span style=\"color: #000000; font-family: Futura; font-size: medium;\">(0 + 9800) \/2<\/span> <p><span style=\"color: #000000; font-family: Futura; font-size: medium;\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">= 4900<\/mark><\/span><\/p> <\/td><td>&nbsp;<\/td><td>&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Once we have a \u2018<b>New Guess<\/b>\u2019 we repeat the procedure over and over again (<b>Iterations<\/b>)<\/p>\n\n\n\n<p>The general practice is to put a limit on the number of <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<b>iterations<\/b>\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>say <span class=\"Apple-converted-space\">&nbsp; <\/span>\u2019<b>10<\/b>\u2019 iterations or to stop when we get the solution.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Flowchart<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1220\" height=\"1682\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart.jpg\" alt=\"\" class=\"wp-image-2634\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart.jpg 1220w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart-218x300.jpg 218w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart-768x1059.jpg 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart-743x1024.jpg 743w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart-243x335.jpg 243w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_flow_chart-600x827.jpg 600w\" sizes=\"auto, (max-width: 1220px) 100vw, 1220px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>We can implement the flowchart in the following program<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>bisection.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Program to find square root using bisection<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Get input for finding square root<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_s = <\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;Enter number for calculating square root: &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">))<br>_limit = <\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">int<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;Enter number of iterations: &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Define interval for initial solution<\/i><br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_high = _s<br>_low = <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">0.0<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_iter = <\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">0<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Create loop with condition<\/i><br><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x * _x) != _s <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> _iter &lt; _limit:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"> (_x * _x) &gt; _s:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span>_high = _x<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #a626a4; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">:<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span>_low = _x<br><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>_iter = _iter+<\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>_x = (_high + _low)\/<\/span><span style=\"color: #986804; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;At&#8221;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; iteration solution is: &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _x)<br><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #1084bc; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8220;After &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; iterations the square root of &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _s, <\/span><span style=\"color: #50a14f; font-family: Courier; font-size: medium;\">&#8221; is : &#8220;<\/span><span style=\"color: #383a42; font-family: Courier; font-size: medium;\">, _x<br><\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output of the program is as follows<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_sqrt_output_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"301\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_sqrt_output_python.png\" alt=\"\" class=\"wp-image-2635\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_sqrt_output_python.png 590w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_sqrt_output_python-300x153.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_sqrt_output_python-335x171.png 335w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>From the output of our program using the Bisection Method, a few important observations become evident:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Use of Floats<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Advantage of Floats<\/strong>: One clear advantage of using &#8216;floats&#8217; instead of &#8216;integers&#8217; for our calculations is that, after the 3rd iteration, we enter the realm of decimal numbers. The square roots of many numbers are not whole integers, and it&#8217;s important to use &#8216;floats&#8217; to accurately represent and calculate these decimal values.<\/li>\n<\/ol>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">The Bisection Method: An Approximation Technique<\/h3>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Approximation Nature<\/strong>: The Bisection Method is inherently an approximation technique. As we increase the number of iterations, our solution becomes increasingly &#8216;closer&#8217; to the actual value. For instance, at the end of 10 iterations, the calculated value is &#8216;143.554,&#8217; with an error of 3.554 (|143.554 \u2013 140|).<\/li>\n\n\n\n<li><strong>Trade-off Between Accuracy and Time<\/strong>: There&#8217;s always a trade-off between the accuracy of the solution we desire and the time or number of iterations it takes to reach that solution. The Bisection Method allows us to control this trade-off. If we prioritize higher accuracy, we can increase the number of iterations, which will result in a more precise estimate. This is in contrast to the &#8216;brute force&#8217; method, which would require significantly more steps to achieve a similar level of accuracy. For example if we take 25 steps, the answer has an error in the sixth decimal place. <\/li>\n<\/ol>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/more_iterations_bisection_metho.png\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"327\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/more_iterations_bisection_metho.png\" alt=\"\" class=\"wp-image-2636\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/more_iterations_bisection_metho.png 445w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/more_iterations_bisection_metho-300x220.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/more_iterations_bisection_metho-335x246.png 335w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Handling Non-Perfect Squares<\/strong>: <\/p>\n\n\n\n<p>Another notable advantage of the Bisection Method is its ability to calculate the approximate roots of non-perfect squares. For instance, if we were to apply the &#8216;brute force&#8217; method to the number &#8217;32,&#8217; it would inform us that &#8217;32 is not a perfect square.&#8217; However, when we provide &#8217;32&#8217; to the Bisection Method, it approximates the root as &#8216;5.671&#8217; after 10 iterations. This showcases the flexibility of the Bisection Method in handling a wider range of cases, making it a valuable tool in numerical computation.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"300\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python.png\" alt=\"\" class=\"wp-image-2637\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python.png 543w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python-300x166.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python-150x83.png 150w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python-335x185.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/non_perfect_square_bisection_python-225x125.png 225w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In summary, the use of &#8216;floats&#8217; and the Bisection Method together offer a more accurate and flexible approach to calculating square roots. The Bisection Method&#8217;s approximation nature allows us to strike a balance between accuracy and efficiency, making it a powerful tool for numerical computations, especially when dealing with both perfect and non-perfect squares<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><b>Choice of Loop<\/b><\/h3>\n\n\n\n<p>Whether to use &#8216;<b>for<\/b>&#8216; loop or &#8216;<b>while<\/b>&#8216; loop is a matter of convenience. Whichever you find useful to get the desired output is acceptable.<\/p>\n\n\n\n<p>The critical question is: Is &#8216;<b>while<\/b>&#8216; loop just an additional method to execute the same operations done by a &#8216;<b>for&#8217;<\/b>&nbsp;loop?<\/p>\n\n\n\n<p><b><i>The answer is &#8216;NO.&#8217;<\/i><\/b><\/p>\n\n\n\n<p>While &#8216;for&#8217; and &#8216;while&#8217; loops can often be used interchangeably to achieve similar results, they have their own unique purposes. It&#8217;s important to be comfortable using both loops and to select the one that best fits the problem you&#8217;re solving.<\/p>\n\n\n\n<p>In the context of the square root calculation programs we&#8217;ve explored, both &#8216;for&#8217; and &#8216;while&#8217; loops were used effectively. The &#8216;for&#8217; loop provided a clear way to iterate through a known range, while the &#8216;while&#8217; loop allowed us to iterate until a specific condition was met.<\/p>\n\n\n\n<p>As we progress in our programming journey, we&#8217;ll encounter more situations that will make it evident when to use one type of loop over the other. Additionally, we&#8217;ll discover specific features of the &#8216;for&#8217; loop, such as traversing lists and strings, which will be explored in later chapters.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Roots of Quadratic Equation using Python<\/h3>\n\n\n\n<p>Let&#8217;s consider an application of these techniques: finding the roots of a quadratic equation. Solving equations can present challenges, such as determining the range for solutions and handling both positive and negative roots.<\/p>\n\n\n\n<p>For instance, consider the equation <strong>2x^2 + 10x = 1288<\/strong>. To tackle equations like this, we can prompt for a range and account for both positive and negative roots.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_01.png\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"477\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_01.png\" alt=\"\" class=\"wp-image-2638\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_01.png 635w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_01-300x225.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_01-335x252.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_01-600x451.png 600w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_02.png\"><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"579\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_02.png\" alt=\"\" class=\"wp-image-2639\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_02.png 646w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_02-300x269.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_02-335x300.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/roots_of_equation_python_program_bisection_02-600x538.png 600w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The program for finding the roots of a quadratic equation (<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">equation_roots.py<\/mark><\/strong>) is more extensive, but the underlying logic remains the same, relying on the Bisection Method implemented through a &#8216;while&#8217; loop. The output is as follows.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/equation_bisection_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"603\" height=\"620\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/equation_bisection_output.png\" alt=\"\" class=\"wp-image-2640\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/equation_bisection_output.png 603w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/equation_bisection_output-292x300.png 292w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/equation_bisection_output-326x335.png 326w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/equation_bisection_output-600x617.png 600w\" sizes=\"auto, (max-width: 603px) 100vw, 603px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For the equation <strong>2x^2 + 10x = 1288<\/strong>, the real roots are <strong>x = 23<\/strong> and <strong>x = -28<\/strong>. Our &#8216;bisection&#8217; method provides approximate roots of <strong>x = 22.998<\/strong> and <strong>x = -29.97<\/strong>.<\/p>\n\n\n\n<p>These approximate roots are indeed quite close to the actual roots. It&#8217;s worth noting that the change in values during successive iterations is different for the positive and negative roots. This disparity emphasizes the robustness of the &#8216;bisection&#8217; algorithm.<\/p>\n\n\n\n<p>However, in hindsight, it becomes clear that there is room for improvement in our program. One aspect that could be enhanced is the inclusion of a statement to handle cases where the solution falls outside the specified interval. This adjustment would make the program even more robust and user-friendly.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Maybe you can improve the program accordingly.<\/mark> <\/p>\n\n\n<div class=\"vw-infobox\"><div class=\"vw-infobox-inner\"><h4 class=\"vw-infobox-title\"><span>Exercise<\/span><\/h4><div class=\"vw-infobox-content\"><\/p>\n<ul>\n<li>Implement a program to calculate the cube root of a given number, using the brute force method in a \u2018for\u2019 loop.<\/li>\n<li>Implement the previous program using the \u2018bisection\u2019 method.<\/li>\n<li>Modify the program to calculate cube root using \u2018bisection,\u2019 to include a max permissible error to stop the loop.<\/li>\n<\/ul>\n<table cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\"><span style=\"color: #000000; font-family: Courier; font-size: small;\">For example, <\/span><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><span style=\"color: #000000; font-family: Courier; font-size: small;\">Number = 3375<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Courier; font-size: small;\">Interval ( 0 to 3375),<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>Limit on iterations (10), <span class=\"Apple-converted-space\">\u00a0 \u00a0<\/span><\/span><\/p>\n<p><span style=\"color: #000000; font-family: Courier; font-size: small;\">Max permissible error ( 0.1 )<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Courier; font-size: small;\">So the loop should stop, either when the limit on iterations is reached, OR, if the answer falls between ( 14.9<span class=\"Apple-converted-space\">\u00a0 <\/span>to 15.1) <\/span><\/p>\n<p><span style=\"color: #a626a4; font-family: Courier; font-size: small;\">while<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\"> \u2026\u2026 <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: small;\">and<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\"> \u2026\u2026 <\/span><span style=\"color: #a626a4; font-family: Courier; font-size: small;\">or<\/span> <span style=\"color: #1084bc; font-family: Courier; font-size: small;\">abs<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\">(_X**3 &#8211; _S) &lt; <\/span><span style=\"color: #986804; font-family: Courier; font-size: small;\">0.1<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\">:<br \/>\n<span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span><\/span><\/p>\n<p><span style=\"color: #0e6a63; font-family: Courier; font-size: small;\">abs( )<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\"> returns the \u2018absolute\u2019 value of the operation. <\/span><\/p>\n<p><span style=\"color: #0e6a63; font-family: Courier; font-size: small;\"> abs(14+9) = abs(23) = 23<\/span><\/p>\n<p><span style=\"color: #0e6a63; font-family: Courier; font-size: small;\"> abs(-14-9) = abs(-23) = 23<\/span><\/p>\n<p><span style=\"color: #0e6a63; font-family: Courier; font-size: small;\"> abs(-14+9) = abs(-5) = 5<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Courier; font-size: small;\">Essentially it returns the \u2018+ve\u2019 value, and for \u2018\u2013ve\u2019 values, if present, it is converted into a \u2018+ve\u2019 sign<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>Find the \u2018fifth\u2019 root of the number \u2018537824\u2019 using Bisection method<\/li>\n<li>Implement a program to find if the roots of equation <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>2x<sup>3<\/sup> + 10x = 5628,<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>lie in between<span class=\"Apple-converted-space\">\u00a0 <\/span>-70 to 70.<span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<\/ul>\n<p><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>In this chapter on Python loops and iterations, we&#8217;ve explored fundamental concepts related to loops, iteration, and the practical application of these concepts. Key points covered in this chapter include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Introduction to Loops<\/strong>: Loops are a fundamental programming concept used to execute a set of instructions repeatedly until a specific objective is achieved. We&#8217;ve seen how loops are essential for automating repetitive tasks.<\/li>\n\n\n\n<li><strong>Iterations<\/strong>: Iterations involve repeating a set of steps until a desired solution is obtained. We&#8217;ve learned how iterations are fundamental in various computational and mathematical processes.<\/li>\n\n\n\n<li><strong>&#8216;For&#8217; Loop<\/strong>: We&#8217;ve explored the &#8216;for&#8217; loop, a loop that iterates over a predefined range, making it suitable when the number of iterations is known in advance. The &#8216;for&#8217; loop is commonly used to work with sequences like lists and strings.<\/li>\n\n\n\n<li><strong>&#8216;While&#8217; Loop<\/strong>: The &#8216;while&#8217; loop continues to execute as long as a specified condition holds true. It provides flexibility when the number of iterations is not known beforehand and is useful for scenarios that require continuous condition checking.<\/li>\n\n\n\n<li><strong>The Bisection Method<\/strong>: We&#8217;ve introduced the Bisection Method, an approximation technique used to narrow down solutions efficiently. This method involves dividing the solution space into two parts and iteratively refining the guess to achieve a desired level of accuracy.<\/li>\n\n\n\n<li><strong>Using Floats and &#8216;While&#8217; Loops<\/strong>: We discussed the advantages of using &#8216;floats&#8217; for precise calculations and highlighted the &#8216;while&#8217; loop&#8217;s suitability for visualization of conditionality in a single line.<\/li>\n\n\n\n<li><strong>Efficiency and Trade-offs<\/strong>: We&#8217;ve explored how the Bisection Method offers a trade-off between accuracy and the number of iterations. Increasing the number of iterations improves precision but may require more time.<\/li>\n\n\n\n<li><strong>Application: Finding Roots of Quadratic Equations<\/strong>: To apply these concepts, we solved quadratic equations using the Bisection Method and presented a program to find the roots of a given equation. We discussed the program&#8217;s strengths and identified areas for potential improvement.<\/li>\n<\/ol>\n\n\n\n<p>The chapter has equipped you with foundational knowledge of loops and iterations, introduced the Bisection Method as a powerful approximation technique, and demonstrated practical applications of these concepts. As you continue to explore Python programming, you&#8217;ll have a solid foundation to build upon and adapt these concepts to a wide range of problem-solving scenarios.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a class='vw-button vw-button--purple ' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/19\/1-5-loops-and-conditionality\/' target='_self'>Prev Chapter<\/a> \u00a0 \u00a0<a class='vw-button vw-button-- ' href='https:\/\/idiagress.com\/vinaaypatil\/first-steps-in-python\/' target='_self'>Course Homepage<\/a> \u00a0 \u00a0\u00a0<a class='vw-button vw-button--pink  vw-button--arrow' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/09\/28\/1-7-user-defined-functions-in-python\/' target='_self'>Next Chapter<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding Loops and Iterations in Python Loops are a fundamental concept in programming that allows us to execute a set of instructions repeatedly until a specific condition is met. They are essential for automating repetitive tasks and solving complex problems more efficiently. In Python, loops are a crucial part of the language, and there are &#8230;<\/p>\n","protected":false},"author":2,"featured_media":2766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"[]"},"categories":[412],"tags":[429,426,421,428,427],"class_list":["post-2629","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-first-steps-in-python","tag-algorithm","tag-bisection-method","tag-flowchart","tag-for-loop-in-python","tag-while-loop-in-python"],"_links":{"self":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/comments?post=2629"}],"version-history":[{"count":5,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2629\/revisions"}],"predecessor-version":[{"id":3811,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2629\/revisions\/3811"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media\/2766"}],"wp:attachment":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media?parent=2629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/categories?post=2629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/tags?post=2629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}