{"id":2534,"date":"2020-08-09T17:24:04","date_gmt":"2020-08-09T17:24:04","guid":{"rendered":"https:\/\/idiagress.com\/vinaaypatil\/?p=2534"},"modified":"2023-10-28T07:24:36","modified_gmt":"2023-10-28T07:24:36","slug":"1-3-linear-equations-using-python-algorithms","status":"publish","type":"post","link":"https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/09\/1-3-linear-equations-using-python-algorithms\/","title":{"rendered":"1.3 Linear Equations using Python | Algorithms"},"content":{"rendered":"\n<p>In the previous chapter, we explored different data types and carried out algebraic operations. We created a simple Python program to calculate the area of a circle with the radius as an input. Now, let&#8217;s put these concepts into practice by solving another problem \u2013 the problem of ages.<\/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>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #000000; font-family: Courier; font-size: small;\"><i>Amita and Madhur are two sisters. The sum of their ages is 43, and difference of their ages is 3. If Amita is the elder sister, what is her age?<\/i><\/span> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">This is a simple problem of Linear equations,<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; Let Amita\u2019s age be &nbsp; &nbsp;a<\/span><\/p><p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; Let Madhur\u2019s age be &nbsp; b<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; Sum of their ages &nbsp; &nbsp; a+b = 43 &nbsp; &nbsp;eq1<\/span><\/p><p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; Difference of ages &nbsp; &nbsp;a &#8211; b = 3 &nbsp; eq2<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eq1 + eq2<span class=\"Apple-converted-space\">&nbsp; <\/span>\u2014&gt; <span class=\"Apple-converted-space\">&nbsp; <\/span>2a=46,<span class=\"Apple-converted-space\">&nbsp; <\/span><\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">therefore, <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/span>a = 46\u00f72 = 23<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\"> <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/span>b =43 &#8211; a = 43 &#8211; 23 = 20<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\"> we can re-write the solution as a formula<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; &nbsp;a = (eq1 + eq2) \u00f7 2<\/span><\/p> <p><span style=\"color: #000000; font-family: Courier; font-size: small;\">&nbsp; &nbsp; &nbsp;b = eq1 &#8211; a<\/span><\/p> <\/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, lets create a python program for this:<span class=\"Apple-converted-space\">&nbsp; <\/span>\u2018ages.py\u2019<\/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>ages.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: small;\"><i># _a1 #Amita&#8217;s age<\/i><br><i># _b1 #Madhur&#8217;s ages<\/i><br><i># _eq1 #Sum of their ages<\/i><br><i># _eq2 #Difference ot their ages<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">_eq1 = <\/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;\">&#8216;Enter the sum of the ages of Amita and Madhur &#8216;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br>_eq2 = <\/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;\">&#8216;Enter the difference of the ages of Amita and Madhur &#8216;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">))<br>_a1 = ((_eq1+_eq2) \/ <\/span><span style=\"color: #d33682; font-family: Courier; font-size: small;\">2<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">)<br>_b1 = (_eq1-_a1)<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;\">&#8216;Amita age is &#8216;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _a1)<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;\">&#8216;Madhur age is &#8216;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: small;\">, _b1)<\/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>and the output of the above program is, <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\/ages_python_program_output_idiagress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"124\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_output_idiagress.png\" alt=\"\" class=\"wp-image-2535\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_output_idiagress.png 645w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_output_idiagress-300x58.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_output_idiagress-335x64.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_output_idiagress-600x115.png 600w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/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 data-preserver-spaces=\"true\">The variables are <\/span><strong><span data-preserver-spaces=\"true\">_a1, _b1, _eq1, _eq2,<\/span><\/strong><span data-preserver-spaces=\"true\"> virtually by force of habit, I am using underscores in variable names; even if you use straight forward&nbsp; &nbsp; &nbsp;&nbsp;<\/span><strong><span data-preserver-spaces=\"true\">a, b, eq1, eq2<\/span><\/strong><span data-preserver-spaces=\"true\">&nbsp; &nbsp; &nbsp; &nbsp;it will be sufficient.&nbsp;<\/span><\/p>\n\n\n\n<p><span data-preserver-spaces=\"true\">Now, consider a scenario wherein my objectives from the program are changed. I want to make a program whose output is similar to a tutorial for solving the problem, in a stepwise format illustrated above. (ages_with_steps.py)<\/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\/ages_python_program_with_steps.png\"><img loading=\"lazy\" decoding=\"async\" width=\"887\" height=\"460\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_with_steps.png\" alt=\"\" class=\"wp-image-2536\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_with_steps.png 887w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_with_steps-300x156.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_with_steps-768x398.png 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_with_steps-335x174.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/ages_python_program_with_steps-600x311.png 600w\" sizes=\"auto, (max-width: 887px) 100vw, 887px\" \/><\/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 answer is the same, only the visual aspect of presenting the output has changed depending on our audience.<span class=\"Apple-converted-space\">&nbsp;<\/span>This is what we had discussed previously as making the code more &#8216;readable&#8217;. As we can see from the output below, we can identify the steps taken in solving the problem<\/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_ages_python_program_with_steps.png\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"311\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_ages_python_program_with_steps.png\" alt=\"\" class=\"wp-image-2537\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_ages_python_program_with_steps.png 629w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_ages_python_program_with_steps-300x148.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_ages_python_program_with_steps-335x166.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_ages_python_program_with_steps-600x297.png 600w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><i>Now, run the program again for a sum of ages as 59, and difference of 5.<\/i><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Some observations from the output show that to create spaces, we use empty quotes<span class=\"Apple-converted-space\">&nbsp; <\/span>\u2018<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>\u2019.<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>But, the output is lacking some basics of grammar.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<p>Like instead of<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span>&#8216;Amita&#8217;s age is&#8217; <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>we are printing<span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>\u2018Amita age is.&#8217;<span class=\"Apple-converted-space\">&nbsp; &nbsp;<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The syntax of python is coming in the way of grammar because if we use single quotes in the print command, another intermediate quote will break the string.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<p>In &#8216;Amita&#8217;s age is&#8217; the string will break at<span class=\"Apple-converted-space\">&nbsp; <\/span>Amita, and the rest of the string might go, or we might get an error.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<p>Hence, the print command allows usage of double quotes <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; <\/span>\u201c<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u201d.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<p>In double quotes, the single quote will be considered another character, a part of the string.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>print<\/b>(\"Amita's age is\", _a1)<\/pre>\n\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>To explore further let us try out an another problem of linear equations.<\/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\/linear_equation_math_solution.png\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"834\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/linear_equation_math_solution.png\" alt=\"\" class=\"wp-image-2691\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/linear_equation_math_solution.png 653w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/linear_equation_math_solution-235x300.png 235w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/linear_equation_math_solution-262x335.png 262w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/linear_equation_math_solution-600x766.png 600w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Similar to ages problem, we can convert the solution&#8217;s step wise process into a python program \u2014 \u2018<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">pens_pencils.py<\/mark>&#8216;<\/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\/python_program_linear_equations.png\"><img loading=\"lazy\" decoding=\"async\" width=\"871\" height=\"556\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_linear_equations.png\" alt=\"\" class=\"wp-image-2538\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_linear_equations.png 871w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_linear_equations-300x192.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_linear_equations-768x490.png 768w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_linear_equations-335x214.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_linear_equations-600x383.png 600w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Quite a messy program to view; especially line no 24! However, if you check each step, it will correspond to an operation. The program is quite elaborate, detailing every step of the algebraic solution in 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_linear_equation_python_program.png\"><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"343\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_linear_equation_python_program.png\" alt=\"\" class=\"wp-image-2539\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_linear_equation_python_program.png 589w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_linear_equation_python_program-300x175.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_linear_equation_python_program-335x195.png 335w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><i>I have completed the program till you get the value for the pen, as an exercise complete the&nbsp;remainder the &nbsp;program, for calculating the value of pencil.<\/i><\/p>\n\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>In the &#8216;<strong>ages<\/strong>&#8216; problem, we first created a program that captured the essence of the solution. Later, we developed an expanded program that detailed the steps of solving the problem. Now, for the &#8216;<strong>pens and pencils<\/strong>&#8216; problem, we initially created an expanded program. Let&#8217;s work in reverse and create a program that captures the solution&#8217;s essence.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/essence_of_linear_equation_math_solution.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2692\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/essence_of_linear_equation_math_solution.png\" alt=\"\" width=\"657\" height=\"716\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/essence_of_linear_equation_math_solution.png 657w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/essence_of_linear_equation_math_solution-275x300.png 275w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/essence_of_linear_equation_math_solution-307x335.png 307w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/essence_of_linear_equation_math_solution-600x654.png 600w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/a><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We will now create a python program which will capture this generic version for any linear simultaneous equation:<\/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\/python_program_essence_linear_equations.png\"><img loading=\"lazy\" decoding=\"async\" width=\"519\" height=\"458\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_essence_linear_equations.png\" alt=\"\" class=\"wp-image-2541\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_essence_linear_equations.png 519w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_essence_linear_equations-300x265.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/python_program_essence_linear_equations-335x296.png 335w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We&#8217;ll name this program &#8216;<strong>essence_linear_eq.py.&#8217;<\/strong> The introduction of intermediate variables, &#8216;_numerator&#8217; and &#8216;_denominator,&#8217; not only makes our code cleaner but also helps us track potential mistakes, reducing confusion. This is in contrast to our previous program, where an elaborate &#8216;print&#8217; command could lead to errors due to a single missing bracket.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">print(\u2018cost of one pen is\u2019,((_p1*_do2)-(_p2*_co2))\/((_co1*_do2)-(_do1*_co2)))<\/mark><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>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\/08\/output_essence_of_linear_equations.png\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"208\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_essence_of_linear_equations.png\" alt=\"\" class=\"wp-image-2693\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_essence_of_linear_equations.png 600w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_essence_of_linear_equations-300x104.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/output_essence_of_linear_equations-335x116.png 335w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/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 generalized program, we can write the logic of our program in a step wise format as below:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><b>Step No<\/b><\/td><td><b>Step Description<span class=\"Apple-converted-space\">&nbsp;<\/span><\/b><\/td><td><b>Symbolical Step Presentation<\/b><\/td><\/tr><tr><td>1<\/td><td>Begin program to solve generic linear simultaneous equations<\/td><td>Pa + Qb = R\n<p>Ua + Vb =W<\/p>\n<\/td><\/tr><tr><td>2<\/td><td>Get Inputs for equation<\/td><td>Get, P, Q, R, U, V and W<\/td><\/tr><tr><td>3<\/td><td>Solve for Intermediate variables<\/td><td>Numerator = (R\u2022V) &#8211; (W\u2022Q),\n<p>Denominator is (P\u2022V) &#8211; (Q\u2022U)<\/p>\n<\/td><\/tr><tr><td>4<\/td><td>Calculate final solution<\/td><td>a=Numerator\/Denominator\n<p>b = (R &#8211; Pa)\/Q<\/p>\n<\/td><\/tr><tr><td>5<\/td><td>Print the final output<\/td><td>Out, a, and b.<\/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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Algorithm<\/h2>\n\n\n\n<p>When we create a step-by-step detailing of the program&#8217;s logic, it is technically referred to as an &#8216;algorithm.&#8217; Algorithms serve as a fundamental component of programming, outlining a structured and systematic approach to problem-solving.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Thus our &#8216;<strong>essence_linear_eq.py<\/strong>&#8216; program captures the core essence or in other words the &#8216;algorithm&#8217; of solving a problem of simultaneous equations. <\/h4>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\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<ol>\n<li>Create an algorithm to solve a generic quadratic equation, Ax2 + By + C =0<\/li>\n<li>Use the algorithm and create a program to find the roots of a quadratic equation, x2 +2x +1 =0. Use hand calculations to verify your answer.<\/li>\n<li>Use the program created in step 2, to also solve for , x2 +5x +6 =0.<\/li>\n<li>Find the roots of the equation 2&#215;2 \u2013 5x + 3 = 0<\/li>\n<li>The product of Jai&#8217;s age (in years) two years ago and her age four years from now, is one more than twice her present age. What is her present age? (<i>Jai&#8217;s present age \u2014 2) \u2022 (Jai\u2019s present age + 4) = 2 * (Jai\u2019s present age) + 1<\/i><\/li>\n<\/ol>\n<p><i>{Hint, use the quadratic formula for getting the roots, and for finding square root, use the exponential operator, raise to 0.5, (25 ** 0.5 =5)}<\/i><\/p>\n<p><\/div><\/div><\/div>\n\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\">Underscore in Variable<\/h2>\n\n\n\n<p>Variable names in Python can begin with letters, but using a variable name that matches a built-in Python command or data type can lead to confusion. <\/p>\n\n\n\n<p>For instance, defining a variable as &#8220;int = 14&#8221; can be problematic because it can get mixed up with the data type &#8216;int()&#8217; used to declare integers. To avoid such confusion, it&#8217;s a good practice to use underscores (&#8216;_&#8217;) in variable names. Text editors like Atom often provide better visibility and organization for variables when they start with an underscore.<\/p>\n\n\n\n<p>While all the programs in this chapter could work with integers instead of floats, it&#8217;s worth considering the choice of data type based on the specific requirements of the program. In general, using &#8216;floats&#8217; is recommended when mathematical operations involve division or roots. This is because using integers in such calculations may lead to the truncation of decimal parts, potentially leading to inaccurate results.<\/p>\n\n\n\n<p>For example, if we run the following program:<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">_a = 32 \n_b = 5 \n_c = int(_a\/_b) \nprint(_c)<\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output we will get is<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>6<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>whereas the actual calculation yields 32\u00f75 = 6.4 the decimal, 0.4 will be ignored in an&nbsp; &nbsp;<b>\u2018int\u2019&nbsp;<\/b>&nbsp;&nbsp;data type.<\/p>\n\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\">Summary<\/h2>\n\n\n\n<p>We put our knowledge to practice by solving the &#8220;<strong>ages<\/strong>&#8221; problem, which is a simple case of linear equations. The program &#8216;<strong>ages.py<\/strong>&#8216; is designed to achieve this. It takes the sum and difference of ages as input and calculates individual ages using the equations. <\/p>\n\n\n\n<p>We also observed that variable names could use underscores (&#8216;_&#8217;) for better visibility and organization. However, straightforward names like &#8216;a&#8217; and &#8216;b&#8217; would suffice.<\/p>\n\n\n\n<p>To make our output more informative and instructional, we created &#8216;ages_with_steps.py.&#8217; This program showcases the steps involved in solving the problem, creating a stepwise tutorial-like output for better understanding.<\/p>\n\n\n\n<p>The concept of using double quotes in the &#8216;print&#8217; command to handle single quotes in strings was introduced. We saw that double quotes allow for more accurate grammar usage.<\/p>\n\n\n\n<p>Then, we explored the &#8220;<strong>pens and pencils<\/strong>&#8221; problem, which also involves solving a system of linear equations. A program, &#8216;<strong>pens_pencils.py<\/strong>,&#8217; was developed to solve this problem, and it provided a detailed output, illustrating each step of the algebraic solution.<\/p>\n\n\n\n<p>Subsequently, we discussed the essence of the solution and its implementation in &#8216;<strong>essence_linear_eq.py.<\/strong>&#8216; This program captures the core logic of solving generic linear simultaneous equations and presents it in a structured stepwise format. We introduced the concept of an algorithm, which forms the foundation of programming.<\/p>\n\n\n\n<p>In the exercises, you were given challenges related to linear and quadratic equations, encouraging you to use algorithms to find solutions. The importance of variable names and the choice of data types, specifically the preference for &#8216;floats&#8217; when mathematical operations involve division or roots, was highlighted.<\/p>\n\n\n\n<p>In summary, this chapter expanded our understanding of linear equations, problem-solving techniques, and the use of algorithms. We practiced writing programs to solve real-world problems, making our code more readable, and addressing issues related to variable names and data types. These skills will be valuable as we continue our journey into programming.<\/p>\n\n\n\n<p><a class='vw-button vw-button--purple ' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/08\/1-2-objects-and-variable\/' target='_self'>Prev Chapter<\/a> &nbsp; &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--  vw-button--arrow' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/17\/1-4-strings-lists-and-directories\/' target='_self'>Next Chapter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous chapter, we explored different data types and carried out algebraic operations. We created a simple Python program to calculate the area of a circle with the radius as an input. Now, let&#8217;s put these concepts into practice by solving another problem \u2013 the problem of ages. &nbsp;Amita and Madhur are two sisters. &#8230;<\/p>\n","protected":false},"author":2,"featured_media":2760,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[412],"tags":[407,411,394,410,406],"class_list":["post-2534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-first-steps-in-python","tag-floats-in-python","tag-linear-equations","tag-python","tag-simultaneous-equations","tag-variables-in-python"],"_links":{"self":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2534","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=2534"}],"version-history":[{"count":5,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2534\/revisions"}],"predecessor-version":[{"id":3733,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2534\/revisions\/3733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media\/2760"}],"wp:attachment":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media?parent=2534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/categories?post=2534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/tags?post=2534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}