{"id":453,"date":"2010-09-24T07:33:22","date_gmt":"2010-09-24T07:33:22","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2010-09-24T07:33:22","modified_gmt":"2010-09-24T07:33:22","slug":"3-comments-literals","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/3-comments-literals\/","title":{"rendered":"3: Comments and Quotes"},"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>A computer program looks like a code language, which is necessary for the computer to precisely understand what your commands mean. But, being a code language makes it harder for humans to read. To compensate for this, you are allowed to write extra notes in your program that the computer ignores. These notes are\u00a0called <strong>comments<\/strong>.<\/p>\n<p>In Python, any line of instructions containing the <code>#<\/code> symbol (\"pound sign\" or \"hash\") denotes the start of a comment. The rest of the line will be ignored when the program is run. Here is an example.<\/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>The effect of a comment.<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(1) # here is a comment\n# print(2)\nprint(3) # another comment @!#!@$<\/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<\/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=\"831117c8a987de290929d790628d2919\"\/>\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>Because the second line started with a <code>#<\/code> sign, Python totally ignored that line, and as you can see, the number <code>2<\/code> was not printed. Common uses for comments include:<\/p>\n<ul>\n<li>explaining parts of the program, for you or other people to read later;<\/li>\n<li>leaving \"to do\" notes, when you write a longer program;<\/li>\n<li>temporarily disabling (\"<em>commenting out<\/em>\")\u00a0a line of a program without totally deleting it, so that it is easier to put back in later.<\/li>\n<\/ul>\n<p>Here is an exercise to illustrate. If you edit the code too much and want to bring back the default version of the code, select <strong>Reset code to default.<\/strong><\/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'>Second Guessing<\/span><\/div> Debug this program so that it prints out the number of seconds in a week. <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# goal: print out the number of seconds in a week \nsecondsPerMinute = 60\nsecondsPerHour = secondsPerMinute * 50 # todo: check this!\nsecondsPerDay = secondsPerHour * 24\ndaysPerWeek = 5\n# daysPerWeek = daysPerWeek + 2 # weekends are disabled!?\nprint(secondsPerDay * daysPerWeek)\n<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode1' value='# goal: print out the number of seconds in a week \\nsecondsPerMinute = 60\\nsecondsPerHour = secondsPerMinute * 50 # todo: check this!\\nsecondsPerDay = secondsPerHour * 24\\ndaysPerWeek = 5\\n# daysPerWeek = daysPerWeek + 2 # weekends are disabled!?\\nprint(secondsPerDay * daysPerWeek)\\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='history' value=\"History\" onclick=\"historyClick(1,'3.debug')\" ><\/td>\n<td><input type='button' name='default' value=\"Reset code to default\" onclick=\"pbSetText(1,descape($('#defaultCode1').val()))\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(1);\" ><\/td>\n<\/tr><\/table><\/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=\"3a0e62067bd4b8de4fc9e28719a9aed0\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<h2>Strings<\/h2>\n<p><strong>Strings<\/strong> are sequences of letters and numbers, or in other words, chunks of text. They are surrounded by two quotes for protection:\u00a0for example in\u00a0<a href=\"\/\">Lesson 0<\/a>\u00a0the part\u00a0<code>\"Hello, World!\"<\/code>\u00a0of the first program\u00a0was a string. If a pound sign <code>#<\/code> appears in a string, then it does not get treated as a comment:<br \/>\n<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 string containing the <code>#<\/code> sign.<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'>\nmyStr = \"To leave a message, press the # key on your phone.\"\nprint(myStr)<\/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<\/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=\"8af2e2f89a84b580f8c85c8987c4bdad\"\/>\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>This behaviour is because the part inside the quotes <code>\"\"<\/code> is a <em>string literal<\/em>, meaning that it should be literally copied and not interpreted as a command. Similarly, <code>print(\"3 + 4\")<\/code> will not print the number 7, but just the string <code>3 + 4<\/code>.<\/p>\n<h2>Escape Sequences<\/h2>\n<p>What if you want to include the quote character <code>\"<\/code> inside of a string? If you try to execute <code><\/code><code>print(\"I said \"Wow!\" to him\")<\/code> this <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/console\/?consolecode=print%28%22I%20said%20%22Wow%21%22%20to%20him.%22%29\" target=\"_blank\">causes an error<\/a>: the problem is that Python sees one string <code>\"I said \"<\/code> followed by something <code>Wow!<\/code> which is not in the string. This is not what we intended!<\/p>\n<p>Python <em>does<\/em> have two simple ways to put quote symbols in strings.<\/p>\n<ul>\n<li>You are allowed to start and end a string literal with single quotes (also known as apostrophes), like <code>'blah blah'<\/code>. Then, double quotes can go in between, such as <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/console\/?consolecode=print%28%27I%20said%20%22Wow%21%22%20to%20him.%27%29\" target=\"_blank\"><code>'I said \"Wow!\" to him.'<\/code><\/a><\/li>\n<li>You can put a backslash character followed by a quote (<code>\\\"<\/code> or <code>\\'<\/code>). This is called an <em>escape sequence<\/em> and Python will remove the backslash, and put just the quote in the string. Here is an example.<br \/>\n<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>The backslashes protect the quotes, but are not printed.<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'>\nprint('Here\\'s a single-quote escape')\nprint(\"This is an \\\"escape\\\" of a double-quote\")<\/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<\/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=\"8c0193821640f0aa2769f8f21fe76d5d\"\/>\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<\/li>\n<\/ul>\n<p>Furthermore, because of escape sequences, backslash (<code>\\<\/code>) is a special character. So to include a backslash in a string, you actually need to \"escape it\" with a second backslash, or in other words you need to write <code>\\\\<\/code> in the string literal.<\/p>\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'>Multiple Choice Exercise: <\/span><span class='title'>Escape Characters<\/span><\/div><div>What is the output of <code>print(\"Backslashes \\\\ and single quotes \\' and double quotes \\\" and pound signs # are awesome!\")<\/code><\/div><label>Your choice: <\/label><select id=\"pyselect5\"><option value=\"d\" selected>Select one<\/option><option value=\"r\">Backslashes \\ and single quotes ' and double quotes \" and pound signs # are awesome!\n<\/option><option value=\"w\">Backslashes \\\\ and single quotes \\' and double quotes \\\" and pound signs # are awesome!<\/option><option value=\"w\"> and pound signs<\/option><option value=\"w\">Causes an error<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" name=\"slug\" value=\"3.escapemulti\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(5)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults5\"><\/div><div class=\"epilogue\">Correct!<\/div><\/div>\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'>The Great Escape<\/span><\/div>Write a program that prints the following: <\/p>\n<pre>A double-quote's escaped using a backslash, e.g. \\\"<\/pre> Note: there is more than one way to do this (as usual)! For example, you could enclose the whole string in single quotes, or in double quotes. In either case, escaping is needed. For extra practice, solve the problem one way, and then solve it the other way. Can you solve it a <a class=\"hintlink\"  id=\"hintlink7\">third way?<\/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='usercode6' id='usercode6'  cols=10 rows=20   class='pyboxCode RW'>\n<\/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='history' value=\"History\" onclick=\"historyClick(6,'3.escape')\" ><\/td>\n<td><input type='button' name='help' value=\"Help\" onclick=\"helpClick(6);\" ><\/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=\"e2be0848ecc2ed520fc2d045bf291361\"\/>\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>There are other escape sequences, like \"newline,\" that we won't discuss right now. For now, you are ready to move on to the next lesson!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A computer program looks like a code language, which is necessary for the computer to precisely understand what your commands mean. But, being a code language makes it harder for humans to read. To compensate for this, you are allowed &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/3-comments-literals\/\">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-453","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/453"}],"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=453"}],"version-history":[{"count":1,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/453\/revisions"}],"predecessor-version":[{"id":11483,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/453\/revisions\/11483"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}