{"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
Example1:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Output:<\/strong><\/p>\n The ‘+=’ operator concatenates the given strings. It is used for the concatenation of two or more strings.<\/p>\n Example1:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Output:<\/strong><\/p>\n The ‘+=’ operator’s associativity property is from right to left.<\/p>\n Example1:<\/strong><\/p>\n Output:<\/strong><\/p>\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 Output:<\/strong><\/p>\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>\ngvn_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
The given number = 100\r\nThe given number after addition with 50 = 150<\/pre>\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
The sum of a,b,c = 60<\/pre>\n
For Strings<\/h4>\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
The given first string = hello\r\nThe given second string = btechgeeks\r\n\r\nThe given first string after concatenation = hellobtechgeeks<\/pre>\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
The concatenation of given three strings = hello this is btechgeeks<\/pre>\n
The “+=” operator’s Associativity in Python<\/h4>\n
a = 3\r\nb = 6\r\na += b >> 2\r\nprint(\"a =\", a)\r\nprint(\"b =\", b)\r\n<\/pre>\n
a = 4\r\nb = 6<\/pre>\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
a = 15\r\nb = 6<\/pre>\n
Conclusion<\/strong><\/h4>\n