{"id":25625,"date":"2021-11-23T08:57:12","date_gmt":"2021-11-23T03:27:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=25625"},"modified":"2021-11-23T08:57:12","modified_gmt":"2021-11-23T03:27:12","slug":"a-complete-tutorial-for-pythons-operator","status":"publish","type":"post","link":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/","title":{"rendered":"A Complete Tutorial for Python’s += Operator"},"content":{"rendered":"

In this session, we’ll look at Python’s += operator. Let us see how it works with a few simple examples.<\/p>\n

The addition assignment operator is denoted by the operator ‘+=’. It is like a short form to it. It adds two values and stores the result in a variable (left operand).<\/p>\n

Addition of Two numbers Using += Operator:<\/h4>\n

Example1:<\/strong><\/p>\n

gvn_num = 100\r\nprint(\"The given number = \", gvn_num)\r\ngvn_num += 50\r\nprint(\"The given number after addition with 50 = \", gvn_num)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The given number =  100\r\nThe given number after addition with 50 =  150<\/pre>\n

Example2:<\/strong><\/p>\n

a = 10\r\nb = 20\r\nc = 30\r\nrslt_sum = 0\r\n# Adding a,b,c and storing it in a variable rslt_sum\r\n# The below indicates rslt_sum = rslt_sum+(a+b+c)\r\nrslt_sum += a+b+c\r\nprint(\"The sum of a,b,c = \", rslt_sum)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The sum of a,b,c =  60<\/pre>\n

For Strings<\/h4>\n

The ‘+=’ operator concatenates the given strings. It is used for the concatenation of two or more strings.<\/p>\n

Example1:<\/strong><\/p>\n

gvn_fststr = \"hello\"\r\ngvn_scndstr = \"btechgeeks\"\r\nprint(\"The given first string = \", gvn_fststr)\r\nprint(\"The given second string = \", gvn_scndstr)\r\nprint()\r\n# It concatenates the given first string with the second string and assigns the\r\n# result to the first string\r\ngvn_fststr += gvn_scndstr\r\n\r\nprint(\"The given first string after concatenation = \", gvn_fststr)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The given first string =  hello\r\nThe given second string =  btechgeeks\r\n\r\nThe given first string after concatenation =  hellobtechgeeks<\/pre>\n

Example2:<\/strong><\/p>\n

gvn_fststr = \"hello\"\r\ngvn_scndstr = \" this is \"\r\ngvn_thrdstr = \"btechgeeks\"\r\nconcat_str = \"\"\r\n# concatenating the given three strings using  '+=' Operator\r\nconcat_str += (gvn_fststr+gvn_scndstr+gvn_thrdstr)\r\nprint(\"The concatenation of given three strings = \", concat_str)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The concatenation of given three strings =  hello this is btechgeeks<\/pre>\n

The “+=” operator’s Associativity in Python<\/h4>\n

The ‘+=’ operator’s associativity property is from right to left.<\/p>\n

Example1:<\/strong><\/p>\n

a = 3\r\nb = 6\r\na += b >> 2\r\nprint(\"a =\", a)\r\nprint(\"b =\", b)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

a = 4\r\nb = 6<\/pre>\n

Explanation:<\/strong><\/p>\n

We set the starting values of two variables, a<\/strong> and b<\/strong>, to 3 and 6, respectively. In the code, we right shift the value of b<\/strong> by two bits, add the result to variable ‘a’<\/strong>, and put the final result in variable ‘a’<\/strong>.<\/p>\n

Therefore the final output is :<\/p>\n

a=4 and b=6<\/p>\n

Example2:<\/strong><\/p>\n

a = 3\r\nb = 6\r\n# Left shift by 1 bit\r\na += b << 1\r\nprint(\"a =\", a)\r\nprint(\"b =\", b)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

a = 15\r\nb = 6<\/pre>\n

Conclusion<\/strong><\/h4>\n

This is about the Python ‘+=’ operator and its numerous implementations.<\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

In this session, we’ll look at Python’s += operator. Let us see how it works with a few simple examples. The addition assignment operator is denoted by the operator ‘+=’. It is like a short form to it. It adds two values and stores the result in a variable (left operand). Addition of Two numbers …<\/p>\n

A Complete Tutorial for Python’s += Operator<\/span> Read More »<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nA Complete Tutorial for Python's += Operator - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Complete Tutorial for Python's += Operator - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this session, we’ll look at Python’s += operator. Let us see how it works with a few simple examples. The addition assignment operator is denoted by the operator ‘+=’. It is like a short form to it. It adds two values and stores the result in a variable (left operand). Addition of Two numbers … A Complete Tutorial for Python’s += Operator Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-23T03:27:12+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vikram Chiluka\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#webpage\",\"url\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/\",\"name\":\"A Complete Tutorial for Python's += Operator - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-11-23T03:27:12+00:00\",\"dateModified\":\"2021-11-23T03:27:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Complete Tutorial for Python’s += Operator\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\"},\"headline\":\"A Complete Tutorial for Python’s += Operator\",\"datePublished\":\"2021-11-23T03:27:12+00:00\",\"dateModified\":\"2021-11-23T03:27:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#webpage\"},\"wordCount\":176,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\",\"name\":\"Vikram Chiluka\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"caption\":\"Vikram Chiluka\"},\"url\":\"https:\/\/python-programs.com\/author\/vikram\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Complete Tutorial for Python's += Operator - Python Programs","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:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/","og_locale":"en_US","og_type":"article","og_title":"A Complete Tutorial for Python's += Operator - Python Programs","og_description":"In this session, we’ll look at Python’s += operator. Let us see how it works with a few simple examples. The addition assignment operator is denoted by the operator ‘+=’. It is like a short form to it. It adds two values and stores the result in a variable (left operand). Addition of Two numbers … A Complete Tutorial for Python’s += Operator Read More »","og_url":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-11-23T03:27:12+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Vikram Chiluka","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#webpage","url":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/","name":"A Complete Tutorial for Python's += Operator - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-11-23T03:27:12+00:00","dateModified":"2021-11-23T03:27:12+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"A Complete Tutorial for Python’s += Operator"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602"},"headline":"A Complete Tutorial for Python’s += Operator","datePublished":"2021-11-23T03:27:12+00:00","dateModified":"2021-11-23T03:27:12+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#webpage"},"wordCount":176,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/a-complete-tutorial-for-pythons-operator\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602","name":"Vikram Chiluka","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","caption":"Vikram Chiluka"},"url":"https:\/\/python-programs.com\/author\/vikram\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/25625"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=25625"}],"version-history":[{"count":5,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/25625\/revisions"}],"predecessor-version":[{"id":25630,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/25625\/revisions\/25630"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=25625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=25625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=25625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}