{"id":7005,"date":"2024-04-11T12:01:34","date_gmt":"2024-04-11T12:01:34","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7005"},"modified":"2024-04-11T12:01:34","modified_gmt":"2024-04-11T12:01:34","slug":"precedence-and-associativity-of-operators-in-python","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/precedence-and-associativity-of-operators-in-python\/","title":{"rendered":"Precedence and Associativity of Operators in Python"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-operator-precedence-in-python\">What is Operator Precedence in Python?<\/a><\/li><li ><a href=\"#precedence-of-python-operators\">Precedence of Python Operators<\/a><\/li><li ><a href=\"#precedence-of-logical-operators-in-python\">Precedence of logical operators in Python<\/a><\/li><li ><a href=\"#associativity-of-the-python-operators\">Associativity of the Python Operators<\/a><\/li><li ><a href=\"#operators-precedence-and-associativity-in-python\">Operators&#8217; Precedence and Associativity in Python<\/a><\/li><li ><a href=\"#what-are-non-associative-operators\">What are non-associative operators?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#precedence-and-associativity-of-operators-in-python-fa-qs\">Precedence and Associativity of Operators in Python- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>The arithmetic operator will take precedence over the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/\" data-type=\"post\" data-id=\"6978\">logical operator<\/a>.  Initially, python will check the<a href=\"https:\/\/www.skillvertex.com\/blog\/python-arithmetic-operators\/\" data-type=\"post\" data-id=\"6958\"> arithmetic operators<\/a>.  Relational operators will be checked next.  At the end,  logical operators will be evaluated. <\/p>\n\n\n\n<p>Therefore, if multiple operators are present in an expression, then the higher precedence will be checked first. However, the associativity will be given more importance while considering the order of evaluation in the operator. Let us look into this article to learn more about the precedence and Associativity of operators in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-operator-precedence-in-python\">What is Operator Precedence in Python?<\/h2>\n\n\n\n<p>In Python,  an expression is of <a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\">variables<\/a>, <a href=\"https:\/\/www.skillvertex.com\/blog\/operators-in-c\/\" data-type=\"post\" data-id=\"1918\">operators<\/a>, and <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-pass-an-array-by-value-in-c\/\" data-type=\"post\" data-id=\"2638\">values<\/a>. The <a href=\"https:\/\/www.skillvertex.com\/blog\/how-does-a-python-interpreter-works\/\" data-type=\"post\" data-id=\"6895\">Python interpreter <\/a>will come across an expression that has various operations and that will be evaluated with the ordered hierarchy. This process is defined as operator precedence.<\/p>\n\n\n\n<p>The table below has provided all the operators from the highest precedence to the lowest precedence.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong><em>Precedence<\/em><\/strong><\/th><th><strong><em>Operators<\/em><\/strong><\/th><th><strong><em>Description<\/em><\/strong><\/th><th><strong><em>Associativity<\/em><\/strong><\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>()<\/td><td>Parentheses<\/td><td>Left to right<\/td><\/tr><tr><td>2<\/td><td>x[index],&nbsp;x[index:index]<\/td><td>Subscription, slicing<\/td><td>Left to right<\/td><\/tr><tr><td>3<\/td><td>await x<\/td><td>Await expression<\/td><td>N\/A<\/td><\/tr><tr><td>4<\/td><td>**<\/td><td>Exponentiation<\/td><td>Right to left<\/td><\/tr><tr><td>5<\/td><td>+x,&nbsp;-x,&nbsp;~x<\/td><td>Positive, negative, bitwise NOT<\/td><td>Right to left<\/td><\/tr><tr><td>6<\/td><td>*,&nbsp;@,&nbsp;\/,&nbsp;\/\/,&nbsp;%<\/td><td>Multiplication, matrix, division, floor division, remainder<\/td><td>Left to right<\/td><\/tr><tr><td>7<\/td><td><a href=\"https:\/\/www.skillvertex.com\/blog\/python-operators\/\" data-type=\"post\" data-id=\"6937\"><strong>+<\/strong>,&nbsp;<strong>\u2013<\/strong><\/a><\/td><td>Addition and subtraction<\/td><td>Left to right<\/td><\/tr><tr><td>8<\/td><td>&lt;&lt;,&nbsp;&gt;&gt;<\/td><td>Shifts<\/td><td>Left to right<\/td><\/tr><tr><td>9<\/td><td>&amp;<\/td><td>Bitwise AND<\/td><td>Left to right<\/td><\/tr><tr><td>10<\/td><td><strong>^<\/strong><\/td><td>Bitwise XOR<\/td><td>Left to right<\/td><\/tr><tr><td>11<\/td><td><strong>|<\/strong><\/td><td>Bitwise OR<\/td><td>Left to right<\/td><\/tr><tr><td>12<\/td><td>in,&nbsp;not in,&nbsp;is,&nbsp;is not,&nbsp;&lt;,&nbsp;&lt;=,&nbsp;&gt;,&nbsp;&gt;=,&nbsp;!=,&nbsp;==<\/td><td>Comparisons, membership tests, identity tests<\/td><td>Left to Right<\/td><\/tr><tr><td>13<\/td><td>not x<\/td><td>Boolean NOT<\/td><td>Right to left<\/td><\/tr><tr><td>14<\/td><td>and<\/td><td>Boolean AND<\/td><td>Left to right<\/td><\/tr><tr><td>15<\/td><td>or<\/td><td>Boolean OR<\/td><td>Left to right<\/td><\/tr><tr><td>16<\/td><td>if-else<\/td><td>Conditional expression<\/td><td>Right to left<\/td><\/tr><tr><td>17<\/td><td>lambda<\/td><td>Lambda expression<\/td><td>N\/A<\/td><\/tr><tr><td>18<\/td><td><strong>:=<\/strong><\/td><td>Assignment expression (walrus operator)<\/td><td>Right to left<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"precedence-of-python-operators\">Precedence of Python Operators<\/h2>\n\n\n\n<p>It is commonly used in the expression that has more than one operator with different precedence for identifying which operator will be required to do first.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 + 20 * 30\n<\/code><\/pre>\n\n\n\n<p> output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 + 20 * 30 is calculated as 10 + (20 * 30)\nand not as (10 + 20) * 30<\/code><\/pre>\n\n\n\n<p><strong>Python code for the above example<\/strong><\/p>\n\n\n\n<p># Precedence of &#8216;+&#8217; &amp; &#8216;*&#8217;<\/p>\n\n\n<div class=\"gb-container gb-container-464787a9\">\n\n<p>expr =10+20*30<\/p>\n\n<\/div>\n\n\n<p>print(expr)<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>610<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"precedence-of-logical-operators-in-python\">Precedence of logical operators in Python<\/h2>\n\n\n\n<p>The if block will be run in the code provided below even though the age is 0.  The precedence of <a href=\"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/\" data-type=\"post\" data-id=\"6978\">logical<\/a> &#8220;<strong><a href=\"https:\/\/www.skillvertex.com\/blog\/and-operators-in-c\/\" data-type=\"post\" data-id=\"3294\">and<\/a><\/strong>&#8221; will be greater than the logical &#8220;<strong><a href=\"https:\/\/www.skillvertex.com\/blog\/python-identity-operator\/\" data-type=\"post\" data-id=\"6997\">or<\/a><\/strong>&#8220;.<\/p>\n\n\n\n<p># Precedence of &#8216;or&#8217; &amp; &#8216;and&#8217;<\/p>\n\n\n\n<p>name =&#8221;Alex&#8221;<\/p>\n\n\n\n<p>age =0<\/p>\n\n\n\n<p>ifname ==&#8221;Alex&#8221;orname ==&#8221;John&#8221;andage &gt;=2:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Hello! Welcome.&#8221;<code>)<\/code><\/p>\n\n\n\n<p>else<code>:<\/code><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Good Bye!!&#8221;<code>)<\/code><\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello! Welcome.\n<\/code><\/pre>\n\n\n\n<p>Another way to execute the &#8216;<a href=\"https:\/\/www.skillvertex.com\/blog\/c-ifelse-statement\/\" data-type=\"post\" data-id=\"2270\">else<\/a>&#8216; block is the use of parenthesis (). It will be considered as their precedence and the highest among all the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-membership-operator\/\" data-type=\"post\" data-id=\"6992\">operators<\/a>.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Precedence of 'or' &amp; 'and'\nname = \"John\"\nage = 0\n \nif (name == \" John\" or name == \" Albert\") and age &gt;= 2:\n    print(\"Hello! Welcome.\")\nelse:\n    print(\"Good Bye!!\")<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<p>Good Bye!!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"associativity-of-the-python-operators\">Associativity of the Python Operators<\/h2>\n\n\n\n<p>The expression has two or more <a href=\"https:\/\/www.skillvertex.com\/blog\/python-comparison-operators\/\" data-type=\"post\" data-id=\"6966\">operators<\/a> with the same precedence, and then the operator associativity will determine it as either to be left or reft or from right to left.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>&#8216;*&#8217; and &#8216; \/&#8217;   consist of the same precedence and the associativity will be from Left to Right in the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Left-associative operators\na = 10 + 5 - 2  # Addition and subtraction are left-associative\nprint(a)  # Output: 13\n\nb = 2 * 3 \/ 2  # Multiplication and division are left-associative\nprint(b)  # Output: 3.0\n\n# Right-associative operators (only one example in Python, exponentiation)\nc = 2 ** 3 ** 2  # Exponentiation is right-associative\nprint(c) \n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>13\n3.0\n512<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"operators-precedence-and-associativity-in-python\">Operators&#8217; Precedence and Associativity in Python<\/h2>\n\n\n\n<p>In <a href=\"https:\/\/www.skillvertex.com\/blog\/python-unicode-system\/\" data-type=\"post\" data-id=\"6949\">Python<\/a>, Operators&#8217; precedence and Associativity are considered the two important characteristics of operators that will be used to check the order of subexpression in the absence of brackets.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>100 + 200 \/ 10 - 3 * 10\n\n100 + 200 \/ 10 - 3 * 10 is calculated as 100 + (200 \/ 10) - (3 * 10)\nand not as (100 + 200) \/ (10 - 3) * 10\n<\/code><\/pre>\n\n\n\n<p><strong> Python code for the example above <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>expression = 100 + 200 \/ 10 - 3 * 10\nprint(expression)<\/code><\/pre>\n\n\n\n<p> Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>90.0\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-non-associative-operators\">What are non-associative operators?<\/h2>\n\n\n\n<p>In Python, most of the operators have associativity and this indicates that it can be used to evaluate from left to right or right to left and have the same precedence.  <\/p>\n\n\n\n<p>Therefore, few operators are considered as non- associative and this means that they cannot be chained together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 5\nb = 10\nc = 15\n \na = b = (a &lt; b) += (b &lt; c)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = b= (a &lt; b) += (b &lt; c)\n                   ^^\nSyntaxError: invalid syntax<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, this article has listed about the operator precedence and associativity in Python. Moreover, several examples are also provided for a better understanding of the operator&#8217;s precedence. This can allow the students to improve their knowledge and skills regarding the topic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"precedence-and-associativity-of-operators-in-python-fa-qs\">Precedence and Associativity of Operators in Python- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707720693440\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. Is Python right associative?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The associativity will be either from left to right or right to left.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707720700285\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is precedence in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It denotes the order of precedence. Python has divided the operators into several categories such as  Arithmetic operators and assignment operators.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707720708699\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.  What is the highest precedence?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  Parentheses will have the highest precedence. Higher precedence operators will be performed before the lower precedence operations.  <\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The arithmetic operator will take precedence over the logical operator. Initially, python will check the arithmetic operators. Relational operators will be checked next. At the end, logical operators will be evaluated. Therefore, if multiple operators are present in an expression, then the higher precedence will be checked first. However, the associativity will be given more &#8230; <a title=\"Precedence and Associativity of Operators in Python\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/precedence-and-associativity-of-operators-in-python\/\" aria-label=\"More on Precedence and Associativity of Operators in Python\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7007,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[897,893,57,892],"class_list":["post-7005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-arithmetic-operator","tag-logical-operator","tag-python","tag-python-operator","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7005"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7005\/revisions"}],"predecessor-version":[{"id":8907,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7005\/revisions\/8907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7007"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}