{"id":2599,"date":"2020-08-19T18:32:13","date_gmt":"2020-08-19T18:32:13","guid":{"rendered":"https:\/\/idiagress.com\/vinaaypatil\/?p=2599"},"modified":"2023-10-28T09:23:56","modified_gmt":"2023-10-28T09:23:56","slug":"1-5-loops-and-conditionality","status":"publish","type":"post","link":"https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/19\/1-5-loops-and-conditionality\/","title":{"rendered":"1.5 Loops and Conditionality"},"content":{"rendered":"\n<p>Let&#8217;s jump directly into some programming. Using concepts learned so far, let\u2019s create a program to calculate the average height of three students (three_average.py)<\/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>three_average.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># program to calculate average height of three students<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># get input of height<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_a1= <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter height of first student (cm): &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br>_a2= <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter height of second student (cm): &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br>_a3= <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter height of third student (cm): &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br><span class=\"Apple-converted-space\">&nbsp;&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># use the formula for average = Sum of values\/ total number of values<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_avg=(_a1+_a2+_a3)\/(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">3<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># print the output<\/i><br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;The average height is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _avg)<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>At the output we get,<\/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\/08\/output_python_average_three_numbers.png\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"144\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_python_average_three_numbers.png\" alt=\"\" class=\"wp-image-2600\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_python_average_three_numbers.png 665w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_python_average_three_numbers-300x65.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_python_average_three_numbers-335x73.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_python_average_three_numbers-600x130.png 600w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/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>Creating a More Versatile Program<\/strong><\/p>\n\n\n\n<p>While the initial program successfully calculates the average height of three students, it falls short in scenarios involving a larger number of students. Consider a situation with ten students\u2014writing ten input commands for each student&#8217;s height isn&#8217;t practical. Furthermore, creating a new program for each change in the number of students is inefficient.<\/p>\n\n\n\n<p>To address these issues, we need a single program capable of accommodating various scenarios. The key to achieving this flexibility lies in the concept of loops.<\/p>\n\n\n\n<p><strong>Introducing Loops<\/strong><\/p>\n\n\n\n<p>Loops are a fundamental programming structure that enables us to repeat a sequence of instructions multiple times. In essence, they help us automate tasks that need to be executed a certain number of times.<\/p>\n\n\n\n<p>One common and powerful type of loop is the &#8216;for&#8217; loop. Below is a typical syntax for using a &#8216;for&#8217; loop:<\/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: #859905; font-family: Courier; font-size: small;\">for<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _num <\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">in<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: small;\">range<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">5<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">1<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">\u201chello&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<\/span><\/p>\n\n\n\n<p><span class=\"Apple-converted-space\">&nbsp;<\/span>If executed, we will get the output below<\/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\/08\/output_simple_for_loop.png\"><img loading=\"lazy\" decoding=\"async\" width=\"585\" height=\"139\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_simple_for_loop.png\" alt=\"\" class=\"wp-image-2601\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_simple_for_loop.png 585w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_simple_for_loop-300x71.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_simple_for_loop-335x80.png 335w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Let&#8217;s delve into the &#8216;<strong>for<\/strong>&#8216; loop, a fundamental programming construct used for repeating a sequence of instructions. To illustrate this concept, let&#8217;s break down the components of a &#8216;for&#8217; loop used in our example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for _num in range(0, 5, 1):\n    # Code inside the loop<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&#8216;_num&#8217; &#8211; The Variable<\/strong>: In Python, you can use any variable as the loop control variable. Here, we&#8217;ve used &#8216;_num&#8217; as an example, but you can choose a variable name that suits your needs.<\/li>\n\n\n\n<li><strong>The <code>range()<\/code> Function<\/strong>: The <code>range(0, 5, 1)<\/code> defines the behavior of the loop.\n<ul class=\"wp-block-list\">\n<li>&#8216;0&#8217; is the initial value assigned to &#8216;_num.&#8217;<\/li>\n\n\n\n<li>&#8216;5&#8217; is the final or limiting value for the variable. This means the loop will continue as long as &#8216;_num&#8217; is less than &#8216;5.&#8217;<\/li>\n\n\n\n<li>&#8216;1&#8217; is the increment by which &#8216;_num&#8217; will increase its value. With each iteration of the &#8216;for&#8217; loop, &#8216;_num&#8217; will start at 0, increase to 1, then to 2, and so on.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>&#8216;Upper Bound&#8217; Exclusivity<\/strong>: Python follows the &#8216;upper bound&#8217; exclusive rule (Remember Strings and Lists!). This means that while &#8216;5&#8217; is defined as the limiting value, the loop will stop just before &#8216;_num&#8217; reaches it. So, the maximum value &#8216;_num&#8217; will reach is &#8216;4&#8217; (0, 1, 2, 3, 4).<\/li>\n<\/ol>\n\n\n\n<p>What is part of the loop that will get executed?<\/p>\n\n\n\n<p>For example<\/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: #859905; font-family: Courier; font-size: small;\">for<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _num <\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">in<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: small;\">range<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">5<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">1<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;hello&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Namaskar&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;End of program&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<\/span><\/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: #000000; font-family: Iowan Old Style; font-size: medium;\">The two commands, <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\"><b>print<\/b><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><b>(<\/b><\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><b>\u201chello&#8221;<\/b><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><b>) and <\/b><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\"><b>print<\/b><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><b>(<\/b><\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><b>&#8220;Namaskar&#8221;<\/b><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><b>)<\/b><\/span><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\"> <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>are indented below the <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><b>\u2018for\u2019<\/b><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>command, so they are part of the loop and will get repeated <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><b>\u20185\u2019<span class=\"Apple-converted-space\">\u00a0 <\/span><\/b><span class=\"Apple-converted-space\">\u00a0 <\/span>times<span class=\"Apple-converted-space\">\u00a0 <\/span>as the value of<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><b>\u2018_num\u2019<\/b><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>increases <b>(0, 1, 2, 3, 4)<\/b>. <span class=\"Apple-converted-space\">\u00a0 <\/span><\/span><\/p>\n\n\n\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">Whereas <span class=\"Apple-converted-space\">\u00a0 <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\"><b>print<\/b><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><b>(<\/b><\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><b>&#8220;End of program&#8221;<\/b><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)<\/span><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\"> is not part of the loop, hence will be executed only once.<\/span><\/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\/08\/indents_in_for_loop.png\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"262\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/indents_in_for_loop.png\" alt=\"\" class=\"wp-image-2602\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/indents_in_for_loop.png 580w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/indents_in_for_loop-300x136.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/indents_in_for_loop-335x151.png 335w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/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>Using a &#8216;for&#8217; Loop to Calculate Average Height<\/strong><\/p>\n\n\n\n<p>In our program for calculating the average height of students, we can employ a &#8216;for&#8217; loop to streamline the input process. By specifying the upper bound of the loop with the total number of students, we can control the repetition of the &#8216;Input&#8217; command:<\/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: #657b83; font-family: Iowan Old Style; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_total = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">int<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\">&#8220;Enter the number of Students:&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)) <\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\"><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> _num <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _total, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_height = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\">&#8220;Enter height of Student: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If, for example, you enter &#8216;5&#8217; as the number of students, the loop will repeat five times, allowing you to input the heights for each student.<\/p>\n\n\n\n<p>The next step is to calculate the average height. The formula for average height is:<\/p>\n\n\n\n<p><strong>Average height = (Sum of all heights) \/ (Total number of students)<\/strong><\/p>\n\n\n\n<p>To calculate the sum of all heights, we can use the &#8216;_height&#8217; variable, which stores a single height value at any given time. We&#8217;ll utilize the technique of manipulating a variable with itself. To do this, we&#8217;ll define a new variable, &#8216;_sum&#8217;, and continuously add &#8216;_height&#8217; to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_sum = 0  # Initialize _sum to 0\n\nfor _num in range(0, _total, 1):\n    _height = float(input(\"Enter the height of Student: \"))\n    _sum = _sum + _height\n<\/code><\/pre>\n\n\n\n<p>To avoid confusion, we declared the initial value of <span class=\"Apple-converted-space\">\u00a0 <\/span><b><span class=\"Apple-converted-space\">\u00a0 <\/span>\u2018_sum\u2019<\/b><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>to be<span class=\"Apple-converted-space\">\u00a0 <\/span><b><span class=\"Apple-converted-space\">\u00a0 <\/span>\u20180\u2019<\/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: #657b83; font-family: Courier; font-size: small;\">_total = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">int<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\">&#8220;Enter the number of Students:&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br>_sum = <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">0<br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">for<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _num <\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">in<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: small;\">range<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _total, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">1<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_height = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\">&#8220;Enter height of Student: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_sum = _sum + _height<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_avg = _sum\/_total<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #000000; font-family: Courier; font-size: small;\">&#8220;The average height is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _avg) <\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output run of our program is<\/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\/08\/average_using_for_loop.png\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"140\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_using_for_loop.png\" alt=\"\" class=\"wp-image-2603\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_using_for_loop.png 534w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_using_for_loop-300x79.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_using_for_loop-335x88.png 335w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Initializing &#8216;_sum&#8217; to &#8216;0&#8217; helps avoid confusion and ensures that we start with a clean slate for each calculation. As the loop iterates, &#8216;_height&#8217; values will be accumulated in &#8216;_sum&#8217;, which can later be used to calculate the average height.<\/p>\n\n\n\n<p>This systematic approach allows us to efficiently determine the average height of any number of students while simplifying the input and calculation process.<\/p>\n\n\n\n<p>Ideally, the program should have prompted<span class=\"Apple-converted-space\">\u00a0 <\/span>\u2018Enter height of first student:\u2019<span class=\"Apple-converted-space\">\u00a0 <\/span>and so on. Aside from this cosmetic inconsistency, the core of our program works fine.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>So let\u2019s understand this program in a stepwise <strong>algorithm.<\/strong><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\"><b>Step No<\/b><\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\"><b>Command<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\"><b>Interpretation<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\"><b>Parameter Values<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\"><b>Comment<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">1<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_total = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">int<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter the number of Students:&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)) <\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Ask user to input value for total number of students<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3<\/span><\/td><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">2<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_sum = <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">0<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Initialize value for \u2018_sum\u2019 variable<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum = 0<\/span><\/p>\n<\/td><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #859905; font-family: Courier; font-size: small;\">for<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _num <\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">in<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: small;\">range<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _total, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">1<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">):<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">begin \u2018for\u2019 loop<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum = 0<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 0<\/span><\/p>\n<\/td><td>&nbsp;<\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.1<\/span><\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_height = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter height of Student: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Ask user input for height of first student<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum = 0<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 0<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =131<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">within loop<\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.2<\/span><\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_sum = _sum + _height <\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Modify \u2018_sum\u2019 variable<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum=0 +131 =131<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 0<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =131<\/span><\/p>\n<\/td><td>&nbsp;<\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.3<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Loop step executed<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">check if range end is reached <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">(upper &#8211; bound exclusive)<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Is _num \u2265 (_total-1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">True <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">or False<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">0 \u2260 2 (3 &#8211; 1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">FALSE<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">This step is executed internally <\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.4<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">If FALSE go back to step 3 (3.L.1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">increase range variable by step size<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num=_num+1<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum =131<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num =0+1 =1<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =131<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">This step is executed internally <\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.1<\/span><\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_height = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter height of Student: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Ask user input for height of second student<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum = 131<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 1<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =140<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">within loop &#8211; overwrite _height<\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.2<\/span><\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_sum = _sum + _height <\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Modify \u2018_sum\u2019 variable<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum=131+140 =271<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 1<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =140<\/span><\/p>\n<\/td><td>&nbsp;<\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.3<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Loop step executed<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">check if range end is reached <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">(upper &#8211; bound exclusive)<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Is _num \u2265 (_total-1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">True <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">or False<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">1 \u2260 2 (3 &#8211; 1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">FALSE<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">This step is executed internally <\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.4<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">If FALSE go back to step 3 (3.L.1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">increase range variable by step size<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num=_num+1<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum =271<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 1+1 =2<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =140<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">This step is executed internally <\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.1<\/span><\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_height = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter height of Student: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Ask user input for height of Third<span class=\"Apple-converted-space\">&nbsp; <\/span>student<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum = 271<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 2<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =149<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">within loop<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">overwrite<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height<\/span><\/p>\n<\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.2<\/span><\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_sum = _sum + _height <\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Modify \u2018_sum\u2019 variable<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum=271+149 =420<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 2<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =149<\/span><\/p>\n<\/td><td>&nbsp;<\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.3<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Loop step executed<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">check if range end is reached <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">(upper &#8211; bound exclusive)<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">Is _num \u2265 (_total-1)<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">True <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">or False<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">2 = 2<\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">TRUE<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">This step is executed internally <\/span><\/td><\/tr><tr><td>&nbsp;<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">3.L.4<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">If TRUE, End of Loop<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num=_num+1<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum =420<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 2<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =149<\/span><\/p>\n<\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">LOOP END<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">4<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_avg = _sum\/_total <\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">calculate _avg<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_total = 3 <\/span>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_sum =420<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_num = 2<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_height =149<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_avg=420\u00f73=140<\/span><\/p>\n<\/td><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">5<\/span><\/td><td>&nbsp;<\/td><td><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;The average height is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _avg) <\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">print output<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">_avg = 140<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: small;\">END of PROG<\/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>Check the final value of&nbsp;&nbsp;<b>\u2018_num\u2019<\/b>&nbsp;&nbsp;it is&nbsp;&nbsp;<b>\u20182\u2019<\/b>&nbsp; &nbsp;&nbsp;(Upper bound \u2014 1)<\/p>\n\n\n\n<p>Next, the&nbsp; &nbsp;<b>\u2018_height\u2019<\/b>&nbsp;&nbsp;is holding the<b>&nbsp;latest value<\/b>&nbsp;of height. Thus we need a&nbsp; &nbsp;<b>&nbsp;\u2018_sum\u2019<\/b>&nbsp;&nbsp;variable to capture the values as we repeat the loop, and&nbsp;&nbsp;<b>&nbsp;\u2018_height\u2019<\/b>&nbsp;&nbsp;gets&nbsp;<b>overwritten<\/b>.<\/p>\n\n\n\n<p>You can also view the stepwise change in the value of variables easily in \u2018Thonny\u2019<\/p>\n\n\n\n<p>Open Thonny, and as shown below, in&nbsp;<b>the top pane<\/b>, go to&nbsp;&nbsp;<b>&nbsp;\u2018Run\u2019<\/b>&nbsp;&nbsp;options<\/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\/08\/thonny_debug_nicer_option.png\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"566\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_nicer_option.png\" alt=\"\" class=\"wp-image-2604\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_nicer_option.png 852w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_nicer_option-300x199.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_nicer_option-768x510.png 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_nicer_option-335x223.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_nicer_option-600x399.png 600w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span class=\"Apple-converted-space\">&nbsp;<\/span>under which you will find an option to&nbsp;&nbsp;<b>&nbsp;\u2018Debug current script (nicer)\u2019<\/b>&nbsp; &nbsp;click on it &#8211; enter the debug mode.<\/p>\n\n\n\n<p>Once in debug mode, to move from one line of the program to next, in the same \u2018Run\u2019 option, we have the option to&nbsp; &nbsp;<b>\u2018Step into.\u2019<\/b>&nbsp; &nbsp;&nbsp;It is more convenient to use the&nbsp;&nbsp;<b>\u2018F7\u2019<\/b>&nbsp;&nbsp;key as we have to keep on clicking it until the program ends.<\/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\/08\/thonny_debug_step_into_option.png\"><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"510\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_step_into_option.png\" alt=\"\" class=\"wp-image-2605\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_step_into_option.png 846w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_step_into_option-300x181.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_step_into_option-768x463.png 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_step_into_option-335x202.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/thonny_debug_step_into_option-600x362.png 600w\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>As you move through the code stepwise, in the&nbsp;<b>variables window<\/b>, you can see the value of variables change right next to your program window. (In case you are unable to see to Variables window, in the&nbsp;<b>top pane,<\/b>&nbsp;go to \u2018<b>view,\u2019<\/b>&nbsp;in which&nbsp;&nbsp;<b>&nbsp;tick<\/b>&nbsp;on&nbsp;&nbsp;<b>variables window<\/b>)<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<p><iframe loading=\"lazy\" title=\"For Loop (Python) Debugging in Thonny\" width=\"1140\" height=\"641\" src=\"https:\/\/www.youtube.com\/embed\/tehY3ZFVPmE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>A stepwise Algorithm becomes slightly tedious to follow, especially for loops. Hence a graphical flow representation is preferred, defined as&nbsp; &nbsp;<b>\u2018Flowchart.\u2019<\/b><\/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\/08\/for_loop_flowchart.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"951\" height=\"1219\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart.jpg\" alt=\"\" class=\"wp-image-2606\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart.jpg 951w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart-234x300.jpg 234w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart-768x984.jpg 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart-799x1024.jpg 799w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart-261x335.jpg 261w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/for_loop_flowchart-600x769.jpg 600w\" sizes=\"auto, (max-width: 951px) 100vw, 951px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The average program is not storing individual heights values as the loop overwrites the &#8216;<b>_height&#8217;&nbsp;<\/b>variable at every execution. In case we need to recall the height of a particular student, we will need to add a few more lines to our code.<\/p>\n\n\n\n<p>Firstly, we will need to store all heights. A great way of storing all the heights together will be a &#8216;<b>list.&#8217;<\/b><\/p>\n\n\n\n<p>Secondly, we can use&#8217; dictionaries&#8217; for associating the name of a student with the<b>&nbsp;corresponding<\/b>&nbsp;height.<\/p>\n\n\n\n<p>We need to do all this before we overwrite the &#8216;<b>_height.&#8217;&nbsp;<\/b>So it should be a part of the loop. Plus, we need the lists to be&nbsp;<b>&#8216;dynamic.&#8217;<\/b>&nbsp;We mean by &#8216;dynamic&#8217; that the number of members in a list needs to be flexible.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Enhancing the Average Height Program with Lists and Dictionaries<\/strong><\/h3>\n\n\n\n<p>The original average height program doesn&#8217;t store individual height values as the &#8216;_height&#8217; variable is overwritten with each iteration of the loop. To recall the height of a specific student, we&#8217;ll make a few modifications.<\/p>\n\n\n\n<p><strong>Storing Individual Heights<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We&#8217;ll use a &#8216;list&#8217; to efficiently store all the heights together.<\/li>\n\n\n\n<li>Lists offer dynamic sizing, adapting to the number of students. We can use the &#8216;append()&#8217; function to add new members to a list.\n<ul class=\"wp-block-list\">\n<li>Example: <code>_items = [23, 41, 5, 100]<\/code>\n<ul class=\"wp-block-list\">\n<li><code>_items.append(77)<\/code> will increase the list size dynamically to <code>[23, 41, 5, 100, 77]<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Associating Students with Heights<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We&#8217;ll use &#8216;dictionaries&#8217; to associate each student&#8217;s name with their corresponding height.<\/li>\n\n\n\n<li>Dictionaries don&#8217;t have a specific order; they pair keys with values.\n<ul class=\"wp-block-list\">\n<li>Example: <code>_months = {1: \"Jan\", 2: \"Feb\"}<\/code>\n<ul class=\"wp-block-list\">\n<li><code>_months[3] = \"Mar\"<\/code> will update the dictionary to <code>{1: \"Jan\", 2: \"Feb\", 3: \"Mar\"}<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>To implement these enhancements, we&#8217;ll integrate lists and dictionaries into our program, creating a new script called &#8216;avg_with_storage.py.&#8217;<\/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\/08\/avg_with_lists_dictionaries_program.png\"><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"664\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/avg_with_lists_dictionaries_program.png\" alt=\"\" class=\"wp-image-2607\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/avg_with_lists_dictionaries_program.png 701w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/avg_with_lists_dictionaries_program-300x284.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/avg_with_lists_dictionaries_program-335x317.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/avg_with_lists_dictionaries_program-600x568.png 600w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/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 output is as below<\/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\/08\/output_avg_list_dictionaries.png\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"241\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_avg_list_dictionaries.png\" alt=\"\" class=\"wp-image-2608\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_avg_list_dictionaries.png 859w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_avg_list_dictionaries-300x84.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_avg_list_dictionaries-768x215.png 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_avg_list_dictionaries-335x94.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_avg_list_dictionaries-600x168.png 600w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Conditionality<\/b><\/h2>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>Branching<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">As seen in the flowchart, the<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>&#8216;for&#8217; loop&#8217;s<\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>execution includes a &#8216;condition&#8217; check. This check can be expressed in detail as follows<\/span>\n<p><span style=\"color: #859905; font-family: Courier; font-size: medium;\">for<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> _num <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">in<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">range<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">5<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, <\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">):<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">&nbsp; &nbsp; &nbsp;Is _num \u2265<span class=\"Apple-converted-space\">&nbsp; <\/span>5 ?<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">&nbsp; &nbsp; &nbsp;NO : Execute statements within<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018for\u2019 loop<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">&nbsp; &nbsp; &nbsp;YES: Exit the <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018for\u2019 loop<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">The same logic can we written in a slightly different format \u2014<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">&nbsp; &nbsp; &nbsp;If _num \u2265 5 <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>then execute the commands<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">&nbsp; &nbsp; &nbsp;else<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>exit <\/span><\/p>\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: medium;\">The <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018If\u2026..else\u2026..\u2019 <\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>statements come under a category called as <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018Conditionality\u2019 <\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>statements. Technically they introduce <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018branching\u2019<\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>in the code.<\/span><\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\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\/08\/branching.png\"><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"292\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/branching.png\" alt=\"\" class=\"wp-image-2609\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/branching.png 583w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/branching-300x150.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/branching-335x168.png 335w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><\/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>Comparing Student Heights with the Average<\/strong><\/p>\n\n\n\n<p>To enhance our average height program, we can introduce branching to provide different output based on certain conditions. For instance, we can prompt the user to enter a student&#8217;s name and then determine whether that student has an above-average height, below-average height, or an average height.<\/p>\n\n\n\n<p>To implement this feature, we&#8217;ll add &#8216;if&#8217; and &#8216;elif&#8217; (else if) statements to our code:<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>avg_with_storage.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># ask for name of student for comparison<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_compare = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;Enter name of student for comparison: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># extract value of height from dictionary<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_cheight = _students[_compare]<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># if else branching to compare height with Average<\/i><br><\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">if<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _cheight &gt; _avg:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(_compare,<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;s height is more than average&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br><\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">elif<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _cheight &lt; _avg:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(_cheight,<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;s height is less than average&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br><\/span><span style=\"color: #859905; font-family: Courier; font-size: small;\">elif<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\"> _cheight == _avg: <\/span>\n<p><span style=\"color: #657b83; font-family: Courier; font-size: small;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: small;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">(_compare,<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: small;\">&#8220;s height is same as the average\u201d<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<\/span><\/p>\n<\/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 after adding the code to<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018avg_with_storage.py\u2019<\/b><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>and creating a new file<span class=\"Apple-converted-space\">&nbsp; <\/span>(<b>compare_average.py<\/b>)<span class=\"Apple-converted-space\">&nbsp;<\/span><\/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\/08\/average_compare_if_branching_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"284\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_compare_if_branching_python.png\" alt=\"\" class=\"wp-image-2610\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_compare_if_branching_python.png 842w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_compare_if_branching_python-300x101.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_compare_if_branching_python-768x259.png 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_compare_if_branching_python-335x113.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/average_compare_if_branching_python-600x202.png 600w\" sizes=\"auto, (max-width: 842px) 100vw, 842px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span class=\"Apple-converted-space\"> <\/span>In the &#8216;elif&#8217; command for checking if <code>_cheight<\/code> is equal to <code>_avg<\/code>, the symbol used for &#8220;equal to&#8221; is &#8216;==&#8217;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>elif \u00a0 _cheight == _avg:<\/code><\/pre>\n\n\n\n<p>For the other comparison operations the symbols are as below \u2014<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\"><b>No<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\"><b>Description<\/b><\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\"><b>Syntax<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">1<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A<span class=\"Apple-converted-space\">&nbsp; <\/span>Greater than B<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A &gt; B<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">2<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A Less than B<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A &lt; B<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">3<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A Greater than or equal to B<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A &gt;= B<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">4<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A Less than or equal to B<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A &lt;= B<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">5<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A Equal to B<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A == B<\/span><\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">6<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A Not equal to B<\/span><\/td><td><span style=\"color: #000000; font-family: Avenir; font-size: large;\">A ! = B<\/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<h4 class=\"wp-block-heading\">Summary<\/h4>\n\n\n\n<p>This chapter introduces the concept of loops and conditional statements in programming. It focuses on the creation and enhancement of a program for calculating the average height of students, while also providing explanations of the key programming elements introduced.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Introduction to Loops and &#8216;for&#8217; Loop:<\/strong> The chapter begins by introducing the &#8216;for&#8217; loop, a fundamental programming structure that repeats a sequence of instructions until a specific condition is met. It explains the syntax of the &#8216;for&#8217; loop and demonstrates how it can be used to repeat a task a certain number of times, making it efficient for scenarios involving multiple data points.<\/li>\n\n\n\n<li><strong>Stepwise Algorithm Explanation:<\/strong> The chapter provides a stepwise algorithm for the average height program, highlighting the input of student heights and the calculation of the average height within the &#8216;for&#8217; loop. It also explains the concept of the upper bound exclusive rule in Python.<\/li>\n\n\n\n<li><strong>Enhancement with Lists and Dictionaries:<\/strong> The text emphasizes the need to store individual height values and introduces &#8216;lists&#8217; and &#8216;dictionaries&#8217; as powerful data structures. Lists are employed to dynamically store all heights, while dictionaries are used to associate students&#8217; names with their heights. This ensures data integrity and flexibility as the number of students can change.<\/li>\n\n\n\n<li><strong>Conditional Statements and Branching:<\/strong> The chapter demonstrates the use of conditional statements (if and elif) to compare individual student heights with the calculated average. It explains the syntax of these statements and provides a reference for comparison operators, showing how to determine whether a student&#8217;s height is above, below, or equal to the average.<\/li>\n\n\n\n<li><strong>Creating a Comprehensive Program:<\/strong> Finally, the chapter combines all the elements to create a comprehensive program for calculating the average height, storing individual heights, and comparing heights. The code is organized into separate scripts, making it modular and easy to manage.<\/li>\n<\/ol>\n\n\n\n<p>In summary, this chapter provides a foundational understanding of loops, conditional statements, data structures, and how to use them to solve practical problems in programming. It demonstrates a step-by-step approach to creating programs, making them more versatile and informative.<\/p>\n\n\n\n<p><i>Technical Note: The functions that we used for strings, lists, like&nbsp;<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/i><b><i>capitalize( ), append( ),&nbsp;<\/i><\/b><i>etc., <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>are technically referred to as <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>&#8216;<\/i><b><i>methods.&#8217;<\/i><\/b><i>&nbsp; &nbsp; <span class=\"Apple-converted-space\">&nbsp; <\/span>Because, later on, we will see that python allows us to create some &#8216;user-defined functions.&#8217; Hence, for clarity, the<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>&#8216;<\/i><b><i>method&#8217;&nbsp; <span class=\"Apple-converted-space\">&nbsp; <\/span><\/i><\/b><i>terminology is used.<\/i><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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 style=\"text-align: left;\">Prompt the user to enter the name of a student and his score in English on a scale of 100. Proceed towards calculating the average of the class. The grade is assigned as per below formula.<\/li>\n<\/ul>\n<p style=\"text-align: left;\">Score above average = A Grade<\/p>\n<p style=\"text-align: left;\">Average score = B Grade<\/p>\n<p style=\"text-align: left;\">Score below average = C Grade<\/p>\n<p style=\"text-align: left;\">Prompt user for a name of the student, and return the calculated grade for that student.<\/p>\n<ul>\n<li style=\"text-align: left;\">Prompt the user to define the distance on a race track. Ask for the number of drivers on the track. Prompt for the name of the driver, and the time in minutes taken to complete the race. Calculate the speed of each driver in m\/min, and display each drivers speed.<\/li>\n<li style=\"text-align: left;\">Ask the user for input on the account balance of a bank client. also ask for the input on his outstanding loan. Compare the account balance and the outstanding to return whether the client is in default or not.<\/li>\n<li style=\"text-align: left;\">In problem no 2, prompt for the names of the two drivers, determine who is the faster one amongst the two, and also display the percentage amount by which one driver is faster than the other.<\/li>\n<li style=\"text-align: left;\">Create a program to compare the number of letters in the names of two people and return whose name is longer than the other person.<\/li>\n<\/ul>\n<p><\/div><\/div><\/div>\n\n\n\n<p><a class='vw-button vw-button--purple ' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/17\/1-4-strings-lists-and-directories\/' target='_self'>Prev Chapter<\/a> &nbsp; <a class='vw-button vw-button-- ' href='https:\/\/idiagress.com\/vinaaypatil\/first-steps-in-python\/' target='_self'>Course Homepage<\/a> &nbsp; &nbsp;<a class='vw-button vw-button--pink  vw-button--arrow' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/09\/04\/1-6-loops-and-iterations\/' target='_self'>Next Chapter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s jump directly into some programming. Using concepts learned so far, let\u2019s create a program to calculate the average height of three students (three_average.py) three_average.py# program to calculate average height of three students&nbsp; # get input of height_a1= float(input(&#8220;Enter height of first student (cm): &#8220;))_a2= float(input(&#8220;Enter height of second student (cm): &#8220;))_a3= float(input(&#8220;Enter height of &#8230;<\/p>\n","protected":false},"author":2,"featured_media":2764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[412],"tags":[417,419,421,420,418],"class_list":["post-2599","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-first-steps-in-python","tag-branching-in-python","tag-dictionaries-in-python","tag-flowchart","tag-if-else-in-python","tag-loops-in-python"],"_links":{"self":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2599","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=2599"}],"version-history":[{"count":5,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2599\/revisions"}],"predecessor-version":[{"id":3743,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2599\/revisions\/3743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media\/2764"}],"wp:attachment":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media?parent=2599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/categories?post=2599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/tags?post=2599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}