{"id":548,"date":"2023-02-04T18:51:02","date_gmt":"2023-02-04T18:51:02","guid":{"rendered":"http:\/\/yesrenee.com\/?p=548"},"modified":"2023-08-28T18:03:01","modified_gmt":"2023-08-28T18:03:01","slug":"python-order-of-operations","status":"publish","type":"post","link":"https:\/\/yesrenee.com\/?p=548","title":{"rendered":"Python Order of Operations"},"content":{"rendered":"\n<p>In math, we use order of operations. Multiplication and division goes first, unless brackets, then addition and subtraction. Python also has them, just with more rules.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The order of operations goes from highest to lowest rank, in order: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>parentheses<strong> ( 1+1 ) <\/strong>* 2<\/li><li>exponents <strong>1 ** 2<\/strong><\/li><li>unary plus and minus <em><strong>+a, -a<\/strong><\/em><\/li><li>multiplication, division, remainders ( <strong>1 * 2 \/ 1<\/strong> ) <strong>% 3<\/strong><\/li><li>addition and subtraction <strong>1 + 1 &#8211; 2<\/strong><\/li><li>comparing operators <strong>1 &lt; 2,  2 &gt; 1,  3 == 3,  1 != 2,  1 &lt;= 2 ,  2 &gt;= 1<\/strong><\/li><li>boolean NOT (not) <strong>x = True; not x False<\/strong><\/li><li>boolean AND (and) <strong>x = True; y = True; x and y = True<\/strong><\/li><li>boolean OR (or) <strong>x = True; y = False; x or y = True<\/strong><\/li><\/ol>\n\n\n\n<p>In Python order of operations, these go in order.<\/p>\n\n\n\n<p>Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = (6 + 7 * 2)\nprint(n)<\/code><\/pre>\n\n\n\n<p>First, multiply 7 by 2. The answer is 14. Then add 14 to 6, making 20. If your answer was 26, then you have not considered order of operations. In the list above, multiplication goes before addition. Try running this code to see if Python will answer with 20 or 26.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = (2 ** 3 \/ 4)\nprint(a)<\/code><\/pre>\n\n\n\n<p>Exponents go before division. 2 to the power of 3 (2 x 2 x 2) is 8. 8 \/ 4 is 2.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>m = (8 * 3 + (19 - 5))\nprint(m)<\/code><\/pre>\n\n\n\n<p>Should we multiply 8 by 3 first? After all, multiplication is before subtraction, right? But there are parentheses in the subtraction. Parentheses go before multiplication,  so we have to do the subtraction first. 19 &#8211; 5 is 14. Now the parentheses have been removed (we solved 19 &#8211; 5), we can do 8 * 3, which is 24. Then, 24 + 14 equals to 38.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h = (8 >= 8 or 9 == 10)\nprint(h)<\/code><\/pre>\n\n\n\n<p>In the first statement, 8 is bigger or equal to 8. So that&#8217;s True. But 9 is not 10, so now it&#8217;s True or False, which outputs False.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In math, we use order of operations. Multiplication and division goes first, unless brackets, then addition and subtraction. Python also has them, just with more rules. The order of operations goes from highest to lowest rank, in order: parentheses ( 1+1 ) * 2 exponents 1 ** 2 unary plus and minus +a, -a multiplication, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-daily","tag-python-programming"],"_links":{"self":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=548"}],"version-history":[{"count":2,"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions\/553"}],"wp:attachment":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}