{"id":15687,"date":"2022-02-28T15:35:05","date_gmt":"2022-02-28T15:35:05","guid":{"rendered":"https:\/\/linuxways.net\/?p=15687"},"modified":"2022-02-28T15:35:05","modified_gmt":"2022-02-28T15:35:05","slug":"how-to-use-assignment-operators-in-python","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-use-assignment-operators-in-python\/","title":{"rendered":"How to Use Assignment Operators in Python"},"content":{"rendered":"<p>This section will go through the Python programming language&#8217;s assignment operators. A little refresher on Python operators is in order before we get into the core of the topic. Operators are special symbols in a programming language that are used in between operands to perform logical and mathematical operations. The value on which the operator conducts the computation is known as the operand. Arithmetic, logical, relational, assignment, and bitwise are just a few of the operators available.<\/p>\n<h2><strong>Assignment Operators in Python with examples<\/strong><\/h2>\n<p>As the name suggest, you can assign any value to any variable using an assignment operator. This operator represents by the equality (=) sign whereas you need to write any variable on the left side which is also known as operand and on the right side you need to mention any number.<\/p>\n<p>A basic syntax of representing the assignment operator is shown below.<\/p>\n<pre>a\u00a0=\u00a0Any number<\/pre>\n<p>Here a is the variable or the operand and on the right side you can assign any constant number that will be stored in it for example,<\/p>\n<pre>a = 5<\/pre>\n<h2><strong>Different Assignment Operators in Python<\/strong><\/h2>\n<p>In this section we are going to discuss all the related assignment operators that can be used in Python programming language.<\/p>\n<h3><strong>Add and Assignment Operator (+=)<\/strong><\/h3>\n<p>Here we are not only assigning the value, but we are also going to add them as well and the result will be stored in the variable a on the left side that can be seen below<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax:<\/strong> \r\na += b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3\r\n\r\nb = 5 \r\n\r\n#a = a + b\u00a0\u00a0\r\n\r\na += b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"1048\" height=\"176\" class=\"wp-image-15688\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-727.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-727.png 1048w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-727-300x50.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-727-1024x172.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-727-768x129.png 768w\" sizes=\"auto, (max-width: 1048px) 100vw, 1048px\" \/><\/strong><\/p>\n<h3><strong>Subtract and Assignment Operator (-=)<\/strong><\/h3>\n<p>Here we are also going to subtract other than just assigning the values as shown below. The result will be stored in variable an on the left side.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax:<\/strong> \r\na -= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 5 \r\nb = 3\r\n#a = a - b\u00a0\u00a0 \r\na -= b\u00a0\u00a0\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1122\" height=\"180\" class=\"wp-image-15689\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-728.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-728.png 1122w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-728-300x48.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-728-1024x164.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-728-768x123.png 768w\" sizes=\"auto, (max-width: 1122px) 100vw, 1122px\" \/><\/p>\n<h3><strong>Multiply and Assignment Operator (*=)<\/strong><\/h3>\n<p>In this section, we are to use the multiplication operator as well as the assignment operator. The result will be stored in the variable an on the left side.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax:<\/strong> \r\na *= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 5 \r\n\r\nb = 3 \r\n\r\n#a = a * b\u00a0\u00a0 \r\n\r\na *= b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>15<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"187\" class=\"wp-image-15690\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-729.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-729.png 916w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-729-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-729-768x157.png 768w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/p>\n<h3><strong>Divide and Assignment Operator (\/=)<\/strong><\/h3>\n<p>Here we are also going to use the division operator along with the assignment operator as shown below. The result will be stored in the variable a on the left side.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax:<\/strong> \r\na \/= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3 \r\nb = 5\r\n#a = a \/ b\u00a0\u00a0 \r\na \/= b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>0.6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"904\" height=\"184\" class=\"wp-image-15691\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-730.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-730.png 904w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-730-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-730-768x156.png 768w\" sizes=\"auto, (max-width: 904px) 100vw, 904px\" \/><\/p>\n<h3><strong>Modulus and Assignment Operator (%=)<\/strong><\/h3>\n<p>Modulus operator will show you the remainder when you divide two numbers, and their result will be stored in the variable an as shown below.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax: \r\n<\/strong> a %= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3 \r\nb = 5\r\n#a = a % b\u00a0\u00a0 \r\na %= b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"183\" class=\"wp-image-15692\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-731.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-731.png 900w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-731-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-731-768x156.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/strong><\/p>\n<h3><strong>Exponent and Assignment Operator (**=)<\/strong><\/h3>\n<p>This operator is used to calculate the exponential value assigned to any number and its result will be stored on the left side in the variable a.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax<\/strong>: \r\na **= b<\/pre>\n<p><strong>Example<\/strong>:<\/p>\n<pre>a = 3 \r\nb = 5\r\n#a = a ** b\u00a0\u00a0 \r\na **= b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>243<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"1012\" height=\"181\" class=\"wp-image-15693\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-732.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-732.png 1012w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-732-300x54.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-732-768x137.png 768w\" sizes=\"auto, (max-width: 1012px) 100vw, 1012px\" \/><\/strong><\/p>\n<h3><strong>Bitwise And (&amp;) and Assignment Operator (&amp;=)<\/strong><\/h3>\n<p>This operator will first convert the numbers in their binary form and then performed the And (&amp;) operator to display the result back in decimal form.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax<\/strong>: \r\na &amp;= b<\/pre>\n<p><strong>Example<\/strong>:<\/p>\n<pre>a = 3 (0011) \r\nb = 5 (0101)\r\n#a = a(0011) &amp; b(0101)\u00a0\u00a0 \r\n\r\n\r\na &amp;= b = 0001 = 1\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"963\" height=\"186\" class=\"wp-image-15694\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-733.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-733.png 963w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-733-300x58.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-733-768x148.png 768w\" sizes=\"auto, (max-width: 963px) 100vw, 963px\" \/><\/strong><\/p>\n<h3><strong>Bitwise OR and Assignment Operator (|=)<\/strong><\/h3>\n<p>This operator will first convert the numbers in their binary form and then performed the OR (|) operator to display the result back in decimal form.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax:<\/strong> \r\na |= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3 (0011) \r\nb = 5 (0101)\r\n#a = a (0011) | b (0101)\u00a0 \r\na |= b = 0111 = 7\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"856\" height=\"178\" class=\"wp-image-15695\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-734.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-734.png 856w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-734-300x62.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-734-768x160.png 768w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/><\/strong><\/p>\n<h3><strong>Bitwise XOR and Assignment Operator (^=)<\/strong><\/h3>\n<p>This operator is used to perform Bitwise XOR on the operands and then assigning result to the left operand.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<h3><strong>Syntax:<br \/>\n<\/strong> a ^= b<\/h3>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3 (0011) \r\nb = 5 (0101)\r\n#a = a ^ b (0011) ^ (0101)\u00a0\u00a0 \r\na ^= b\r\n\r\nprint(a) = (0110)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"912\" height=\"183\" class=\"wp-image-15696\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-735.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-735.png 912w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-735-300x60.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-735-768x154.png 768w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/p>\n<h3><strong>Bitwise Right Shift and Assign Operator (&gt;&gt;=)<\/strong><\/h3>\n<p>This operator is used to perform Bitwise right shift on the assigned values given to the variables.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax:<\/strong> \r\na &gt;&gt;= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3 \r\nb = 5\r\n#a = a &gt;&gt; b\u00a0\u00a0 \r\na &gt;&gt;= b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"907\" height=\"184\" class=\"wp-image-15697\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-736.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-736.png 907w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-736-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-736-768x156.png 768w\" sizes=\"auto, (max-width: 907px) 100vw, 907px\" \/><\/p>\n<h3><strong>Bitwise Left Shift and Assign operator (&lt;&lt;=)<\/strong><\/h3>\n<p>This operator is used to perform Bitwise left shift on the assigned values given to the variables.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>Syntax: \r\n<\/strong> a &lt;&lt;= b<\/pre>\n<p><strong>Example: <\/strong><\/p>\n<pre>a = 3 \r\nb = 5\r\n#a = a &lt;&lt; b\u00a0\u00a0 \r\na &lt;&lt;= b\r\n\r\nprint(a)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>96<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"925\" height=\"184\" class=\"wp-image-15698\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-737.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-737.png 925w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-737-300x60.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-737-768x153.png 768w\" sizes=\"auto, (max-width: 925px) 100vw, 925px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>In this article, we have discussed some of the most useful assignment operators in Python. Their explanation and syntax have been discussed in detail in this article for better understanding.<\/p>","protected":false},"excerpt":{"rendered":"<p>This section will go through the Python programming language&#8217;s assignment operators. A little refresher on Python operators is in order before we get into the core of the&hellip;<\/p>","protected":false},"author":1,"featured_media":15699,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[917,10],"class_list":["post-15687","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-assignment-operators","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=15687"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15687\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/15699"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=15687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=15687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=15687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}