{"id":5733,"date":"2022-01-08T16:25:55","date_gmt":"2022-01-08T15:25:55","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5733"},"modified":"2024-06-17T19:09:39","modified_gmt":"2024-06-17T17:09:39","slug":"the-print-function","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/01\/08\/the-print-function\/","title":{"rendered":"The print() function"},"content":{"rendered":"<p>1. The <code>print()<\/code> function is a <strong>built-in<\/strong> function. It prints\/outputs a specified message to the screen\/console window.<\/p>\n<p><!--more--><\/p>\n<p>2. Built-in functions, contrary to user-defined functions, are always available and don&#8217;t have to be imported. Python 3.8 comes with 69 built-in functions. You can find their full list provided in alphabetical order in the <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html\" target=\"_blank\" rel=\"noopener\">Python Standard Library<\/a>.<\/p>\n<p>3. To call a function (this process is known as <strong>function invocation<\/strong> or <strong>function call<\/strong>), you need to use the function name followed by parentheses. You can pass arguments into a function by placing them inside the parentheses. You must separate arguments with a comma, e.g.,<\/p>\n<pre class=\"lang:default decode:true\">print(\"Hello,\", \"world!\")<\/pre>\n<p>An &#8220;empty&#8221; <code>print()<\/code> function outputs an empty line to the screen.<\/p>\n<p>4. Python strings are delimited with <strong>quotes<\/strong>, e.g., <code>\"I am a string\"<\/code> (double quotes), or <code>'I am a string, too'<\/code> (single quotes).<\/p>\n<p>5. Computer programs are collections of <strong>instructions<\/strong>. An instruction is a command to perform a specific task when executed, e.g., to print a certain message to the screen.<\/p>\n<p>6. In Python strings the <strong>backslash<\/strong> (<code>\\<\/code>) is a special character which announces that the next character has a different meaning, e.g., <code>\\n<\/code> (the <strong>newline character<\/strong>) starts a new output line.<\/p>\n<p>7. <strong>Positional arguments<\/strong> are the ones whose meaning is dictated by their position, e.g., the second argument is outputted after the first, the third is outputted after the second, etc.<\/p>\n<p>8. <strong>Keyword arguments<\/strong> are the ones whose meaning is not dictated by their location, but by a special word (keyword) used to identify them.<\/p>\n<p>9. The <code>end<\/code> and <code>sep<\/code> parameters can be used for formatting the output of the <code>print()<\/code> function. The <code>sep<\/code> parameter specifies the separator between the outputted arguments, e.g.,<\/p>\n<pre class=\"lang:default decode:true \">print(\"H\", \"E\", \"L\", \"L\", \"O\", sep=\"-\")<\/pre>\n<p>whereas the <code>end<\/code> parameter specifies what to print at the end of the print statement.<\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 1<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true \">x = \"Monty\" \"Python\"\r\nprint(x)<\/pre>\n<p>&nbsp;<\/p>\n<p>The correct answer is <code>MontyPython<\/code>.<\/p>\n<p>In Python, you can concatenate literal strings by placing them one after the other. In other words, x = &#8220;Monty&#8221; &#8220;Python&#8221; is equal to x = &#8220;Monty&#8221; + &#8220;Python&#8221;.<\/p>\n<p>This implicit concatenation works with string literals, but not with string variables. For example, the following code will generate a SyntaxError exception because it implicitly combines string variables:<\/p>\n<pre class=\"lang:default decode:true\">a = \"Monty\"\r\nb = \"Python\"\r\nc = a b # SyntaxError\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Ex. 2<\/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>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 = 2\r\ny = 1\r\nx = y\r\ny -= x\r\n\r\nprint(y, x)<\/pre>\n<p><span class=\"pln\">\u00a0<\/span><\/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-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>0 1<\/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-incorrect--vFyOv\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Incorrect answer<br \/>\n<\/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>01<\/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\">\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>In <strong>line 4<\/strong>, the <code><strong>-=<\/strong><\/code> operator combines the arithmetic operator <code><strong>-<\/strong><\/code> with the assignment operator <code><strong>=<\/strong><\/code> :<code><strong>y -= x<\/strong><\/code> is equal to <code><strong>y = y - x<\/strong><\/code>.<\/li>\n<li>In <strong>line 6<\/strong>, the <code><strong>print<\/strong><\/code> statement prints the values of <code><strong>y<\/strong><\/code> and <code><strong>x<\/strong><\/code> separated by a space on the screen.<\/li>\n<li>In detail:<strong>line 1<\/strong> &#8211; <code><strong>x = 2<\/strong><\/code>\n<p><strong>line 2<\/strong> &#8211; <code><strong>y = 1<\/strong><\/code><\/p>\n<p><strong>line 3<\/strong> &#8211; <code><strong>x = y<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>x = 1<\/strong><\/code><\/p>\n<p><strong>line 4<\/strong> &#8211; <code><strong>y -= x<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>y = y - x<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>y = 1 - 1<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>y = 0<\/strong><\/code><\/p>\n<p><strong>line 6<\/strong> &#8211; <code><strong>print(y, x)<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>print(0, 1)<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>0 1<\/strong><\/code><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. The print() function is a built-in function. It prints\/outputs a specified message to the screen\/console window.<\/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\/5733"}],"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=5733"}],"version-history":[{"count":7,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5733\/revisions"}],"predecessor-version":[{"id":5894,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5733\/revisions\/5894"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5733"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}