{"id":23080,"date":"2021-11-01T11:05:00","date_gmt":"2021-11-01T11:05:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=23080"},"modified":"2021-11-18T15:14:07","modified_gmt":"2021-11-18T15:14:07","slug":"plus-equal-operator","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/plus-equal-operator","title":{"rendered":"The += Operator In Python &#8211; A Complete Guide"},"content":{"rendered":"\n<p>In this lesson, we will look at the <strong>+= operator <\/strong>in Python and see how it works with several simple examples. <\/p>\n\n\n\n<p>The operator &#8216;+=&#8217; is a shorthand for the <strong>addition assignment operator<\/strong>. It adds two values and assigns the sum to a variable (left operand).<\/p>\n\n\n\n<p>Let&#8217;s look at three instances to have a better idea of how this operator works.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Adding Two Numeric Values With += Operator<\/h2>\n\n\n\n<p>In the code mentioned below, we have initialized a variable X with an initial value of 5 and then add value 15 to it and store the resultant value in the same variable X.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nX = 5\nprint(&quot;Value Before Change: &quot;, X)\nX += 15\nprint(&quot;Value After Change: &quot;, X)\n<\/pre><\/div>\n\n\n<p>The output of the Code is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nValue Before Change:  5\nValue After Change:  20\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Adding Two Strings <\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nS1 = &quot;Welcome to &quot;\nS2 = &quot;AskPython&quot;\n\nprint(&quot;First String : &quot;, S1)\nprint(&quot;Second String: &quot;, S2)\nS1+=S2\nprint(&quot;Final String: &quot;, S1)\n<\/pre><\/div>\n\n\n<p>In the code mentioned above, we initialized two variables S1 and S2 with initial values as \u201cWelcome to \u201d and \u201dAskPython\u201d respectively.<\/p>\n\n\n\n<p>We then add the two strings using the \u2018+=\u2019 operator which will concatenate the values of the string.<\/p>\n\n\n\n<p>The output of the code is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nFirst String :  Welcome to \nSecond String:  AskPython\nFinal String:  Welcome to AskPython\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Understanding Associativity of &#8220;+=&#8221; operator in Python<\/h2>\n\n\n\n<p>The associativity property of the \u2018+=\u2019 operator is from right to left.  Let&#8217;s look at the example code mentioned below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nX = 5\nY = 10\nX += Y&gt;&gt;1\nprint(X)\n<\/pre><\/div>\n\n\n<p>We initialized two variables X and Y with initial values as 5 and 10 respectively. In the code, we right shift the value of Y by 1 bit and then add the result to variable X and store the final result to X.<\/p>\n\n\n\n<p>The output comes out to be X = 10 and Y = 10.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Congratulations! You just learned about the &#8216;+=&#8217; operator in python and also learned about its various implementations.<\/p>\n\n\n\n<p>Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/in-and-not-in-operators-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">The \u201cin\u201d and \u201cnot in\u201d operators in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/python-floor-based-division\" target=\"_blank\" rel=\"noreferrer noopener\">Python \/\/ operator \u2013 Floor Based Division<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/python-not-equal-operator\" target=\"_blank\" rel=\"noreferrer noopener\">Python Not Equal operator<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/operator-overloading-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">Operator Overloading in Python<\/a><\/li><\/ol>\n\n\n\n<p>Thank you for taking your time out! Hope you learned something new!! &#x1f604;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we will look at the += operator in Python and see how it works with several simple examples. The operator &#8216;+=&#8217; is a shorthand for the addition assignment operator. It adds two values and assigns the sum to a variable (left operand). Let&#8217;s look at three instances to have a better idea [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":23108,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-23080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=23080"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/23080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/23108"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=23080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=23080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=23080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}