{"id":5735,"date":"2022-01-15T10:23:44","date_gmt":"2022-01-15T09:23:44","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5735"},"modified":"2024-06-17T19:01:27","modified_gmt":"2024-06-17T17:01:27","slug":"literals","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/01\/15\/literals\/","title":{"rendered":"Literals"},"content":{"rendered":"<p>1. <strong>Literals<\/strong> are notations for representing some fixed values in code. Python has various types of literals &#8211; for example, a literal can be a number (numeric literals, e.g., <code>123<\/code>), or a string (string literals, e.g., &#8220;I am a literal.&#8221;).<\/p>\n<p><!--more--><\/p>\n<p>2. The <strong>binary system<\/strong> is a system of numbers that employs <i>2<\/i> as the base. Therefore, a binary number is made up of 0s and 1s only, e.g., <code>1010<\/code> is <i>10<\/i> in decimal.<\/p>\n<p>Octal and hexadecimal numeration systems, similarly, employ <i>8<\/i> and <i>16<\/i> as their bases respectively. The hexadecimal system uses the decimal numbers and six extra letters.<\/p>\n<p>3. <strong>Integers<\/strong> (or simply <strong>int<\/strong>s) are one of the numerical types supported by Python. They are numbers written without a fractional component, e.g., <code>256<\/code>, or <code>-1<\/code> (negative integers).<\/p>\n<p>4. <strong>Floating-point<\/strong> numbers (or simply <strong>float<\/strong>s) are another one of the numerical types supported by Python. They are numbers that contain (or are able to contain) a fractional component, e.g., <code>1.27<\/code>.<\/p>\n<p>5. To encode an apostrophe or a quote inside a string you can either use the escape character, e.g., <code>'I\\'m happy.'<\/code>, or open and close the string using an opposite set of symbols to the ones you wish to encode, e.g., <code>\"I'm happy.\"<\/code> to encode an apostrophe, and <code>'He said \"Python\", not \"typhoon\"'<\/code> to encode a (double) quote.<\/p>\n<p>6. <strong>Boolean values<\/strong> are the two constant objects <code>True<\/code> and <code>False<\/code> used to represent truth values (in numeric contexts <code>1<\/code> is <code>True<\/code>, while <code>0<\/code> is <code>False<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"label\"><b>EXTRA<\/b><\/span><\/p>\n<p>There is one more, special literal that is used in Python: the <code>None<\/code> literal. This literal is a so-called <code>NoneType<\/code> object, and it is used to represent <strong>the absence of a value<\/strong>. We&#8217;ll tell you more about it soon.<\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 1<\/p>\n<p>What types of literals are the following two examples?<\/p>\n<p><code class=\"codep \">\"Hello \", \"007\"<\/code><\/p>\n<p id=\"sol\">They&#8217;re both strings\/string literals.<\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 2<\/p>\n<p>What types of literals are the following four examples?<\/p>\n<p><code class=\"codep \">\"1.5\", 2.0, 528, False<\/code><\/p>\n<p id=\"sol2\">The first is a string, the second is a numerical literal (a float), the third is a numerical literal (an integer), and the fourth is a boolean literal.<\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 3<\/p>\n<p>What is the decimal value of the following binary number?<\/p>\n<p><code class=\"codep \">1011<\/code><\/p>\n<p id=\"sol3\">It&#8217;s <code>11<\/code>, because <span style=\"font-family: courier new;\">(2**0) + (2**1) + (2**3) = 11<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 4<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following <code><strong>print<\/strong><\/code> statements displays the integer decimal <code><strong>1000<\/strong><\/code> (<strong>one thousand<\/strong>) on the screen?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>print(1_0_0_0)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>print(1,000)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>print(01000)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>print(1_000)<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>print(1_0_0_0)<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>print(1_000)<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<pre class=\"lang:default decode:true\">print(1,000) # 1 0\r\nprint(1_0_0_0) # 1000\r\n# print(01000) # SyntaxError\r\nprint(1_000) # 1000<\/pre>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>print(1_0_0_0)<\/strong><\/code> and <code><strong>print(1_000)<\/strong><\/code> statements are <strong>correct<\/strong>.. Starting with version <strong>3.6<\/strong>, Python allows the use of <strong>underscores<\/strong> in numeric literals to increase their readability when they are very large.<\/li>\n<li><code><strong>print(1,000)<\/strong><\/code> is <strong>incorrect<\/strong>. It is equal to <code><strong>print(1, 0)<\/strong><\/code> and outputs <code><strong>1 0<\/strong><\/code> on the screen.<\/li>\n<li><code><strong>print(01000)<\/strong><\/code> is <strong>incorrect<\/strong> because in Python <strong>leading zeros<\/strong> in decimal <strong>integer<\/strong> <strong>literals<\/strong> are not allowed.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 5<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">n = 0\r\nfor i in \"\\'Monthy Python\\'\":\r\n  n += 1\r\n\r\nprint(n)<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>15<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">The loop counts the number of characters in the string <code><strong>str<\/strong><\/code>. The escape sequence <code><strong>\\'<\/strong><\/code>, representing the <strong>single<\/strong> <strong>quotation<\/strong> <strong>mark<\/strong>, is counted as a <strong>single character<\/strong>. Therefore, the total number of characters, including the space, is <code><strong>15<\/strong><\/code>.<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<div>Ex. 6<\/div>\n<div>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">x = int(3.5)\r\ny = str(3)\r\nz = float(0)\r\n\r\nprint(x, y, z)<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\"><\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>3 3 0.0<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>int()<\/strong><\/code> function converts a number or string to an integer. If the number is a float, the function discards the decimal part.<\/li>\n<li>The <code><strong>str()<\/strong><\/code> function converts a value to a string.<\/li>\n<li>The <code><strong>float()<\/strong><\/code> function converts a number or string to a float, including <code><strong>0<\/strong><\/code>.<\/li>\n<li>By default, the <code><strong>print()<\/strong><\/code> function does not print quotation marks of a string:\n<p><code><strong>print(x, y, z)<\/strong><\/code><strong> -&gt;<\/strong> <code><strong>print(3, '3', 0.0)<\/strong><\/code><strong> -&gt;<\/strong> <code><strong>3 3 0.0<\/strong><\/code><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Literals are notations for representing some fixed values in code. Python has various types of literals &#8211; for example, a literal can be a number (numeric literals, e.g., 123), or a string (string literals, e.g., &#8220;I am a literal.&#8221;).<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5735"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5735"}],"version-history":[{"count":9,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5735\/revisions"}],"predecessor-version":[{"id":5891,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5735\/revisions\/5891"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5735"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}