{"id":5739,"date":"2022-01-30T13:24:10","date_gmt":"2022-01-30T12:24:10","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5739"},"modified":"2026-01-24T13:32:24","modified_gmt":"2026-01-24T12:32:24","slug":"5739","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/01\/30\/5739\/","title":{"rendered":"Operators"},"content":{"rendered":"<p>1. An <strong>expression<\/strong> is a combination of values (or variables, operators, calls to functions \u2012 you will learn about them soon) which evaluates to a certain value, e.g., <code>1 + 2<\/code>.<\/p>\n<p><!--more--><\/p>\n<p>2. <strong>Operators<\/strong> are special symbols or keywords which are able to operate on the values and perform (mathematical) operations, e.g., the <code>*<\/code> operator multiplies two values: <code>x * y<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p>3. Arithmetic operators in Python:<\/p>\n<p><code>+<\/code> (addition),<\/p>\n<p><code>-<\/code> (subtraction),<\/p>\n<p><code>*<\/code> (multiplication),<\/p>\n<p><code>\/<\/code> (classic division \u2012 always returns a float),<\/p>\n<p><code>%<\/code> (modulus \u2012 divides left operand by right operand and returns the remainder of the operation, e.g., <code>5 % 2 = 1<\/code>),<\/p>\n<p><code>**<\/code> (exponentiation \u2012 left operand raised to the power of right operand, e.g., <code>2 ** 3 = 2 * 2 * 2 = 8<\/code>),<\/p>\n<p><code>\/\/<\/code> (floor\/integer division \u2012 returns a number resulting from division, but rounded down to the nearest whole number, e.g., <code>3 \/\/ 2.0 = 1.0<\/code>)<\/p>\n<p>&nbsp;<\/p>\n<p>4. A <strong>unary<\/strong> operator is an operator with only one operand, e.g.,<\/p>\n<p><code>-1<\/code>, or <code>+3<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p>5. A <strong>binary<\/strong> operator is an operator with two operands, e.g.,<\/p>\n<p><code>4 + 5<\/code>, or <code>12 % 5<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p>6. Some operators act before others \u2013 <strong>the hierarchy of priorities<\/strong>:<\/p>\n<ul>\n<li>the <code>**<\/code> operator (exponentiation) has the highest priority;<\/li>\n<li>then the unary <code>+<\/code> and <code>-<\/code> (note: a unary operator to the right of the exponentiation operator binds more strongly, for example: <code>4 ** -1<\/code> equals <code>0.25<\/code>)<\/li>\n<li>then <code>*<\/code>, <code>\/<\/code>, <code>\/\/<\/code>, and <code>%<\/code>;<\/li>\n<li>and, finally, the lowest priority: the binary <code>+<\/code> and <code>-<\/code>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>7. Subexpressions in <strong>parentheses<\/strong> are always calculated first, e.g.,<\/p>\n<p><code>15 - 1 * (5 * (1 + 2)) = 0<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p>8. The <strong>exponentiation<\/strong> operator uses <strong>right-sided binding<\/strong>, e.g.,<\/p>\n<p><code>2 ** 2 ** 3 = 256<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 1<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true\">print((2 ** 4), (2 * 4.), (2 * 4))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p id=\"sol\"><code class=\"codep \">16 8.0 8<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true\">print((-2 \/ 4), (2 \/ 4), (2 \/\/ 4), (-2 \/\/ 4))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p id=\"sol2\"><code class=\"codep \">-0.5 0.5 0 -1<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 3<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true \">print((2 % -4), (2 % 4), (2 ** 3 ** 2))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p id=\"sol3\"><code class=\"codep \">-2 2 512<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 4<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true \">a = False\r\nb = True\r\n\r\nx = a or b\r\ny = a and b\r\n\r\nprint(x + (not y))<\/pre>\n<p>&nbsp;<\/p>\n<p><code><strong>2<\/strong><\/code><\/p>\n<p>Line 4 (step by step)<\/p>\n<p>Step 1 &#8211; x = a or b<\/p>\n<p>Step 2 &#8211; x = False or True<\/p>\n<p>Result &#8211; x = True.<\/p>\n<p>&nbsp;<\/p>\n<p>Line 5 (step by step)<\/p>\n<p>Step 1 &#8211; y = a and b<\/p>\n<p>Step 2 &#8211; y = False and True<\/p>\n<p>Result &#8211; x = False.<\/p>\n<p>&nbsp;<\/p>\n<p>Line 7 (step by step)<\/p>\n<p>Step 1 &#8211; print(x + (not y))<\/p>\n<p>Step 2 &#8211; print(True + (not False))<\/p>\n<p>Step 3 &#8211; print(True + True)<\/p>\n<p>Step 4 &#8211; print(1 + 1)<\/p>\n<p>Step 5 &#8211; print(2)<\/p>\n<p>Output &#8211; 2<\/p>\n<p>A Boolean expression always returns one of two Boolean values True or False. However, if Boolean values are involved in arithmetic calculations, as in line 7, they are represented with their corresponding numerals 1 and 0.<\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 5<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true \">s1 = 'Python'\r\ns2 = s1[0] + s1[-3] * len(s1)\r\nprint(s2)<\/pre>\n<p>&nbsp;<\/p>\n<p><code>Phhhhhh<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 6<\/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 \/ .5 % (5 \/\/ 2)\r\n\r\nprint(x)<\/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-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\">\n<p><code>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>4<\/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>0.0<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\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.0<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<p>Here&#8217;s how Python executes the expression on <strong>line 1<\/strong> of the code:<\/p>\n<ul>\n<li><strong>Step 1<\/strong>: expressions in parentheses have the highest evaluation precedence. The <strong>floor<\/strong> operator (<code><strong>\/\/<\/strong><\/code>) rounds the division result down to the nearest whole number.<code><strong>x = 2 \/ .5 % (5 \/\/ 2)<\/strong><\/code><strong> -&gt; <\/strong><code><strong>x = 2 \/ .5 % 2<\/strong><\/code>.<\/li>\n<li><strong>Step 2<\/strong>: operators with the same priority are evaluated from left to right. The <strong>division<\/strong> (<code><strong>\/<\/strong><\/code>) operator returns the result of a division as a float.<code><strong>x = 2 \/ .5 % 2<\/strong><\/code><strong> -&gt; <\/strong><code><strong>x = 4.0 % 2<\/strong><\/code>.<\/li>\n<li><strong>Step 3<\/strong>: the <strong>modulo<\/strong> (<code><strong>%<\/strong><\/code>) operator returns the remainder of a division.<code><strong>x = 4.0 % 2<\/strong><\/code><strong> -&gt; <\/strong><code><strong>x = 0.0<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex.7<\/p>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following statements is true?<\/p>\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\">\n<p>A. The exponentiation operator uses the left-sided binding.<\/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\">B.\u00a0 <\/span>The result of the \/\/ operator is always an integer.<\/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><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">C.\u00a0 <\/span>A unary operator is an operator with only one operand.<\/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\">\n<p>D. The right argument of the % operator can be zero.<\/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<p>&nbsp;<\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Explanation:<\/em><\/p>\n<p>-&gt; &#8220;<em>The result of the \/\/ operator is always an integer&#8221; : <\/em>this is false &#8211; if one operand is a float, the result will be a float. For example<\/p>\n<p>-&gt; &#8220;<em>The exponentiation operator uses the left-sided binding&#8221; <\/em>: this is false &#8211; The exponentiation operator uses the right-sided binding.<\/p>\n<p>-&gt; &#8220;<em>A unary operator is an operator with only one operand&#8221; <\/em>: this is true ! Example of unary operator : <code>-5<\/code>\u00a0 , or + <code>14<\/code><\/p>\n<p><em>-&gt; &#8220;The right argument of the % operator can be zero&#8221;<\/em> : this is false. This will raise a ZeroDivisionError exception.<\/p>\n<p><em>Try it yourself:<\/em><\/p>\n<pre class=\"lang:default decode:true \">    # \"The result of the \/\/ operator is always an integer\" : this is FALSE.\r\n    x= 5\/\/2.0\r\n    print(x)            # 2.0\r\n    print(type(x))      # &lt;class 'float'&gt;\r\n     \r\n    # \"The exponentiation operator uses the left-sided binding\" : this is FALSE.\r\n    print(2**2**3)      # 256\r\n    print(2**8)         # 256\r\n     \r\n    # \"The right argument of the % operator can be zero\" : this is FALSE.\r\n    print(2 % 0)        # ZeroDivisionError<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. An expression is a combination of values (or variables, operators, calls to functions \u2012 you will learn about them soon) which evaluates to a certain value, e.g., 1 + 2.<\/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\/5739"}],"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=5739"}],"version-history":[{"count":10,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5739\/revisions"}],"predecessor-version":[{"id":6289,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5739\/revisions\/6289"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5739"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}