{"id":3556,"date":"2022-10-26T10:42:20","date_gmt":"2022-10-26T10:42:20","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3556"},"modified":"2022-10-26T10:42:22","modified_gmt":"2022-10-26T10:42:22","slug":"python-assignment-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-assignment-operators\/","title":{"rendered":"Python Assignment Operators"},"content":{"rendered":"\n<p class=\"has-text-align-center\">Python Assignment Operators<\/p>\n\n\n\n<p>Now, we are going to learn about the assignment operators in Python. Basically, these operators are used for assignment purposes. We are well familiar with one of the assignment operators, which is the simple assignment operator (=). We are going to use the assignment operators to assign values to the variables.<\/p>\n\n\n\n<p>Here is the list of some assignment operators, many of which, we are going to explore soon.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td>Operator<\/td><td>Description<\/td><td>Example<\/td><\/tr><\/thead><tbody><tr><td>=<\/td><td>Simply assign the value<\/td><td>x = 3 (x is assigned value 3)<\/td><\/tr><tr><td>+=<\/td><td>Add and Assign<\/td><td>x+=3 (add 3 to value of x and then assign the new value to x)\n<p>&nbsp;<\/p>\n<p>so, this is same as x = x+3<\/p>\n<p>if x = 3, then new value of x is 6 with this operation<\/p>\n<\/td><\/tr><tr><td>&#8211; =<\/td><td>Subtract and Assign<\/td><td>x-=3 (subtract 3 from value of x and assign the new value to x)\n<p>&nbsp;<\/p>\n<p>so, this is same as x = x-3<\/p>\n<p>if x =3, then new value of x is 0<\/p>\n<\/td><\/tr><tr><td>*=<\/td><td>Multiply and Assign<\/td><td>x*=3 (multiply 3 to value of x and assign the new value to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x*3<\/p>\n<p>if x = 3, then new value of x is 9<\/p>\n<\/td><\/tr><tr><td>\/=<\/td><td>Divide and Assign<\/td><td>x\/=5 (Divide x by 5 and assign the new value to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x \/ 5.<\/p>\n<p>if x = 3 then new value of x is 0.6<\/p>\n<\/td><\/tr><tr><td>\/\/=<\/td><td>Divide and assign (floor value)<\/td><td>x\/\/=5 (divide x by 5 and assign the floor value to the x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x \/\/ 5.<\/p>\n<p>if x = 3 then new value of x is 0<\/p>\n<\/td><\/tr><tr><td>**=<\/td><td>Exponentiate and assign<\/td><td>x**=2 (compute x to the power 2 and assign it to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x**2<\/p>\n<p>if x =3 then new value of x is 9<\/p>\n<\/td><\/tr><tr><td>%=<\/td><td>Perform division and assign the remainder of division to the variable.<\/td><td>x%=2 (divide x by 2 and assign remainder to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x%2<\/p>\n<p>if x = 3 then new value of x is 1(this is the remainder when 3 is divided by 2)<\/p>\n<\/td><\/tr><tr><td>&amp;=<\/td><td>Perform AND operation and assign to the variable<\/td><td>x&amp;=2(perform AND operation between value of x and 2, and assign the result to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x&amp;2<\/p>\n<p>if x = 3 then new value of x is 2.<\/p>\n<p>(To understand this, you have to convert 3 and 2 in binary format and perform AND operation on them)<\/p>\n<\/td><\/tr><tr><td>|=<\/td><td>Perform OR operation and assign to the variable.<\/td><td>x|=2 (Perform OR operation between value of x and 2 and assign the result to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x|2<\/p>\n<p>if x =3 then new value of x is 3.<\/p>\n<p>(To understand this, you need to convert 3 and 2 into binary format and perform OR operation on them)<\/p>\n<\/td><\/tr><tr><td>^=<\/td><td>Perform XOR operation and assign to the variable.<\/td><td>x^=2 (Perform XOR operation between value of x and 2 and assign the result to x)\n<p>&nbsp;<\/p>\n<p>this is same as x = x^2<\/p>\n<p>if x = 3 then new value of x is 1<\/p>\n<p>(To understand this, you need to convert 3 and 2 into binary format and perform XOR operation on them)<\/p>\n<\/td><\/tr><tr><td>&gt;&gt;=<\/td><td>&nbsp;Right shift and assign<\/td><td>x &gt;&gt; = 3 (perform right shift on x by 3 bit\u2019s and assign the new value to x)\n<p>&nbsp;<\/p>\n<p>the right shift is again the operation on bit\u2019s (binary digit\u2019s).<\/p>\n<p>If x = 8 (1000), then the operation x &gt;&gt;=1 gives result<\/p>\n<p>4(100)<\/p>\n<\/td><\/tr><tr><td>&lt;&lt;=<\/td><td>Left shift and assign<\/td><td>x&lt;&lt;=2 (perform left shift on x by 2 bit\u2019s and assign the new value to x)\n<p>&nbsp;<\/p>\n<p>the left shift is again the operation on bit\u2019s (binary digit\u2019s).<\/p>\n<p>If x = 8 (1000), then the operation x &lt;&lt;=1 gives result &#8211;<\/p>\n<p>16(10000)<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So, as you can see here, we have simply given some of the assignment operators in the above table, with symbol, and some examples. Now, let\u2019s consider a simple example, which demonstrates the same things.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"197\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-OPERATORS.jpg\" alt=\"\" class=\"wp-image-3557\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-OPERATORS.jpg 630w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ASSIGNMENT-OPERATORS-300x94.jpg 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see in the above program, we are simply assigning some value to the variable, which is number, and then we are using some of the different assignment operators, like +=, -=, *= and many others, and we are printing the respective output every time we are performing some operation.<\/p>\n\n\n\n<p>So, now let\u2019s try to have a look at the output &#8211;<\/p>\n\n\n\n<p>29<br>39<br>29<br>290<br>29<br>9<\/p>\n\n\n\n<p>As you can see, first we had assigned the value 29 to the variable number, and then we have used some different assignment operators, and we have got the respective outputs. We can use the assignment operators, as and when required. Now, let\u2019s move on to the next type of operators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Assignment Operators Now, we are going to learn about the assignment operators in Python. Basically, these operators are used for assignment purposes. We are well familiar with one of the assignment operators, which is the simple assignment operator (=). We are going to use the assignment operators to assign values to the variables. Here [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3556","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3556"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3556\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3556\/revisions\/3567"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}