I am having diffculty to understand this program
<p>i just came across this program and it seems to me very difficult to understand.
which is calculating the Fibonacci boundary for a positive number, returns a 2-tuple. The first element is the Largest Fibonacci Number smaller than x and the second component is the Smallest Fibonacci Number larger than x. The return value is immediately stored via unpacking into the variables lub and sup:</p>

<pre><code>def fib_intervall(x):
    """ returns the largest fibonacci
    number smaller than x and the lowest
    fibonacci number higher than x"""
    if x &lt; 0:
        return -1
    (old,new, lub) = (0,1,0)
    while True:
        if new &lt; x:
            lub = new 
            (old,new) = (new,old+new)
        else:
            return (lub, new)

while True:
    x = int(input("Your number: "))
    if x &lt;= 0:
        break
    (lub, sup) = fib_intervall(x)
    print("Largest Fibonacci Number smaller than x: " + str(lub))
    print("Smallest Fibonacci Number larger than x: " + str(sup))
</code></pre>

<p>I am trying to understand but i am lacking somewhere to understand it properly some big doubts:</p>

<p>why its returning -1 ?
what  this line  <code>(old,new, lub) = (0,1,0)</code> doing ?? 
what this piece of code intend to do ? </p>

<pre><code> if new &lt; x:
            lub = new 
            (old,new) = (new,old+new)
        else:
            return (lub, new)
</code></pre>

<p>How this assignment works in python ??   <code>(lub, sup) = fib_intervall(x)</code></p>

<p>overall if you explain whole program with deep explanation that would be so helpful.</p>

<p>Thanks in advance.</p>

<blockquote>
  <p>Why is it returning -1</p>
</blockquote>

<p>It validates the input. There is no sensible result if <code>x</code> is less than zero so in that case it returns -1 which will cause the calling code to throw an exception when it fails to unpack the result. This is poor code, it would be much more normal in Python to throw a meaningful exception directly such as <code>ValueError</code> if the arguments are invalid.</p>

<pre><code>(old,new, lub) = (0,1,0)
</code></pre>

<p>would more usually be written as:</p>

<pre><code>old, new, lub = 0, 1, 0
</code></pre>

<p>as the parentheses are redundant here. It simply assigns to all three variables in parallel so in this case it is a shorthand for:</p>

<pre><code>old = 0
new = 1
lub = 0
</code></pre>

<p>But in this case:</p>

<pre><code>(old,new) = (new,old+new)
</code></pre>

<p>this would be equivalent to:</p>

<pre><code>tmp = old + new
old = new
new = tmp
</code></pre>

<p>The shorthand works out the values before it does the assignment so if you are updating existing variables with an expression that depends on their current value it allows you to avoid using a temporary variable to store the intermediate result. Again you can omit the parentheses:</p>

<pre><code>old, new = new, old+new
</code></pre>

<p>Internally these compound assignments work by creating a tuple and then unpacking it into the values named on the left hand side. You must have the same number of elements on both sides.</p>

<pre><code>(lub, sup) = fib_intervall(x)
</code></pre>

<p>This is just another unpacking assignment. The only difference here is that instead of specifying the tuple on the right hand side as a literal value it is returned from a function call.</p>

<p>There is nothing special here about it being a tuple, you can use an unpacking assignment on any sequence so long as the sequence contains the correct number of elements. If you have a varying number of elements then in Python 3 you can use the star operator to pick up any extras in a list. So here using a string for the sequence:</p>

<pre><code>&gt;&gt;&gt; a, b, *c = "hello"
&gt;&gt;&gt; print(a, b, c)
h e ['l', 'l', 'o']
&gt;&gt;&gt; a, *b, c = "hello"
&gt;&gt;&gt; print(a, b, c)
h ['e', 'l', 'l'] o
</code></pre>

python python-2.7 python-3.x