{"id":5147,"date":"2022-02-26T13:23:11","date_gmt":"2022-02-26T12:23:11","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5147"},"modified":"2025-05-05T18:07:45","modified_gmt":"2025-05-05T16:07:45","slug":"conditionals","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/02\/26\/conditionals\/","title":{"rendered":"Conditionals"},"content":{"rendered":"<div class=\"small-12 large-6 columns\">\n<p>1. The <strong>comparison<\/strong> (otherwise known as <i>relational<\/i>) operators are used to compare values. The table below illustrates how the comparison operators work, assuming that <code>x = 0<\/code>, <code>y = 1<\/code>, and <code>z = 0<\/code>:<\/p>\n<\/div>\n<p><!--more--><\/p>\n<div class=\"small-12 large-6 columns\">\n<table style=\"width: 100%;\">\n<tbody>\n<tr>\n<th style=\"width: 13.4483%;\">Operator<\/th>\n<th style=\"width: 59.8276%;\">Description<\/th>\n<th style=\"width: 26.5517%;\">Example<\/th>\n<\/tr>\n<tr>\n<td style=\"width: 13.4483%;\"><code>==<\/code><\/td>\n<td style=\"width: 59.8276%;\">returns <code>True<\/code> if operands&#8217; values are equal, and <code>False<\/code> otherwise<\/td>\n<td style=\"width: 26.5517%;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.4483%;\"><code>!=<\/code><\/td>\n<td style=\"width: 59.8276%;\">returns <code>True<\/code> if operands&#8217; values are not equal, and <code>False<\/code> otherwise<\/td>\n<td style=\"width: 26.5517%;\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">!=<\/span> <span class=\"ace_identifier\">y<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">!=<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># False<\/span><\/code><\/div>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.4483%;\"><code>&gt;<\/code><\/td>\n<td style=\"width: 59.8276%;\"><code>True<\/code> if the left operand&#8217;s value is greater than the right operand&#8217;s value, and <code>False<\/code> otherwise<\/td>\n<td style=\"width: 26.5517%;\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">&gt;<\/span> <span class=\"ace_identifier\">y<\/span> <span class=\"ace_comment\"># False<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">y<\/span> <span class=\"ace_keyword ace_operator\">&gt;<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.4483%;\"><code>&lt;<\/code><\/td>\n<td style=\"width: 59.8276%;\"><code>True<\/code> if the left operand&#8217;s value is less than the right operand&#8217;s value, and <code>False<\/code> otherwise<\/td>\n<td style=\"width: 26.5517%;\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">&lt;<\/span> <span class=\"ace_identifier\">y<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">y<\/span> <span class=\"ace_keyword ace_operator\">&lt;<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># False<\/span><\/code><\/div>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.4483%;\"><code>\u2265<\/code><\/td>\n<td style=\"width: 59.8276%;\"><code>True<\/code> if the left operand&#8217;s value is greater than or equal to the right operand&#8217;s value, and <code>False<\/code> otherwise<\/td>\n<td style=\"width: 26.5517%;\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">&gt;=<\/span> <span class=\"ace_identifier\">y<\/span> <span class=\"ace_comment\"># False<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">&gt;=<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">y<\/span> <span class=\"ace_keyword ace_operator\">&gt;=<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.4483%;\"><code>\u2264<\/code><\/td>\n<td style=\"width: 59.8276%;\"><code>True<\/code> if the left operand&#8217;s value is less than or equal to the right operand&#8217;s value, and <code>False<\/code> otherwise<\/td>\n<td style=\"width: 26.5517%;\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">&lt;=<\/span> <span class=\"ace_identifier\">y<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">x<\/span> <span class=\"ace_keyword ace_operator\">&lt;=<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># True<\/span><\/code><\/div>\n<div class=\"ace_line\"><code><span class=\"ace_identifier\">y<\/span> <span class=\"ace_keyword ace_operator\">&lt;=<\/span> <span class=\"ace_identifier\">z<\/span> <span class=\"ace_comment\"># False<\/span><\/code><\/div>\n<div class=\"ace_line\"><\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2. When you want to execute some code only if a certain condition is met, you can use a <strong>conditional statement<\/strong>:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>a single <code>if<\/code> statement, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">x = 10\r\n\r\nif x == 10: # condition\r\n   print(\"x is equal to 10\") # Executed if the condition is True.<\/pre>\n<\/div>\n<\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>a series of <code>if<\/code> statements, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">x = 10\r\n\r\nif x &gt; 5: # condition one\r\n   print(\"x is greater than 5\") # Executed if condition one is True.\r\n\r\nif x &lt; 10: # condition two\r\n   print(\"x is less than 10\") # Executed if condition two is True.\r\n\r\nif x == 10: # condition three\r\n   print(\"x is equal to 10\") # Executed if condition three is True.<\/pre>\n<\/div>\n<\/div>\n<p>Each <code>if<\/code> statement is tested separately.<\/p>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>an <code>if-else<\/code> statement, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">x = 10\r\n\r\nif x &lt; 10: # Condition\r\n  print(\"x is less than 10\") # Executed if the condition is True.\r\n\r\nelse:\r\n  print(\"x is greater than or equal to 10\") # Executed if the condition is False.<\/pre>\n<\/div>\n<\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>a series of <code>if<\/code> statements followed by an <code>else<\/code>, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">x = 10\r\n\r\nif x &gt; 5: # True\r\nprint(\"x &gt; 5\")\r\n\r\nif x &gt; 8: # True\r\n  print(\"x &gt; 8\")\r\n\r\nif x &gt; 10: # False\r\n  print(\"x &gt; 10\")\r\n\r\nelse:\r\n  print(\"else will be executed\")<\/pre>\n<p>Each <code>if<\/code> is tested separately. The body of <code>else<\/code> is executed if the last <code>if<\/code> is <code>False<\/code>.<\/p>\n<\/div>\n<\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>The <code>if-elif-else<\/code> statement, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">x = 10\r\n\r\nif x == 10: # True\r\n   print(\"x == 10\")\r\n\r\nif x &gt; 15: # False\r\n   print(\"x &gt; 15\")\r\n\r\nelif x &gt; 10: # False\r\n   print(\"x &gt; 10\")\r\n\r\nelif x &gt; 5: # True\r\n   print(\"x &gt; 5\")\r\n\r\nelse:\r\nprint(\"else will not be executed\")<\/pre>\n<p>If the condition for <code>if<\/code> is <code>False<\/code>, the program checks the conditions of the subsequent <code>elif<\/code> blocks \u2013 the first <code>elif<\/code> block that is <code>True<\/code> is executed. If all the conditions are <code>False<\/code>, the <code>else<\/code> block will be executed.<\/p>\n<\/div>\n<\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Nested conditional statements, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">x = 10\r\n\r\nif x &gt; 5: # True\r\n   if x == 6: # False\r\n      print(\"nested: x == 6\")\r\n   elif x == 10: # True\r\n      print(\"nested: x == 10\")\r\n   else:\r\n      print(\"nested: else\")\r\nelse:\r\n   print(\"else\")<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 1<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">x = 5\r\ny = 10\r\nz = 8\r\n\r\nprint(x &gt; y)\r\nprint(y &gt; z)<\/pre>\n<p>&nbsp;<\/p>\n<div id=\"sol\"><code class=\"codep \">False<br \/>\nTrue<\/code><\/div>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 2<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">x, y, z = 5, 10, 8\r\n\r\nprint(x &gt; z)\r\nprint((y - 5) == x)<\/pre>\n<p>&nbsp;<\/p>\n<div id=\"sol2\"><code class=\"codep \">False<br \/>\nTrue<\/code><\/div>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 3<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">x, y, z = 5, 10, 8\r\nx, y, z = z, y, x\r\n\r\nprint(x &gt; z)\r\nprint((y - 5) == x)<\/pre>\n<p>&nbsp;<\/p>\n<div id=\"sol3\"><code class=\"codep \">True<br \/>\nFalse<\/code><\/div>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 4<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">x = 10\r\n\r\nif x == 10:\r\n  print(x == 10)\r\nif x &gt; 5:\r\n  print(x &gt; 5)\r\n\r\nif x &lt; 10:\r\n  print(x &lt; 10)\r\nelse:\r\n  print(\"else\")<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"small-12 large-6 columns\">\n<div id=\"sol4\"><code class=\"codep \">True<br \/>\nTrue<br \/>\nelse<\/code><\/div>\n<div><\/div>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<p>&nbsp;<\/p>\n<p><strong>Exercise 5<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">x = \"1\"\r\n\r\nif x == 1:\r\n  print(\"one\")\r\nelif x == \"1\":\r\n  if int(x) &gt; 1:\r\n    print(\"two\")\r\n  elif int(x) &lt; 1:\r\n    print(\"three\")\r\n  else:\r\n    print(\"four\")\r\nif int(x) == 1:\r\n  print(\"five\")\r\nelse:\r\n  print(\"six\")<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div id=\"sol5\"><code class=\"codep \">four<br \/>\nfive<\/code><\/div>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 6<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">x = 1\r\ny = 1.0\r\nz = \"1\"\r\n\r\nif x == y:\r\n  print(\"one\")\r\nif y == int(z):\r\n  print(\"two\")\r\nelif x == y:\r\n  print(\"three\")\r\nelse:\r\n  print(\"four\")<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"small-12 large-6 columns\">\n<div id=\"sol6\"><code class=\"codep \">one<br \/>\ntwo<\/code><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 7<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true\">incipit = \"It was a dark and stormy night\"\r\n\r\nif 'it' in incipit:\r\n  print('one')\r\nelif 'ht\"' in incipit:\r\n  print('two')\r\nelse:\r\n  print('three')<\/pre>\n<p>&nbsp;<\/p>\n<p>three<\/p>\n<ul>\n<li>The program executes the <code><strong>else<\/strong><\/code> statement and outputs <code><strong>three<\/strong><\/code> to the console.<\/li>\n<li>The <code><strong>if<\/strong><\/code> condition is <code><strong>False<\/strong><\/code> because the lowercase substring <code><strong>'it'<\/strong><\/code> is not present in <code><strong>incipit<\/strong><\/code>.<\/li>\n<li>The <code><strong>elif<\/strong><\/code> condition is also <code><strong>False<\/strong><\/code>, because the substring <code><strong>'ht\"'<\/strong><\/code>includes a <strong>quotation mark<\/strong> that is not part of the string contained in the <code><strong>incipit<\/strong><\/code> variable.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Ex. 8<\/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\">a = 3\r\nb = '3'\r\nc = 3.0\r\n\r\nif int(b) == a:\r\n  print('A')\r\n\r\nif a == b:\r\n  print('B')\r\n\r\nif c == a:\r\n  print('C')<\/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-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<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><span class=\"pln\">A<\/span><\/p>\n<p><span class=\"pln\">C<\/span><\/p>\n<\/div>\n<\/div>\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\"><\/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 condition of the <strong>first<\/strong> <code><strong>if<\/strong><\/code> statement is <code><strong>True<\/strong><\/code>, because the <code><strong>int()<\/strong><\/code> function converts every literal integer, represented as a string, into the corresponding integer.<code><strong>int(b) == a <\/strong><\/code><strong>-&gt;<\/strong><code><strong> int('3') == 3 <\/strong><\/code><strong>-&gt;<\/strong><code><strong> 3 == 3 <\/strong><\/code><strong>-&gt;<\/strong><code><strong> True<\/strong><\/code>\n<p>Consequently, the program prints <code><strong>A<\/strong><\/code> on the screen.<\/li>\n<li>The condition of the <strong>second<\/strong> <code><strong>if<\/strong><\/code> statement is <strong>False<\/strong>:<code><strong>a == b <\/strong><\/code><strong>-&gt;<\/strong><code><strong> 3 == '3' <\/strong><\/code><strong>-&gt;<\/strong><code><strong> False<\/strong><\/code>\n<p>(the integer <code><strong>3<\/strong><\/code> cannot be equal to the string <code><strong>'3'<\/strong><\/code>).<\/li>\n<li>The condition of the <strong>third<\/strong> <code><strong>if<\/strong><\/code> statement is <code><strong>True<\/strong><\/code>, since the integer <code><strong>3<\/strong><\/code> is implicitly converted to float before comparison:<code><strong>c == a <\/strong><\/code><strong>-&gt;<\/strong><code><strong> 3.0 == 3 <\/strong><\/code><strong>-&gt;<\/strong><code><strong> 3.0 == 3.0 <\/strong><\/code><strong>-&gt;<\/strong><code><strong> True<\/strong><\/code>\n<p>The <code><strong>print()<\/strong><\/code> function outputs the letter <strong>C<\/strong> to console.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. The comparison (otherwise known as relational) operators are used to compare values. The table below illustrates how the comparison operators work, assuming that x = 0, y = 1, and z = 0:<\/p>\n","protected":false},"author":2,"featured_media":5932,"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\/5147"}],"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=5147"}],"version-history":[{"count":13,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5147\/revisions"}],"predecessor-version":[{"id":5933,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5147\/revisions\/5933"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/5932"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5147"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}