× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Python a, b = b, a +b

Geoffrey Spear
Feb 02, 2015
<p>Let's say we start with <code>a</code> and <code>b</code> like this:</p> <pre><code>a = 2 b = 3 </code></pre> <p>So, when you do:</p> <pre><code>a, b = b, a + b </code></pre> <p>what happens is you create the tuple <code>(b, a + b)</code> or <code>(3, 5)</code> and then unpack it into <code>a</code> and <code>b</code> so <code>a</code> becomes <code>3</code> and <code>b</code> becomes <code>5</code>.</p> <p>In your second example:</p> <pre><code>a = b # a is now 3 b = a + b # b is 3 + 3, or 6. </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/21990883/Python%20a,%20b%20=%20b,%20a%20+b/21991001">Stack Overflow</a>.</p>
comments powered by Disqus