Or & and

In addition to the operators that we saw in the Comparison lesson, we will now add 3 more logical (Boolean) operators to the table:

Symbol Example Description
and x and y True if both operands are true
or x or y True if either operand is true
not not x True if the operand is false
(it negates the operand)
# This program gives naive life advice.

print('Answer "yes" or "no".')
happy_status = input('Are you happy?')
if happy_status == 'yes':
    happy = True
elif happy_status == 'no':
    happy = False
else:
    print('I do not understand!')

rich_status = input('Are you rich?')
if rich_status == 'yes':
    rich = True
elif rich_status == 'no':
    rich = False
else:
    print('I do not understand!')

if rich and happy:
    # rich and at the same time.
    print('Congratulations!')
elif rich:
    # rich but not "rich and happy",
    #so must be only rich.
    print('Try to smile more.')
elif happy:
    # must be only happy.
    print('Try to spend less.')
else:
    # neither happy nor rich.
    print("I'm sorry for you.")

What happens if you answer something other than "Yes" or "No"?

The variables happy and rich won't be set, and later when they are needed, the program will end with an error.

We will learn how to handle errors next time.

{
  "data": {
    "sessionMaterial": {
      "id": "session-material:2018/pyladies-en-prague:loops:1",
      "title": "Or & and",
      "html": "\n          \n    \n\n    <h1><em>Or</em> &amp; <em>and</em></h1>\n<p>In addition to the operators that we saw in the Comparison lesson, we will now add 3 more logical (Boolean) operators to the table:</p>\n<table class=\"table\">\n    <tbody><tr>\n        <th>Symbol</th>\n        <th>Example</th>\n        <th>Description</th>\n    </tr>\n    <tr>\n        <td><code>and</code></td>\n        <td><code>x and y</code></td>\n        <td>True if both operands are true</td>\n    </tr>\n    <tr>\n        <td><code>or</code></td>\n        <td><code>x or y</code></td>\n        <td>True if either operand is true</td>\n    </tr>\n    <tr>\n        <td><code>not</code></td>\n        <td><code>not x</code></td>\n        <td>True if the operand is false<br> \n        (it negates the operand)</td>\n    </tr>\n</tbody></table><div class=\"highlight\"><pre><span></span><span class=\"c1\"># This program gives naive life advice.</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;Answer &quot;yes&quot; or &quot;no&quot;.&apos;</span><span class=\"p\">)</span>\n<span class=\"n\">happy_status</span> <span class=\"o\">=</span> <span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">&apos;Are you happy?&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">if</span> <span class=\"n\">happy_status</span> <span class=\"o\">==</span> <span class=\"s1\">&apos;yes&apos;</span><span class=\"p\">:</span>\n    <span class=\"n\">happy</span> <span class=\"o\">=</span> <span class=\"bp\">True</span>\n<span class=\"k\">elif</span> <span class=\"n\">happy_status</span> <span class=\"o\">==</span> <span class=\"s1\">&apos;no&apos;</span><span class=\"p\">:</span>\n    <span class=\"n\">happy</span> <span class=\"o\">=</span> <span class=\"bp\">False</span>\n<span class=\"k\">else</span><span class=\"p\">:</span>\n    <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;I do not understand!&apos;</span><span class=\"p\">)</span>\n\n<span class=\"n\">rich_status</span> <span class=\"o\">=</span> <span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">&apos;Are you rich?&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">if</span> <span class=\"n\">rich_status</span> <span class=\"o\">==</span> <span class=\"s1\">&apos;yes&apos;</span><span class=\"p\">:</span>\n    <span class=\"n\">rich</span> <span class=\"o\">=</span> <span class=\"bp\">True</span>\n<span class=\"k\">elif</span> <span class=\"n\">rich_status</span> <span class=\"o\">==</span> <span class=\"s1\">&apos;no&apos;</span><span class=\"p\">:</span>\n    <span class=\"n\">rich</span> <span class=\"o\">=</span> <span class=\"bp\">False</span>\n<span class=\"k\">else</span><span class=\"p\">:</span>\n    <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;I do not understand!&apos;</span><span class=\"p\">)</span>\n\n<span class=\"k\">if</span> <span class=\"n\">rich</span> <span class=\"ow\">and</span> <span class=\"n\">happy</span><span class=\"p\">:</span>\n    <span class=\"c1\"># rich and at the same time.</span>\n    <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;Congratulations!&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">elif</span> <span class=\"n\">rich</span><span class=\"p\">:</span>\n    <span class=\"c1\"># rich but not &quot;rich and happy&quot;,</span>\n    <span class=\"c1\">#so must be only rich.</span>\n    <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;Try to smile more.&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">elif</span> <span class=\"n\">happy</span><span class=\"p\">:</span>\n    <span class=\"c1\"># must be only happy.</span>\n    <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;Try to spend less.&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">else</span><span class=\"p\">:</span>\n    <span class=\"c1\"># neither happy nor rich.</span>\n    <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;I&apos;m sorry for you.&quot;</span><span class=\"p\">)</span>\n</pre></div><div class=\"admonition note\"><p>What happens if you answer something other than &quot;Yes&quot; or &quot;No&quot;?</p>\n<p>The variables <code>happy</code> and <code>rich</code> won&apos;t be set, and later when they are needed, the program will end with an error.</p>\n<p>We will learn how to handle errors <a href=\"/2018/pyladies-en-prague/beginners/exceptions/\">next time</a>.</p>\n</div>\n\n\n        "
    }
  }
}