{"id":954,"date":"2010-11-09T10:36:25","date_gmt":"2010-11-09T10:36:25","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2010-11-09T10:36:25","modified_gmt":"2010-11-09T10:36:25","slug":"12-tips","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/12-tips\/","title":{"rendered":"12: Tips"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p>This lesson contains a few facts about the Python language which we left out of earlier lessons.<\/p>\n<h2><code>print<\/code> Options<\/h2>\n<p>So far, the only way we have seen to print is to use the <code>print()<\/code> function, which prints several items one line at a time, and separates all items by spaces. In fact, print accepts a few <em>keyword arguments<\/em> which allow you to modify this behaviour to suit your needs.<\/p>\n<ul>\n<li>To change the space separator to some other string <code>S<\/code>, include the argument <code><\/code><code>sep = \u00abS\u00bb<\/code><\/li>\n<li>To replace the \"end-of-line\" character with some other string <code>S<\/code>, include the argument <code><\/code><code>end = \u00abS\u00bb<\/code><\/li>\n<\/ul>\n<p>This new feature, <em>keyword arguments<\/em>, is easiest to demonstrate with a few examples.<\/p>\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>Examples of <code>sep<\/code> and <code>end<\/code>. Look at the effect of each <code>print<\/code> call.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nprint('D', 'A', 'S', 'H', sep='-')\nprint('The price is $', 12*5, sep='')\nprint('These two lines', end='')\nprint(' will appear together.')\nprint('Great', end='!')<\/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=\"c8c2acb441ca22bbdf5071393cbe4af2\"\/>\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>These keyword arguments must be passed at the end of the argument list or else an <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/console\/?consolecode=print%28end%3D%27%27%2C%205%29\" target=\"_blank\">error<\/a> will occur. If you use both they can be given in any order.<\/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'>Alphabet Aerobics<\/span><\/div>Using what we just introduced, debug the following program so that it outputs the upper-case alphabet <em>all on one line<\/em>.<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'>\nfor c in range(0, 26):\n print(chr(ord('A')+c))\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode1' value='for c in range(0, 26):\\n print(chr(ord(\\u0027A\\u0027)+c))\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/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<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(1,'12.alphabet')\" ><\/td>\n<\/tr><\/table><select id='pbSelect1' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(1,descape($('#defaultCode1').val()))\" >Reset code to default<\/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=\"10370b99c2ad70623e6b0664ee756f9e\"\/>\n<div id='pbresults1' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(1);});document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<h2>Valid Variable\/Function Names<\/h2>\n<ul>\n<li>Names can contain letters, numbers, and the underscore (<code>_<\/code>) character.<\/li>\n<li>The first character of every name must be a letter.<\/li>\n<li>Python distinguishes between upper- and lower-case letters.<\/li>\n<\/ul>\n<p>Thus <code>my_3rd_int<\/code> is a valid variable name, but <code>3rd_int<\/code> is not.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>An example of distinguishing between upper- and lower-case.<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\ngreaterOde = \"Truth is beauty, beauty truth\"\ngreatErode = \"Wind erosion\"\nprint(greaterOde) # still poetry<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"045208e9dc7dd2820a91bab0e459ef7b\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<h2>Descending <code>for<\/code> loops &amp; other increments<\/h2>\n<p>Recall that we showed how <code>for<\/code> loops can iterate through numbers in increasing order:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox3'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>A <code>for<\/code> loop<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nfor i in range(0, 5):\n print(i)<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/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><\/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=\"48e0123a12ad2574217df6c6fcebe640\"\/>\n<div id='pbresults3' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/p>\n<p>Quite often, it is necessary to write a <code>for<\/code> loop which goes through the numbers in <em>descending<\/em> order (biggest to smallest). To do this we call <code>range<\/code> with a third argument called the <em>step<\/em>:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox4'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Negative step for <code>range<\/code><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode4' id='usercode4'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nfor i in range(5, 1, -1):\n print(i)<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(4)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(4,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"61f7b1cfa7128b1075811c3bc806df3a\"\/>\n<div id='pbresults4' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit4\").value = \"Run program\";document.getElementById(\"inputInUse4\").value = \"N\";<\/script>\n<\/p>\n<p>If you think for a moment, you also notice <code>range(0, 5)<\/code> is the same as <code>range(0, 5, 1)<\/code>: the <em>default increment <\/em>is 1. In either case, be careful that <code>range(start, stop, step)<\/code> goes until just <em>before<\/em> reaching the value <code>stop<\/code>. For more information you can see the <a href=\"http:\/\/docs.python.org\/py3k\/library\/functions.html#range\">Python manual<\/a>.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox5'>\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'>Lucky Sevens<\/span><\/div>Write a program, using a <code>for<\/code> loop with step size 10, to print out all 2-digit numbers which end in 7, in increasing order.<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='usercode5' id='usercode5'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/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<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(5,'12.sevens')\" ><\/td>\n<\/tr><\/table><select id='pbSelect5' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(5);\" >Help<\/option>\n<\/select><\/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=\"3ddfe6a0b90f746bba2e0701b5b798d4\"\/>\n<div id='pbresults5' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(5);});document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<p>Similarly, you can use <code>string[x:y:2]<\/code> to get the substring with characters <code>x, x+2, x+4, ...<\/code>\u00a0or <code>string[y:x:-1]<\/code> to get a reversed part of a string (where <code>y<\/code> &gt; <code>x<\/code>).<\/p>\n<p>With a <code>for<\/code> loop, another way to accomplish a decreasing range is\u00a0<code>reversed(range(x, y))<\/code>, which goes from\u00a0<code>y-1<\/code>\u00a0to\u00a0<code>x<\/code>\u00a0in decreasing order.<\/p>\n<h2>Writing Smaller Code<\/h2>\n<p>Python allows several ways to write shorter code; we introduce a few of them here.<\/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> On this website we won't always use these features, since sometimes it makes code harder to read. <\/span><\/td><\/table><\/p>\n<h3>Assignment Operators<\/h3>\n<p>Python lets you write \"<code>x += 1<\/code>\" to mean \"add one to <code>x<\/code>.\" So it is equivalent to \"<code>x = x + 1<\/code>\" as we show below:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox6'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Examples of <code>+=<\/code>, <code>-=<\/code>, <code>*=<\/code>, <code>\/=<\/code><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 240px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=9 readonly='readonly'  style = 'height : 240px;'  class='pyboxCode RO'>\nx = 8\nx += 2\nprint(x)\nx -= 1\nprint(x)\nx \/= 3\nprint(x)\nx *= 10\nprint(x)<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/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><\/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=\"657dbfafceda6ebf4bb45614201a5a12\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<p>Similarly, there are operators for integer division (<code>\/\/=<\/code>), modulus (<code>%=<\/code>), and power (<code>**=<\/code>).<\/p>\n<h3>Inline Blocks<\/h3>\n<p>We have seen several statements which are followed by indented \"blocks\" of code: <code>for<\/code>, <code>if<\/code>, <code>else<\/code>, <code>elif<\/code>, <code>while<\/code>, and <code>def<\/code>, for example:<\/p>\n<pre>if x==y:\n  \u00abblock\u00bb #indented, multiline<\/pre>In the special case that the <code>\u00abblock\u00bb<\/code> is only one line long, Python allows the following alternative syntax:<\/p>\n<pre>if x==y: \u00abblock\u00bb #single line<\/pre>Here is an example:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox7'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Inline block statements<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\ndef square(x): return x*x\nfor i in range(0, 6): print(square(i))<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/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><\/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=\"b3ade191aee4175bf56777c569c34c2f\"\/>\n<div id='pbresults7' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit7\").value = \"Run program\";document.getElementById(\"inputInUse7\").value = \"N\";<\/script>\n<\/p>\n<p>This has its limits however: a compound block with a colon (<code>if<\/code>, <code>for<\/code>, etc) can't be used as an inline block. For example, <code>if 2&gt;1: if 4&gt;3: print()<\/code> gives a syntax <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/console\/?consolecode=if%202%26gt%3B1%3A%20if%204%26gt%3B3%3A%20print%28%29\" target=\"_blank\">error<\/a>.<\/p>\n<h3>Multiple Assignments<\/h3>\n<p>Python allows you to combine two assignment statements into one:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox8'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Multiple assignment<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode8' id='usercode8'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nx, y = 3, 4\nprint('x =', x, 'and y =', y)<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(8)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(8,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"4c8e080f4888bb5c86d52fe2d2145597\"\/>\n<div id='pbresults8' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit8\").value = \"Run program\";document.getElementById(\"inputInUse8\").value = \"N\";<\/script>\n<\/p>\n<p>Note that this allows you to solve our <a href=\"\/1-variables\/\">swapping exercise<\/a> using only one line!<\/p>\n<h3>Comparison Chaining<\/h3>\n<p>Python also allows you to combine several comparisons into one:<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>Multiple assignment<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nage = int(input())\nif 12 &lt; age &lt; 20: # same as (12 &lt; age) and (age &lt; 20) \n print('You are a teenager!')<\/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=\"bee3a2493a0e6bf483850a7e3e8ac8ac\"\/>\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>\nAny chain <code>v1 c1 v2 c2 v3 ...<\/code> where <code>v<\/code> are values and <code>c<\/code> are comparisons, is treated the same as <code>(v1 c1 v2) and (v2 c2 v3) and...<\/code><\/p>\n<h3>Default values for <code>[:]<\/code> and <code>range()<\/code><\/h3>\n<p>You can leave out one or both of the start\/end values when using the sub-string operator <code>[:]<\/code>. (This is also true for lists, as we'll see in the next lesson.) The default value of the first index is <code>0<\/code> (the start of the string) and the default value of the last index is the length of the string. Likewise, <code>range(n)<\/code> is short for <code>range(0, n)<\/code>.<\/p>\n<p><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>Using the default values for <code>[:]<\/code> and <code>range()<\/code><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'>\nS = \"Toronto\"\nprint(S[3:])                  # right substring, \"onto\"\nprint(S[:2])                  # left substring, \"To\"\nfor i in range(5): print(i*i) # prints 0, 1, .. 16<\/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=\"e91aa3e67182433fea837df28b7dfeaa\"\/>\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<\/p>\n<h1><code>None<\/code><\/h1>\n<p>Some functions return a value as their main effect, like <code>abs(x)<\/code>. But other functions which are more valuable for their effects, like <code>print()<\/code>, still return the 'default' value <code>None<\/code>:<\/p>\n<p><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>The value of <code>print<\/code><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 136px;'><textarea wrap='off' name='usercode11' id='usercode11'  cols=10 rows=5 readonly='readonly'  style = 'height : 136px;'  class='pyboxCode RO'>\nresult = print('Anything works here')\nprint(result)\ndef forgotToReturn(x):\n x*x\nprint(forgotToReturn(5))<\/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=\"9b7f3f4bb01953a5fbbe924ce2aee475\"\/>\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<\/p>\n<p>Here <code>None<\/code> is a special value used by Python as a general-purpose placeholder. If you <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/console\/?consolecode=print%28type%28None%29%29\" target=\"_blank\">call type(None)<\/a>, you find out that <code>None<\/code> even has its own type, <code>NoneType<\/code>. While we are at it, let's see what is the type of a type variable:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform12\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox12'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>The type of a type<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode12' id='usercode12'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nx = 23432\nprint(x, type(x), type(type(x)), type(type(type(x))))<\/textarea><\/div>\n<div id='pbhistory12' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit12' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(12)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(12,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse12\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"12\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"aea32d11990220cd2435630a82e7da88\"\/>\n<div id='pbresults12' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit12\").value = \"Run program\";document.getElementById(\"inputInUse12\").value = \"N\";<\/script>\n<\/p>\n<p>In the next exercise, we ask you to carefully track the results of a long command using <code>type<\/code> and <code>print<\/code>.<\/p>\n<div class='pybox modeNeutral multiscramble' 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'>Scramble Exercise: <\/span><span class='title'>One None<\/span><\/div>The output of <code>print(type(print(type(type(print(print()))))))<\/code> consists of 4 lines. Put them in the correct order.<ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble13\">\n<li id=\"pyli13_3\" class=\"pyscramble\" >&lt;class 'NoneType'&gt;<\/li>\n<li id=\"pyli13_0\" class=\"pyscramble\" ><i>&laquo;blank line&raquo;<\/i><\/li>\n<li id=\"pyli13_2\" class=\"pyscramble\" >&lt;class 'type'&gt;<\/li>\n<li id=\"pyli13_1\" class=\"pyscramble\" >None<\/li>\n<\/ul>\n<div class=\"pyboxbuttons\"><input type='button' value='Check answer' onclick='pbMultiscrambleCheck(13)'\/>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" name=\"slug\" value=\"12.ordering\"\/>\n<\/div><div id=\"pbresults13\" class=\"pbresults\"><\/div><div class=\"epilogue\">Correct!<\/div><\/div>\n<p>That is all of the tips for now. You are ready to continue to the next lesson!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lesson contains a few facts about the Python language which we left out of earlier lessons. print Options So far, the only way we have seen to print is to use the print() function, which prints several items one &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/12-tips\/\">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-954","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/954"}],"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=954"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/954\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}