

{"id":27292,"date":"2018-09-09T04:51:42","date_gmt":"2018-09-08T23:21:42","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=27292"},"modified":"2026-04-28T11:04:36","modified_gmt":"2026-04-28T05:34:36","slug":"python-operator-precedence","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/","title":{"rendered":"Python Operator Precedence &#8211; PEMDAS &amp; Short Circuiting"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In our last <a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\"><strong>Python tutorial<\/strong><\/a>, we discussed the Python sys Module. Today, we will see Python Operator Precedence. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Given an expression of multiple operators, how do you go about it? <\/span><span style=\"font-weight: 400\">What is 2+3*4%5-1? 13 or 3? To answer such questions, you will need to know what comes first. <\/span><\/p>\n<p><span style=\"font-weight: 400\">In this Precedence of Operator in Python tutorial, we address this issue. Moreover, we will learn PEMDAS and short-circuiting in Python. At last, we will discuss the associativity of Python Operators.<\/span><\/p>\n<p>So, let&#8217;s start the Python Operator Precedence tutorial.<\/p>\n<div id=\"attachment_27296\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-27296\" class=\"wp-image-27296 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg\" alt=\"Python Operator Precedence - PEMDAS &amp; Short Circuiting\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-27296\" class=\"wp-caption-text\">Python Operator Precedence &#8211; PEMDAS &amp; Short Circuiting<\/p><\/div>\n<p>First, let&#8217;s review a little about <strong><a href=\"https:\/\/data-flair.training\/blogs\/python-operators\/\">Python Operators<\/a><\/strong>.<\/p>\n<h3><strong>Python Operator Precedence Table<\/strong><\/h3>\n<p>Operator precedence is the order in which Python solves symbols when many appear on one line. Think of it as math class rules: multiplication before addition. In Python, parentheses have top priority, followed by exponentiation (**), then unary plus\/minus, multiplication\/division, addition\/subtraction, bitwise shifts, comparison, equality, logical and, logical or, and finally assignment.<\/p>\n<p><span style=\"font-weight: 400\">Take a look at the following table of Precedence in Python Operators:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">() \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(Highest precedence)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Parentheses (grouping)<\/span><\/td>\n<\/tr>\n<tr>\n<td><i><span style=\"font-weight: 400\">f<\/span><\/i><span style=\"font-weight: 400\">(args&#8230;)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Function call<\/span><\/td>\n<\/tr>\n<tr>\n<td><i><span style=\"font-weight: 400\">(expressions\u2026), [expressions\u2026], {key: value\u2026}, {expressions\u2026}<\/span><\/i><\/td>\n<td><span style=\"font-weight: 400\">Binding or tuple display, list display, dictionary display, set display<\/span><\/td>\n<\/tr>\n<tr>\n<td><i><span style=\"font-weight: 400\">x[index], x[index:index], x(arguments), x.attribute<\/span><\/i><\/td>\n<td><span style=\"font-weight: 400\">Subscription, slicing, call, attribute reference<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">await x<\/span><\/td>\n<td><span style=\"font-weight: 400\">Await expression<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">**<\/span><\/td>\n<td><span style=\"font-weight: 400\">Exponentiation<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">+<\/span><i><span style=\"font-weight: 400\">x<\/span><\/i><span style=\"font-weight: 400\">, &#8211;<\/span><i><span style=\"font-weight: 400\">x<\/span><\/i><span style=\"font-weight: 400\">, <\/span><i><span style=\"font-weight: 400\">~x<\/span><\/i><\/td>\n<td><span style=\"font-weight: 400\">Positive, negative, bitwise NOT<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">*, @, \/, \/\/, %<\/span><\/td>\n<td><span style=\"font-weight: 400\">Multiplication, division, remainder<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">+, &#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Addition, subtraction<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&lt;&lt;, &gt;&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise shifts<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&amp;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise AND<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">^<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise XOR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">|<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise OR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">in, not in, is, is not, &lt;, &lt;=, \u00a0&gt;, &gt;=,<\/span><br \/>\n<span style=\"font-weight: 400\">&lt;&gt;, !=, ==<\/span><\/td>\n<td><span style=\"font-weight: 400\">Comparisons, membership, identity<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">not <\/span><i><span style=\"font-weight: 400\">x<\/span><\/i><\/td>\n<td><span style=\"font-weight: 400\">Boolean NOT<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">and<\/span><\/td>\n<td><span style=\"font-weight: 400\">Boolean AND<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">or<\/span><\/td>\n<td><span style=\"font-weight: 400\">Boolean OR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">if- else<\/span><\/td>\n<td><span style=\"font-weight: 400\">Conditional expression<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">lambda \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(Lowest precedence)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Lambda expression<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In here, the operators in one cell evaluate <em>left to right<\/em> and exponentiation groups <em>right to left<\/em>.<\/p>\n<p>The lowest Precedence in Python Operator is the least binding, and the highest Precedence in Python Operator is the most.<\/p>\n<p>It is also true that we observe the same precedence for comparisons, membership tests, and identity tests. These also have a left-to-right chaining feature.<\/p>\n<h3><strong>What is a Python Expression?<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">Before we can tell you about which operator comes first, you\u2019d want to be introduced to expressions. An expression is a combination of values, variables, operators, and function calls. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Notably, the Python interpreter can evaluate a valid expression. Why don\u2019t we take an example?<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 4+3<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p><span style=\"font-weight: 400\">4+3 is an expression with one operator. We can also put in more than one. The precedence rules show us the way to follow an order. The division has a higher precedence than addition.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 3+3\/3<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4.0<\/div>\n<p><span style=\"font-weight: 400\">When we use parentheses, however, we can alter the order of execution here.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (3+3)\/3<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2.0<\/div>\n<p><span style=\"font-weight: 400\">What we conclude here is that using parentheses, we can force the operators of lower precedence to run first. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Or we can say that when two operators share an operand, the one with the higher precedence gets to go first.<\/span><\/p>\n<h3><strong>Python Operator Precedence &#8211; PEMDAS<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">If you\u2019re on this page reading about Python, you sure have heard about BODMAS somewhere in your journey so far (mathematics, school). In Python, however, we come across PEMDAS:<\/span><\/p>\n<p><strong>Parentheses<\/strong><br \/>\n<strong>Exponentiation<\/strong><br \/>\n<strong>Multiplication<\/strong><br \/>\n<strong>Division<\/strong><br \/>\n<strong>Addition<\/strong><br \/>\n<strong>Subtraction<\/strong><\/p>\n<p><span style=\"font-weight: 400\">A mnemonic to remember that will be \u201c<\/span><i><span style=\"font-weight: 400\">Please Excuse My Dear Aunt Susie<\/span><\/i><span style=\"font-weight: 400\">\u201d.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Let\u2019s take an example.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; ((((13+5)*2)-4)\/2)-13<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3.0<\/div>\n<p><span style=\"font-weight: 400\">How did that happen? Let\u2019s work it out.<\/span><br \/>\n<span style=\"font-weight: 400\">13+5 gives us 18<\/span><br \/>\n<span style=\"font-weight: 400\">18*2 gives us 36<\/span><br \/>\n<span style=\"font-weight: 400\">36-4 gives us 32<\/span><br \/>\n<span style=\"font-weight: 400\">32\/2 gives us 16.0 <\/span> <span style=\"font-weight: 400\">#Note that division gives us floats!<\/span><br \/>\n<span style=\"font-weight: 400\">16-13 gives us 3.0<\/span><\/p>\n<h3><strong>Python Short Circuiting<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">Python always evaluates the left operand before the right- even in function arguments. For expressions with <\/span><i><span style=\"font-weight: 400\">and<\/span><\/i><span style=\"font-weight: 400\"> or <\/span><span style=\"font-weight: 400\">operations, it uses short-circuiting. This means it evaluates the second operand only until it is needed. Because of this, such statements can work reliably:<\/span><\/p>\n<div id=\"attachment_27309\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-27309\" class=\"wp-image-27309 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01.jpg\" alt=\"Python Operator Precedence - Short Circuiting\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Short-Circuiting-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-27309\" class=\"wp-caption-text\">Python Operator Precedence &#8211; Short Circuiting<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; if(s!=None) and (len(s)&lt;10): pass<\/pre>\n<p><span style=\"font-weight: 400\">To short-circuit is to stop executing the Boolean operation if we have already arrived at the truth value of the expression. Let\u2019s take a look at this:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">X or Y- Evaluates Y only if X is false; otherwise, returns X<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">X and Y- Evaluates Y only if X is true; otherwise, returns X<\/span><\/li>\n<\/ul>\n<h4><strong>a. Short Circuiting with and\/or in Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">See what this gives us:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 0 or \"Hello\" and 1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>This doesn\u2019t give us \u201cHello\u201d, but 1, because:<br \/>\n0 or \u201cHello\u201d gives us \u201cHello\u201d<br \/>\n\u201cHello\u201d and 1 gives us 1<\/p>\n<h4><strong>b. Short-Circuiting with all()\/any() in Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">This also works with the all() and any() functions.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def check(i):\r\n      return i\r\n&gt;&gt;&gt; all(check(i) for i in [1,1,0,0,3])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p><span style=\"font-weight: 400\">This stops at the first False it gets (the 0 at the third position) and returns False.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; any(check(i) for i in [0,0,0,1,3])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p><span style=\"font-weight: 400\">This stops at the first True it gets (the 1 at the fourth position) and returns True.<\/span><\/p>\n<h4><strong>c. Short Circuiting with Conditional Operators in Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Watch how this unfurls with conditional operators like &gt; and &lt;.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 7&gt;8&gt;check(4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p><span style=\"font-weight: 400\">This stops at 7&gt;8 and returns False.<\/span><\/p>\n<h4><strong>d. Short-Circuiting with Ternary Operators<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Now, consider the following expression, which is a <\/span>ternary operator<span style=\"font-weight: 400\">:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; print(\"One\") if print(\"Two\") else print(\"Three\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Two<br \/>\nThree<\/div>\n<p><span style=\"font-weight: 400\">What happens here? Let\u2019s find out.<\/span><br \/>\n<span style=\"font-weight: 400\">Python first checks the condition, print(\u201cTwo\u201d). In evaluating this, it prints \u201cTwo\u201d. Also, the Boolean value for this is False:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; bool(print(\"Two\"))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Two<br \/>\nFalse<\/div>\n<p><span style=\"font-weight: 400\">Since it is False, it does not evaluate print(\u201cOne\u201d) and simply evaluates print(\u201cThree\u201d).<\/span><br \/>\n<span style=\"font-weight: 400\">Hence, the final output we get is:<\/span><br \/>\n<span style=\"font-weight: 400\">Two<\/span><br \/>\n<span style=\"font-weight: 400\">Three<\/span><\/p>\n<h3><strong>Associativity of Operators in Python<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">In that table above, many cells had more than one operator. These share precedence. So then, which to evaluate first? <\/span><\/p>\n<p><span style=\"font-weight: 400\">Associativity comes to the rescue here. Many operators have left-to-right associativity.<\/span><\/p>\n<p><strong>Key Performance Rules:<\/strong><\/p>\n<ul>\n<li><strong>Same priorities:<\/strong> When operators like multiplication and division have the same priorities, Python solves them from left to right.<\/li>\n<li><strong>Exponent exception:<\/strong> Exponent works differently from others; it is solved from right to left.<\/li>\n<li><strong>Chaining operations:<\/strong> The like &lt;, &gt;, ==, etc., are being checked after arithmetic operations but before logical operations.<\/li>\n<li><strong>Use of parentheses:<\/strong> You can use brackets to control the order of calculation. The part inside this bracket should be solved first.<\/li>\n<\/ul>\n<h4><strong>a. Associative Operators in Python<\/strong><\/h4>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Multiplication (*) and Floor Division (\/\/)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">For an example, let\u2019s consider the operators multiplication(*) and floor division(\/\/). Watch how the left operand evaluates first:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 3*5\/\/4<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 3*(5\/\/4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p><span style=\"font-weight: 400\">While both give us the same result, they do that in different ways. Watch how:<\/span><br \/>\n<span style=\"font-weight: 400\">For the first example:<\/span><br \/>\n<span style=\"font-weight: 400\">3*5 gives us 15<\/span><br \/>\n<span style=\"font-weight: 400\">15\/\/4 gives us 3<\/span><br \/>\n<span style=\"font-weight: 400\">For the second example:<\/span><br \/>\n<span style=\"font-weight: 400\">5\/\/4 gives us 1<\/span><br \/>\n<span style=\"font-weight: 400\">3*1 gives us 3<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Exponentiation (**)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Now, let\u2019s try this on exponentiation:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (2**3)**2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">64<\/div>\n<p><span style=\"font-weight: 400\">And now without parentheses:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 2**3**2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">512<\/div>\n<p><span style=\"font-weight: 400\">This is because this is equivalent to:<\/span><br \/>\n<span style=\"font-weight: 400\">2**(3**2)<\/span><br \/>\n<span style=\"font-weight: 400\">This gives us 2**9<\/span><br \/>\n<span style=\"font-weight: 400\">This gives us 512<\/span><\/p>\n<h4><strong>b. Non-Associative Operators in Python<\/strong><\/h4>\n<p><span style=\"font-weight: 400\">Assignment and comparison operators are not associative. What this means is that x&lt;y&lt;z is none of the following:<\/span><br \/>\n<span style=\"font-weight: 400\">(x&lt;y)&lt;z<\/span><br \/>\n<span style=\"font-weight: 400\">x&lt;(y&lt;z)<\/span><br \/>\n<span style=\"font-weight: 400\">This expression is actually equivalent to (and this evaluates left-to-right):<\/span><br \/>\n<span style=\"font-weight: 400\">x&lt;y and y&lt;z<\/span><\/p>\n<p>So, this was all in Python Operator Precedence. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Operator Precedence<\/h3>\n<p>1. What is operator precedence in Python?<\/p>\n<p>2. How does precedence work in Python?<\/p>\n<p>3. Which Python operator has the lowest precedence?<\/p>\n<p>4. What is the precedence of Python operators?<\/p>\n<p>5. Which Python operator has the highest precedence?<\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">Hence, in this Python Operator Precedence tutorial, we learned about operator precedence, PEMDAS, and short-circuiting. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, we discussed expressions and associativity in Python Operator Precedence. <\/span><\/p>\n<p><span style=\"font-weight: 400\">What would you like to read on DataFlair? Do let us know in the comments below.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last Python tutorial, we discussed the Python sys Module. Today, we will see Python Operator Precedence. Given an expression of multiple operators, how do you go about it? What is 2+3*4%5-1? 13&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":27296,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[9455,10528,10736,10737,10740,10741],"class_list":["post-27292","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-pemdas","tag-python-expression","tag-python-operator","tag-python-operator-associativity","tag-python-operator-precedence","tag-python-operator-precedence-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Operator Precedence - PEMDAS &amp; Short Circuiting - DataFlair<\/title>\n<meta name=\"description\" content=\"Given an expression of multiple operators, how do you go about it? Let&#039;s see Python Operator Precedence, PEMDAS, and short-circuiting.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Operator Precedence - PEMDAS &amp; Short Circuiting - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Given an expression of multiple operators, how do you go about it? Let&#039;s see Python Operator Precedence, PEMDAS, and short-circuiting.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-08T23:21:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T05:34:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Operator Precedence - PEMDAS &amp; Short Circuiting - DataFlair","description":"Given an expression of multiple operators, how do you go about it? Let's see Python Operator Precedence, PEMDAS, and short-circuiting.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/","og_locale":"en_US","og_type":"article","og_title":"Python Operator Precedence - PEMDAS &amp; Short Circuiting - DataFlair","og_description":"Given an expression of multiple operators, how do you go about it? Let's see Python Operator Precedence, PEMDAS, and short-circuiting.","og_url":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-09-08T23:21:42+00:00","article_modified_time":"2026-04-28T05:34:36+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Operator Precedence &#8211; PEMDAS &amp; Short Circuiting","datePublished":"2018-09-08T23:21:42+00:00","dateModified":"2026-04-28T05:34:36+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/"},"wordCount":1224,"commentCount":11,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg","keywords":["PEMDAS","Python Expression","python operator","Python Operator associativity","python operator precedence","python operator precedence example"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/","url":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/","name":"Python Operator Precedence - PEMDAS &amp; Short Circuiting - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg","datePublished":"2018-09-08T23:21:42+00:00","dateModified":"2026-04-28T05:34:36+00:00","description":"Given an expression of multiple operators, how do you go about it? Let's see Python Operator Precedence, PEMDAS, and short-circuiting.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-operator-precedence\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-Operator-Precedence-01.jpg","width":1200,"height":628,"caption":"Python Operator Precedence - PEMDAS &amp; Short Circuiting"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-operator-precedence\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Operator Precedence &#8211; PEMDAS &amp; Short Circuiting"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/27292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=27292"}],"version-history":[{"count":15,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/27292\/revisions"}],"predecessor-version":[{"id":147974,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/27292\/revisions\/147974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/27296"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=27292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=27292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=27292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}