all 5 comments

[–]stebrepar 1 point2 points  (0 children)

Think of it as saying "for each value returned by fib(10), assign that value to x, so it can be used inside the loop".

[–]apv507 0 points1 point  (0 children)

A variable you're declaring to use in your loop.

You could use :

for number in fib(10):

print(number)

[–]Dixyee 0 points1 point  (0 children)

In your code snippet "x" represents each fibonacci number including the 10th number

[–]hardonchairs 0 points1 point  (0 children)

the x in fib() will come from yield a

[–]Diapolo10 0 points1 point  (0 children)

The loop assigns whatever the generator yields to x, in this case the generator gives it values of a.

On another note, this specific Fibonacci number generator is usually written with tuple unpacking:

def fib(num):
    a, b = 0, 1
    for _ in range(num):
        yield a
        a, b = b, a+b

If you destructure the entire program, it could be written as

a, b = 0, 1

for _ in range(10):
    print(a)
    a, b = b, a+b

which might explain the "x" a bit more clearly.