{"id":2034,"date":"2020-12-30T09:13:38","date_gmt":"2020-12-30T09:13:38","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2034"},"modified":"2020-12-30T09:28:29","modified_gmt":"2020-12-30T09:28:29","slug":"python-or","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-or\/","title":{"rendered":"Python or Operator"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>or<\/code> operator and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-or-operator'>Introduction to the Python or operator <a href=\"#introduction-to-the-python-or-operator\" class=\"anchor\" id=\"introduction-to-the-python-or-operator\" title=\"Anchor for Introduction to the Python &lt;code&gt;or&lt;\/code&gt; operator\">#<\/a><\/h2>\n\n\n\n<p>The <code>or<\/code> operator is a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-logical-operators\/\">logical operator<\/a>. Typically, you use the <code>or<\/code> operator to combine two Boolean expressions and return a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-boolean\/\">Boolean value<\/a>.<\/p>\n\n\n\n<p>The <code>or<\/code> operator returns <code>True<\/code> if one of the two operands is <code>True<\/code>. And it returns <code>False<\/code> only if both operands are <code>False<\/code>.<\/p>\n\n\n\n<p>This truth table displays the result of the <code>or<\/code> operator:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>x<\/th><th>y<\/th><th>x or y<\/th><\/tr><\/thead><tbody><tr><td>True<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>True<\/td><td>False<\/td><td>True<\/td><\/tr><tr><td>False<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>False<\/td><td>False<\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example shows how to use the <code>or<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">is_admin = <span class=\"hljs-keyword\">False<\/span>\nis_editor = <span class=\"hljs-keyword\">True<\/span>\ncan_edit = is_admin <span class=\"hljs-keyword\">or<\/span> is_editor\n\n<span class=\"hljs-keyword\">print<\/span>(can_edit)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-python-or-operator-is-short-circuiting'>The Python or operator is short-circuiting <a href=\"#the-python-or-operator-is-short-circuiting\" class=\"anchor\" id=\"the-python-or-operator-is-short-circuiting\" title=\"Anchor for The Python or operator is short-circuiting\">#<\/a><\/h2>\n\n\n\n<p>When evaluating an expression that involves the <code>or<\/code> operator, Python can sometimes determine the result without evaluating all the operands. This is called short-circuit evaluation or lazy evaluation.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">x or y<\/code><\/span><\/pre>\n\n\n<p>If <code>x<\/code> is truthy, then the <code>or<\/code> operator returns <code>x<\/code>. Otherwise, it returns <code>y<\/code>.<\/p>\n\n\n\n<p>In other words, if <code>x<\/code> is truthy, then the <code>or<\/code> operator doesn&#8217;t need to evaluate <code>y<\/code>. It just returns <code>x<\/code> immediately. This is why the evaluation is called lazy or short-circuiting evaluation.<\/p>\n\n\n\n<p>The <code>or<\/code> operator only evaluates <code>y<\/code> and returns the result of the evaluation if <code>x<\/code> is falsy.<\/p>\n\n\n\n<p>In Python, <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-bool\/\">every object associates with a Boolean value<\/a>. And the <code>x<\/code> and <code>y<\/code> can be any object. <\/p>\n\n\n\n<p>This opens some useful applications of the <code>or<\/code> operator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='setting-a-default-value-for-a-variable'>Setting a default value for a variable <a href=\"#setting-a-default-value-for-a-variable\" class=\"anchor\" id=\"setting-a-default-value-for-a-variable\" title=\"Anchor for Setting a default value for a variable\">#<\/a><\/h2>\n\n\n\n<p>The <code>or<\/code> operator allows you to set a default value for a variable, for example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">var_name = value or <span class=\"hljs-keyword\">default<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, if <code>value<\/code> is falsy, the <code>or<\/code> operator return the <code>default<\/code>.<\/p>\n\n\n\n<p>The following example prompts you for input. If you don&#8217;t enter anything, the <code>lang<\/code> will default to <code>'Python'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">lang = input(<span class=\"hljs-string\">'Enter your language:'<\/span>) <span class=\"hljs-keyword\">or<\/span> <span class=\"hljs-string\">'Python'<\/span>\n<span class=\"hljs-keyword\">print<\/span>(lang)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example defines a function <code>get_data()<\/code> that returns a list of numbers. It uses the built-in <code>min()<\/code> function to find the lowest element in the list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def get_data(args=None):\n    <span class=\"hljs-keyword\">if<\/span> args:\n        <span class=\"hljs-keyword\">return<\/span> &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\n    <span class=\"hljs-keyword\">return<\/span> &#91;]\n\n\nlowest = min(get_data(args=<span class=\"hljs-keyword\">true<\/span>))\n<span class=\"hljs-keyword\">print<\/span>(lowest)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">1<\/code><\/span><\/pre>\n\n\n<p>It returned 1 as expected. However, the <code>get_data()<\/code> may return an empty list like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">lowest = min(get_data())\n<span class=\"hljs-keyword\">print<\/span>(lowest)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returned a <code>ValueError<\/code>.<\/p>\n\n\n\n<p>To fix this, you can use the <code>or<\/code> operator when calling the <code>min()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def get_data(args=None):\n    <span class=\"hljs-keyword\">if<\/span> args:\n        <span class=\"hljs-keyword\">return<\/span> &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\n    <span class=\"hljs-keyword\">return<\/span> &#91;]\n\n\nlowest = min(get_data() <span class=\"hljs-keyword\">or<\/span> &#91;<span class=\"hljs-number\">0<\/span>])\n<span class=\"hljs-keyword\">print<\/span>(lowest)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">0<\/code><\/span><\/pre>\n\n\n<p>In this example, if the get_data() function returns an empty list, the <code>or<\/code> operator will treat its result as a falsy value.<\/p>\n\n\n\n<p>Since the first operand is falsy, the <code>or<\/code> operator needs to evaluate the second operand <code>[0]<\/code>. In this case, you can specify the default minimum value in the second operand.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2034\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-or\/\"\n\t\t\t\tdata-post-title=\"Python or Operator\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2034\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-or\/\"\n\t\t\t\tdata-post-title=\"Python or Operator\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python or operator and how to use it effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":29,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2034","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2034"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2034\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}