{"id":37891,"date":"2022-12-07T10:10:31","date_gmt":"2022-12-07T10:10:31","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37891"},"modified":"2022-12-07T10:10:32","modified_gmt":"2022-12-07T10:10:32","slug":"python-increment-by-1","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-increment-by-1","title":{"rendered":"Python increment by 1"},"content":{"rendered":"\n<p>To increment a variable by 1 in Python, you can use the augmented assignment operator <code>+=<\/code>. This operator adds the right operand to the left operand and assigns the result to the left operand. For example, if you have a variable <code>x<\/code> with the value 5, you can increment it by 1 using the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx += 1\n<\/pre><\/div>\n\n\n<p>After this code is executed, the value of <code>x<\/code> will be 6. You can also use the <code>+=<\/code> operator with other operands, such as strings, <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" data-type=\"post\" data-id=\"511\">lists<\/a>, and <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" data-type=\"post\" data-id=\"535\">dictionaries<\/a>. For example, if you have a string <code>s<\/code> with the value &#8220;hello&#8221;, you can increment it by 1 by appending the character &#8220;a&#8221; to it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ns = &quot;hello&quot;\ns += &quot;a&quot;\n<\/pre><\/div>\n\n\n<p>After this code is executed, the value of <code>s<\/code> will be &#8220;helloa&#8221;. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Incrementing by 1 with List<\/h2>\n\n\n\n<p>If you have a list <code>l<\/code> with the elements [1, 2, 3], you can increment it by 1 by appending element 4 to it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nl = &#x5B;1, 2, 3]\nl += &#x5B;4]\n<\/pre><\/div>\n\n\n<p>After this code is executed, the value of <code>l<\/code> will be [1, 2, 3, 4]. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary increment by 1<\/h2>\n\n\n\n<p>Let&#8217;s look at another example of incrementing by 1 with a dictionary.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {&quot;a&quot;: 1, &quot;b&quot;: 2}\nd += {&quot;c&quot;: 3}\nprint(d)\n<\/pre><\/div>\n\n\n<p>In this case, the <code>+=<\/code> operator merges the dictionary <code>d<\/code> with the dictionary <code>{\"c\": 3}<\/code>, resulting in the dictionary {&#8220;a&#8221;: 1, &#8220;b&#8221;: 2, &#8220;c&#8221;: 3}.<\/p>\n\n\n\n<p>Overall, to increment a variable by 1 in Python, you can use the augmented assignment operator <code>+=<\/code>. This operator adds the right operand to the left operand and assigns the result to the left operand, allowing you to increment variables of different types by 1.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To increment a variable by 1 in Python, you can use the augmented assignment operator +=. This operator adds the right operand to the left operand and assigns the result to the left operand. For example, if you have a variable x with the value 5, you can increment it by 1 using the following [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-37891","post","type-post","status-publish","format-standard","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37891"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37891\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}