{"id":863,"date":"2010-11-05T15:58:45","date_gmt":"2010-11-05T15:58:45","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2010-11-05T15:58:45","modified_gmt":"2010-11-05T15:58:45","slug":"7b-math","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/7b-math\/","title":{"rendered":"7B: Math"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: Graeme Kemkes, gdkemkes@alumni.uwaterloo.ca, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p><em>Lesson 7 has three parts A, B, C which can be completed in any order.<\/em><\/p>\n<p>So far, we have performed math calculations using Python's operators\u00a0<code>+<\/code>, \u00a0<code>-<\/code>, <code>*<\/code>,\u00a0<code>\/<\/code> and the functions <code>max<\/code> and\u00a0<code>min<\/code>. In this lesson we will see some more operators and functions and learn how to perform more complex calculations.<\/p>\n<h2>Math Operators<\/h2>\n<p>We have already seen how to use operators for addition (<code>a + b<\/code>), subtraction (<code>a - b<\/code>), multiplication (<code>a * b<\/code>) and division (<code>a \/ b<\/code>). We will now learn about three additional operators.<\/p>\n<ul>\n<li>The <strong>power<\/strong> operator <code>a ** b<\/code> computes <code>a<sup>b<\/sup><\/code> (<code>a<\/code>\u00a0multiplied by itself <code>b<\/code> times). For example, <code>2 ** 3<\/code> produces\u00a0<code>8<\/code>\u00a0(which is 2\u00d72\u00d72).<\/li>\n<li>The <strong>integer division<\/strong> operator <code>a \/\/ b<\/code> computes the \"quotient\" of <code>a<\/code> divided by <code>b<\/code> and ignores the remainder. For example, <code>14 \/\/ 3<\/code> produces <code>4<\/code>.<\/li>\n<li>The <strong>modulus<\/strong> operator <code>a % b<\/code> computes the remainder when <code>a<\/code> is divided by <code>b<\/code>. For example, <code>14 % 3<\/code> produces <code>2<\/code>.<\/li>\n<\/ul>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox0'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>The power, integer division, and modulus operators<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nprint(2 ** 3)\nprint(14 \/\/ 3)\nprint(14 % 3)<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"a3511b91883033660b56dabaf0837468\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit0\").value = \"Run program\";document.getElementById(\"inputInUse0\").value = \"N\";<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox1'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Eggsactly<\/span><\/div>Egg cartons each hold exactly 12 eggs. Write a program which reads an integer number of eggs from <code>input()<\/code>, then prints out two numbers: how many cartons can be filled by these eggs, and how many eggs will be left over. For example, the output corresponding to <code>27<\/code> eggs is <\/p>\n<pre>2<br\/>3<\/pre> since <code>27<\/code> eggs fill <code>2<\/code> cartons, leaving <code>3<\/code> eggs left over. <a class=\"hintlink\"  id=\"hintlink2\">Hint<\/a>  <div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput1\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch1\" value=\"Input Switch\" onclick=\"pbInputSwitch(1,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(1)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(1,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect1' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(1,'7b.eggs')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(1);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"92f7490555a7d3bfb78ae267eb295628\"\/>\n<div id='pbresults1' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(1);});pbInputSwitch(1,\"N\");<\/script>\n<\/p>\n<p>The modulus operator is used for a variety of tasks. It can be used to answer questions like these ones:<\/p>\n<ul>\n<li>If the time now is 10 o'clock, what will be the time 100 hours from now? (requires modulus by 12)<\/li>\n<li>Will the year 2032 be a leap year? (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Leap_year\">requires modulus by 4, 100, and 400<\/a>)<\/li>\n<\/ul>\n<p>Checking leap years is an example of divisibility testing; in the next exercise we ask you to write a program that performs divisibility testing in general.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox3'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Divisibility<\/span><\/div>Write a program that reads two positive integers <code>a<\/code> and <code>b<\/code> on separate lines. If <code>a<\/code> is divisible by <code>b<\/code>, print the message \"divisible\". Otherwise, print the message \"not divisible\". For example, when the input is <\/p>\n<pre>14<br\/>3<\/pre> the program should print \"not divisible\". <a class=\"hintlink\"  id=\"hintlink4\">Hint<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput3\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch3\" value=\"Input Switch\" onclick=\"pbInputSwitch(3,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect3' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(3,'7b.divisible')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(3);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"3fb16a1c27b663c5a2e818fd079395c1\"\/>\n<div id='pbresults3' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(3);});pbInputSwitch(3,\"N\");<\/script>\n<\/p>\n<h2>Math Functions<\/h2>\n<p>Python can compute most of the mathematical functions found on a scientific calculator.<\/p>\n<ul>\n<li><code>sqrt(x)<\/code> computes the square root of the number <code>x<\/code>.<\/li>\n<li><code>exp(x)<\/code> and <code>log(x)<\/code> are the exponential and natural logarithmic functions.<\/li>\n<li><code>sin(x)<\/code>, <code>cos(x)<\/code>, <code>tan(x)<\/code> and other trigonometric functions are available.<\/li>\n<li><code>pi<\/code>, the mathematical constant <code>3.1415...<\/code>, \u00a0is also included.<\/li>\n<\/ul>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> When using Python's trigonometric functions, the angle <code>x<\/code> must be expressed in radians, not degrees. <\/span><\/td><\/table><\/p>\n<p>Python includes such a large number of functions that they are organized into groups called <em>modules<\/em>. The above functions belong to the <code>math<\/code> module. Before using any functions from a module, you must <em>import<\/em> the module as shown in the example below. To use a function from a module you must type the module name, followed by a period, followed by the name of the function.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox5'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Using functions from the math module<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nimport math\nprint(math.pi)\ne = math.exp(1)\nprint(e)\nprint(math.sqrt(3), math.tan(math.pi \/ 3))<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(5)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(5,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"f6885f61429ba7ceee96a03033a06e45\"\/>\n<div id='pbresults5' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox6'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Pizza Circles<\/span><\/div>Your friends have eaten their square pizzas and are now ordering a round pizza. Write a program to calculate the area of this circular pizza. The input is a <code>float<\/code> <em>r<\/em>, which represents the radius in cm. The output should be the area in cm<sup>2<\/sup>, calculated using the formula \u00a0<em>A=pi*r<sup>2<\/sup><\/em>. Use Python's <code>pi<\/code> feature instead of typing <code>3.1415...<\/code><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput6\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch6\" value=\"Input Switch\" onclick=\"pbInputSwitch(6,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(6)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(6,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect6' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(6,'7b.pizza')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(6);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"4d9be21907c4cb660da29f75120d531d\"\/>\n<div id='pbresults6' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(6);});pbInputSwitch(6,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox7'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Geometric Mean<\/span><\/div>The <em>geometric mean<\/em> of two numbers <code>a<\/code> and <code>b<\/code> is the number <\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Csqrt%7Bab%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\sqrt{ab}' title='\\sqrt{ab}' class='latex' \/><\/p>\n<p> (It is used to compare aspect ratios of display screens and describe the average growth rate of a population.) Write a program that reads two lines of positive <code>float<\/code>\u00a0from input, and outputs their geometric mean. <br\/> <em>Example<\/em>: If the input is <\/p>\n<pre>5.0<br\/>20.0<\/pre> then the output should be <code>10.0<\/code>.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput7\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch7\" value=\"Input Switch\" onclick=\"pbInputSwitch(7,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect7' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(7,'7b.geometricmean')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(7);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"e9737c6211d2ae4f5ec463c9f5e5c600\"\/>\n<div id='pbresults7' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(7);});pbInputSwitch(7,\"N\");<\/script>\n<\/p>\n<h2>Putting it all together<\/h2>\n<p>As you saw in the previous exercise, you can build mathematical expressions by combining operators. Python evaluates the operators using the same \"order of operations\" that we learn about in math class:<\/p>\n<p style=\"padding-left: 30px\"><strong>B<\/strong>rackets first, then <strong>E<\/strong>xponents, followed by <strong>D<\/strong>ivision and <strong>M<\/strong>ultiplication, then finally <strong>A<\/strong>ddition and <strong>S<\/strong>ubtraction,<\/p>\n<p>which we remember by the acronym \"BEDMAS\".\u00a0Integer division and modulus fit into the \"Division and Multiplication\" category. For example, the expression<\/p>\n<pre>3 * (1 + 2) ** 2 % 4<\/pre>is evaluated by performing the addition in brackets (1+2 = 3), then the exponent (3 ** 2 = 9) , then the multiplication (3 * 9 = 27), and finally the modulus, producing a final result of 27 % 4 =\u00a0<code>3<\/code>.<\/p>\n<div class='pybox modeNeutral' id='pybox8'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Short Answer Exercise: <\/span><span class='title'>Order of Operations<\/span><\/div>Compute the value of the Python expression <\/p>\n<pre>6 - 52 \/\/ 5 ** 2<\/pre><label for=\"pyShortAnswer8\">Your answer (enter a number): <\/label><input type=\"text\" onkeypress=\"{if (event.keyCode==13) pbShortCheck(8)}\" id=\"pyShortAnswer8\"><div class=\"pyboxbuttons\"><input type=\"hidden\" name=\"type\" value=\"number\"\/><input type=\"hidden\" name=\"correct\" value=\"4\"\/><input type=\"hidden\" name=\"slug\" value=\"7b.bedmas\"\/><input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick = 'pbShortCheck(8)'\/><\/div><div class=\"pbresults\" id=\"pyShortResults8\"><\/div><div class=\"epilogue\">Correct!<\/div><\/div>\n<p><em>Integer division with negative numbers<\/em>: The expressions <code>a \/\/ b<\/code> and <code>int(a \/ b)<\/code> are the same when <code>a<\/code> and <code>b<\/code> are positive. However, when <code>a<\/code> is negative, <code>a \/\/ b<\/code> uses \"round towards negative infinity\" and <code>int(a \/ b)<\/code> uses \"round towards zero.\"<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox9'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div> Integer division with negative numbers <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nprint((-10) \/\/ 3, int((-10) \/ 3)) # different\nprint((-12) \/\/ 2, int((-12) \/ 2)) # same, only because 2 divides evenly into -12<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(9)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(9,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"bdedb8cdc178fc3ce4692432fb63735c\"\/>\n<div id='pbresults9' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit9\").value = \"Run program\";document.getElementById(\"inputInUse9\").value = \"N\";<\/script>\n<\/p>\n<h2>Integers and Floating-Point Numbers<\/h2>\n<p>The result of a mathematical expression is a number. As we saw <a title=\"4: Types\" href=\"\/4-types\/\">previously<\/a>, each number is stored as one of the two possible types: \u00a0<code>int<\/code> or <code>float<\/code>. The <code>int<\/code> type represents integers, both positive and negative, that can be as big as you want.<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Python does not accept numbers written in the form <code>1 000 000<\/code> or\u00a0<code>1,000,000<\/code>. Type\u00a0<code>1000000<\/code> instead. <\/span><\/td><\/table><\/p>\n<p>The <code>float<\/code> type represents decimal numbers. Just as a simple calculator stores <code>1\/3<\/code> as its approximate value <code>0.33333333<\/code>, Python also stores decimal numbers as their approximate values. <table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Because Python uses approximations of decimal numbers, certain equations which are mathematically true may not be true in Python.  <form class=\"pbform\" action=\"#\" id=\"pbform10\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox10'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode10' id='usercode10'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nimport math\ns = math.sqrt(2) * math.sqrt(2)\nprint(s == 2)\nprint(s)<\/textarea><\/div>\n<div id='pbhistory10' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit10' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(10)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(10,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse10\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"10\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"b416006b57a6ea47b55211769d6b5292\"\/>\n<div id='pbresults10' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit10\").value = \"Run program\";document.getElementById(\"inputInUse10\").value = \"N\";<\/script>\n  <form class=\"pbform\" action=\"#\" id=\"pbform11\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox11'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode11' id='usercode11'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\ns = 0.1 + 0.1 + 0.1\nprint('s equals 0.3?', s == 0.3)\nprint('s =', s)<\/textarea><\/div>\n<div id='pbhistory11' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit11' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(11)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(11,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse11\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"11\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"152b3da4460191b4c80831387738e623\"\/>\n<div id='pbresults11' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit11\").value = \"Run program\";document.getElementById(\"inputInUse11\").value = \"N\";<\/script>\n  For this reason, it is important to allow some tolerance for these approximations when comparing numbers of type <code>float<\/code>. For example, in the internal grader used by this website, any <code>float<\/code>\u00a0output is marked as correct if it is <a class=\"hintlink\"  id=\"hintlink12\"><em>approximately<\/em><\/a> equal to the expected answer. <\/span><\/td><\/table><\/p>\n<p>We finish this lesson with some exercises.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform13\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox13'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Skill-Testing Question<\/span><\/div>As a \"skill-testing question\", you are given three integers and asked to add the first two numbers, then multiply the result by the third number. Write a program to read the three numbers and print out the correct answer. <a class=\"hintlink\"  id=\"hintlink14\">Hint<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode13' id='usercode13'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory13' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput13\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit13' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch13\" value=\"Input Switch\" onclick=\"pbInputSwitch(13,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(13)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(13,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect13' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(13,'7b.skill')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(13);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse13\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"13\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"64c24e52645047606b8d7d13a2276642\"\/>\n<div id='pbresults13' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(13);});pbInputSwitch(13,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform15\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox15'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>A Feat With Feet<\/span><\/div>For this program the input is a floating-point number representing a height measured in feet. Write a program that will output the equivalent height in centimetres using the conversion formula 1 foot = 30.48 cm. For example, if the input is <code>0.5<\/code>, then the output should be <code>15.24<\/code>.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode15' id='usercode15'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory15' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput15\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit15' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch15\" value=\"Input Switch\" onclick=\"pbInputSwitch(15,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(15)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(15,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect15' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(15,'7b.convert')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(15);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse15\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"15\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"2c639b3a00c82eb1f806a1ee5784c013\"\/>\n<div id='pbresults15' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(15);});pbInputSwitch(15,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform16\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox16'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Gravity<\/span><\/div>A parcel is thrown downward at a speed of <em>v<\/em> m\/s from an airplane at altitude 11000 m. As it falls, its distance from the ground is given by the formula -4.9<em>t<\/em><sup>2<\/sup> - <em>vt<\/em> + 11000, where <em>t<\/em> is the time in seconds since it was dropped. Write a program to output the time it will take to reach the ground. The input to your program is the positive floating-point number <em>v<\/em>. The required time is given by the quadratic formula <\/p>\n<p style=\"text-align: center\"><img src='https:\/\/s0.wp.com\/latex.php?latex=%5Cdisplaystyle%7Bt%3D%5Cfrac%7Bv-%5Csqrt%7Bv%5E2-4%28-4.9%29%2811000%29%7D%7D%7B2%28-4.9%29%7D%7D&#038;bg=T&#038;fg=000000&#038;s=2' alt='\\displaystyle{t=\\frac{v-\\sqrt{v^2-4(-4.9)(11000)}}{2(-4.9)}}' title='\\displaystyle{t=\\frac{v-\\sqrt{v^2-4(-4.9)(11000)}}{2(-4.9)}}' class='latex' \/><\/p>\n<p> <div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode16' id='usercode16'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory16' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput16\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit16' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch16\" value=\"Input Switch\" onclick=\"pbInputSwitch(16,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(16)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(16,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect16' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(16,'7b.gravity')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(16);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse16\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"16\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"14e66e168b09e1ce291c5aef0653c831\"\/>\n<div id='pbresults16' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(16);});pbInputSwitch(16,\"N\");<\/script>\n<\/p>\n<p>Congratulations! After completing these exercises you are ready to move to another lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lesson 7 has three parts A, B, C which can be completed in any order. So far, we have performed math calculations using Python's operators\u00a0+, \u00a0-, *,\u00a0\/ and the functions max and\u00a0min. In this lesson we will see some more &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/7b-math\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-863","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/863"}],"collection":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/comments?post=863"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/863\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}