{"id":82,"date":"2021-03-11T16:54:09","date_gmt":"2021-03-11T16:54:09","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=82"},"modified":"2024-11-15T10:10:00","modified_gmt":"2024-11-15T10:10:00","slug":"conditions","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/conditions\/","title":{"rendered":"Conditions"},"content":{"rendered":"<p>These are used in <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/basics\/decisions\/\">decisions (if commands)<\/a> and <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/basics\/conditional-loops\/\">conditional loops<\/a><\/p>\n<h1>Simple Conditions<\/h1>\n<p>Simple conditions only have one condition<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\n# equals, is the same as\r\nif name == &quot;fred&quot;:\r\n\r\n# is not equal to, is not the same as\r\nif name != &quot;fred&quot;:\r\n\r\n# is less than\r\nif age &lt; 18:\r\n\r\n# is less than or equal to\r\nif age &lt;= 18:\r\n\r\n# is greater than\r\nif age &gt; 18:\r\n\r\n# is greater than or equal to\r\nif age &gt;= 18:\r\n\r\n# is between two numbers (inclusive)\r\nif age&gt;=13 and age&lt;=18:\r\n \r\n# is not between two numbers (inclusive) \r\nif age&lt;13 or age&gt;18:\r\n \r\n#<\/pre>\n<h1>Complex Conditions<\/h1>\n<p>Complex conditions have more than one condition, using AND, OR or NOT<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\n# and - all conditions must be met\r\nif age&gt;= 13 and age&lt;=14:\r\n \r\n# or - any of the conditions must be met\r\nif age==13 or age==14:\r\n \r\n# not - the condition must not be met\r\nif not (age==13 or age==14):\r\n#<\/pre>\n<p>Each condition must be in full:<\/p>\n<pre><span style=\"color: #339966\">if age==13 or age==14:<\/span><\/pre>\n<p>You cannot write:<\/p>\n<pre><span style=\"color: #ff0000\">if age==13 or 14:<\/span><\/pre>\n<h2>Ambiguity<\/h2>\n<p>Conditions with both an \u201cand\u201d and an \u201cor\u201d are ambiguous (more than one meaning).<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">#\r\nabsent=True and year=&quot;S1&quot; or year=&quot;S2&quot;\r\n#<\/pre>\n<p>Brackets should be used to removed the ambiguity.<\/p>\n<p>Pupils in S1 who are absent, and all pupils in S2:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">#\r\n(absent=True and year=&quot;S1&quot;) or year=&quot;S2&quot; \r\n#<\/pre>\n<p>All absent pupils in either S1 or S2:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">#\r\nabsent=True and (year=&quot;S1&quot; or year=&quot;S2&quot;)\r\n#<\/pre>\n<h1>List Conditions<\/h1>\n<p>Instead of using lots of ors or ands:<\/p>\n<pre><span style=\"color: #ff0000\">if age==13 or age==18 or age==21 or age==50:\r\nif name!='fred' and name!='sue' and name!='mark':<\/span><\/pre>\n<p>lists can be used:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\n# in the list\r\nif age in &#x5B;13, 18,21, 50]:\r\nif name in &#x5B;&#039;fred&#039;, &#039;sue&#039;, &#039;mark&#039;]:\r\n \r\n# not in the list\r\nif age not in &#x5B;13, 18,21, 50]:\r\nif name not in &#x5B;&#039;fred&#039;, &#039;sue&#039;, &#039;mark&#039;]: #\r\n#<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>These are used in decisions (if commands) and conditional loops Simple Conditions Simple conditions only have one condition # # equals, is the same as if name == &quot;fred&quot;: # is not equal to, is not the same as if name != &quot;fred&quot;: # is less than if age &lt; 18: # is less than or equal to if age &lt;= 18: # is greater than if age &gt; 18:<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/conditions\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":294,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-82","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":22,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/82\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/82\/revisions\/757"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/294"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}