{"id":3556,"date":"2026-11-11T12:00:06","date_gmt":"2026-11-11T12:00:06","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3556"},"modified":"2026-11-11T12:00:06","modified_gmt":"2026-11-11T12:00:06","slug":"python-assignment-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-assignment-operators\/","title":{"rendered":"Python Assignment Operators with Examples"},"content":{"rendered":"<p>Assignment operators store a value in a variable. The basic one is <code>=<\/code>. The others, called <strong>augmented assignment operators<\/strong>, update a variable using its current value: <code>score += 5<\/code> is a short way of writing <code>score = score + 5<\/code>. You will use them constantly in loops, counters and running totals.<\/p>\n<h2>Python Assignment Operators Table<\/h2>\n<table>\n<thead>\n<tr>\n<th>Operator<\/th>\n<th>Example<\/th>\n<th>Same as<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>=<\/code><\/td>\n<td><code>x = 10<\/code><\/td>\n<td>store 10 in x<\/td>\n<\/tr>\n<tr>\n<td><code>+=<\/code><\/td>\n<td><code>x += 5<\/code><\/td>\n<td><code>x = x + 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-=<\/code><\/td>\n<td><code>x -= 3<\/code><\/td>\n<td><code>x = x - 3<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>*=<\/code><\/td>\n<td><code>x *= 2<\/code><\/td>\n<td><code>x = x * 2<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\/=<\/code><\/td>\n<td><code>x \/= 4<\/code><\/td>\n<td><code>x = x \/ 4<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\/\/=<\/code><\/td>\n<td><code>x \/\/= 5<\/code><\/td>\n<td><code>x = x \/\/ 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>%=<\/code><\/td>\n<td><code>x %= 5<\/code><\/td>\n<td><code>x = x % 5<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>**=<\/code><\/td>\n<td><code>x **= 4<\/code><\/td>\n<td><code>x = x ** 4<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&amp;=<\/code>, <code>|=<\/code>, <code>^=<\/code><\/td>\n<td><code>x &amp;= 10<\/code><\/td>\n<td><code>x = x &amp; 10<\/code> (bitwise)<\/td>\n<\/tr>\n<tr>\n<td><code>&lt;&lt;=<\/code>, <code>&gt;&gt;=<\/code><\/td>\n<td><code>x &lt;&lt;= 2<\/code><\/td>\n<td><code>x = x &lt;&lt; 2<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>:=<\/code><\/td>\n<td><code>(n := len(data))<\/code><\/td>\n<td>assign inside an expression (&#8220;walrus&#8221;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Arithmetic Assignment Operators<\/h2>\n<pre class=\"wp-block-code\"><code>x = 10\nx += 5\nprint(x)\nx -= 3\nprint(x)\nx *= 2\nprint(x)\nx \/= 4\nprint(x)\n\nx = 17\nx \/\/= 5\nprint(x)\nx = 17\nx %= 5\nprint(x)\nx = 3\nx **= 4\nprint(x)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>15\n12\n24\n6.0\n3\n2\n81<\/code><\/pre>\n<p>Notice that <code>x \/= 4<\/code> turns x into a float (<code>6.0<\/code>), because <code>\/<\/code> always gives a float. Use <code>\/\/=<\/code> if you want to stay with whole numbers.<\/p>\n<h2>Bitwise Assignment Operators<\/h2>\n<pre class=\"wp-block-code\"><code>f = 12\nf &amp;= 10\nprint(f)\nf |= 1\nprint(f)\nf ^= 3\nprint(f)\nf &lt;&lt;= 2\nprint(f)\nf &gt;&gt;= 1\nprint(f)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>8\n9\n10\n40\n20<\/code><\/pre>\n<p>These follow the same rules as the <a href=\"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/\">bitwise operators<\/a>; they just store the result back in the variable.<\/p>\n<h2>Assigning Many Variables at Once<\/h2>\n<pre class=\"wp-block-code\"><code>a, b, c = 1, 2, 3\nprint(a, b, c)\n\np = q = r = 0\nprint(p, q, r)\n\na, b = b, a        # swap without a temporary variable\nprint(a, b)\n\nfirst, *rest = [10, 20, 30, 40]\nprint(first, rest)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>1 2 3\n0 0 0\n2 1\n10 [20, 30, 40]<\/code><\/pre>\n<p>The starred name <code>*rest<\/code> collects all the remaining items into a list. This is called <em>unpacking<\/em>.<\/p>\n<h2>Assignment Operators with Strings and Lists<\/h2>\n<pre class=\"wp-block-code\"><code>greeting = \"Hello\"\ngreeting += \", Riya\"\nprint(greeting)\n\nline = \"-\" * 3\nline *= 2\nprint(line)\n\nnums = [1, 2]\nnums += [3]\nprint(nums)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Hello, Riya\n------\n[1, 2, 3]<\/code><\/pre>\n<h2>The Walrus Operator :=<\/h2>\n<p>Added in Python 3.8, <code>:=<\/code> lets you assign a value inside an expression, so you don&#8217;t have to compute it twice:<\/p>\n<pre class=\"wp-block-code\"><code>data = [4, 8, 15, 16, 23, 42]\nif (n := len(data)) &gt; 5:\n    print(f\"List is long: {n} items\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>List is long: 6 items<\/code><\/pre>\n<h2>A Trap: += on Lists Changes the Shared List<\/h2>\n<p>For a mutable object like a list, <code>+=<\/code> changes the list in place. If two names point to the same list, both see the change. Tuples are immutable, so <code>+=<\/code> creates a new tuple instead:<\/p>\n<pre class=\"wp-block-code\"><code>list_a = [1, 2]\nlist_b = list_a\nlist_b += [3]\nprint(list_a)\n\nt = (1, 2)\nt2 = t\nt2 += (3,)\nprint(t, t2)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[1, 2, 3]\n(1, 2) (1, 2, 3)<\/code><\/pre>\n<p>If you want a separate list, make a copy first: <code>list_b = list_a.copy()<\/code>.<\/p>\n<h2>Common Mistakes<\/h2>\n<ul>\n<li><strong>Writing <code>x =+ 5<\/code> instead of <code>x += 5<\/code>.<\/strong> <code>x =+ 5<\/code> is valid Python, but it means <code>x = +5<\/code>, so x simply becomes 5.<\/li>\n<li><strong>Using <code>x++<\/code>.<\/strong> Python has no <code>++<\/code> or <code>--<\/code> operator. Use <code>x += 1<\/code>.<\/li>\n<li><strong>Using a variable before giving it a value.<\/strong> <code>total += marks<\/code> fails with a NameError if <code>total<\/code> was never set. Start with <code>total = 0<\/code>.<\/li>\n<\/ul>\n<h2>Practice Questions<\/h2>\n<ol>\n<li>What does this print? <code>k = 7; k += k; print(k)<\/code><\/li>\n<li>What does this print? <code>k = 20; k \/\/= 3; k **= 2; print(k)<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>14<\/code>. 2) <code>36<\/code>, because 20 \/\/ 3 is 6 and 6 ** 2 is 36.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What is the difference between = and == in Python?<\/h3>\n<p><code>=<\/code> stores a value in a variable. <code>==<\/code> compares two values and returns True or False.<\/p>\n<h3>Is x += 1 the same as x = x + 1?<\/h3>\n<p>For numbers and strings, yes. For lists, <code>+=<\/code> changes the existing list in place, while <code>x = x + [1]<\/code> creates a new list.<\/p>\n<h3>What is the walrus operator in Python?<\/h3>\n<p><code>:=<\/code>, which assigns a value inside an expression, such as an <code>if<\/code> or <code>while<\/code> condition. It needs Python 3.8 or newer.<\/p>\n<p>Next: <a href=\"https:\/\/gyanipandit.com\/programming\/python-logical-operators\/\">Python logical operators<\/a> \u00b7 Previous: <a href=\"https:\/\/gyanipandit.com\/programming\/python-arithmetic-operators\/\">Python arithmetic operators<\/a> \u00b7 All operators: <a href=\"https:\/\/gyanipandit.com\/programming\/python-operators\/\">Python operators overview<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assignment operators store a value in a variable. The basic one is =. The others, called augmented assignment operators, update a variable using its current value: score += 5 is a short way of writing score = score + 5. You will use them constantly in loops, counters and running totals. Python Assignment Operators Table &#8230; <a title=\"Python Assignment Operators with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-assignment-operators\/\" aria-label=\"Read more about Python Assignment Operators with Examples\">Read more<\/a><\/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":["post-3556","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","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":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3556\/revisions"}],"predecessor-version":[{"id":6131,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3556\/revisions\/6131"}],"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}]}}