This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Closed 3 years ago.
Seems the same code but i get the different results
# code1
a = 0
b = 1
for i in range(0, 10):
print(a)
a = b
b = a + b
# code2
a, b = 0, 1
for i in range(0, 10):
print(a)
a,b = b, a + b
a = 0
b = 1
for i in range(0, 10):
print(a)
a = b
b = a + b
print()
a, b = 0, 1
for i in range(0, 10):
print(a)
a,b = b, a + b
I expected the same output
When you use the code 1 : first, a takes the value of b and then b become a+b, but with the new value of a.
In code 2, the evaluation of a and b is "simultaneous", as you use unpacking. In the same time, a takeb and b takes the value of a+b, but a still have his initial value.
Hope that I'm clear !
Related
This question already has an answer here:
How does swapping of members in tuples (a,b)=(b,a) work internally?
(1 answer)
Closed 3 years ago.
a, b, n = 0, 1, 500
while a < n:
print(a)
a, b = b, a+b
and
a, b, n = 0, 1, 500
while a < n:
print(a)
a = b
b = a+b
Both give different output.
What is the difference between a, b = b, a+b and a = b; b = a+b?
The below code is like swapping.
a, b = b, a+b
Its like doing this
temp = a
a = b
b = temp + b
a, b, n = 0, 1, 500
while a < n:
print(a)
a = b
b = a+b
In above line of code - after print(a), code value in b variable will be assigned to a first and hence the value of a is updated and the updated value of a is being used in b = a+b
Lets say, a =0, b =1 . So after print(a), value of a will be 1 first and b will have 1+1 =2.
Whereas,
a, b, n = 0, 1, 500
while a < n:
print(a)
a, b = b, a+b
In the above code, after print(a), a and b values are simultaneously assigned. In this case, whatever value of a is printed in print(a) will be used in assigning value for a in further step.
Lets say, a = 0 and b = 1 ,
after print(a) which will print 0 first, value in a will be 1 and value in b will be 0+1 = 1 because b = a+b will use a =0 and not a =1.
a, b = b, a+b
is equivalent to
tmp = a
a = b
b = tmp+b
This is not comma separated values. You are doing tuple unpacking.
a, b, n = 0, 1, 500
Is the same as:
a, b, n = (0, 1, 500)
The reason they are different is the first line assigns b to a then adds a and b together. It’s essentially the same as:
a = b
b = a+b
This question already has an answer here:
how generators work in python
(1 answer)
Closed 3 years ago.
Can someone explain how the increment of a or value of a occurs within the for loop to generate the Fib sequence?
I have an understanding of (a, b = b, a + b). However, I am unable to figure how the increment occurs in the for loop when next() is called.
def fib(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
x = fib(4)
print(x.__next__())
print(x.__next__())
print(x.__next__())
print(x.__next__())
0
1
1
2
To begin with, you can go to the next element of the generator by next(x).
Just using a print statement in your code will help you understand as well.
def fib(n):
a, b = 0, 1
for _ in range(n):
print(a, b)
yield a
a, b = b, a + b
x = fib(4)
print(next(x))
print(next(x))
print(next(x))
print(next(x))
0 1
0
1 1
1
1 2
1
2 3
2
Here the next function lazily evaluates and prints out the value of a, until you call next again.
So in the first next, it prints out 0.
Then when you call next again, a = 1 and b = 1, and you get a = 1.
Then when you call next again, a = 1 and b = 2, and you get a = 1.
Then when you call next again, a = 2 and b = 3, and you get a = 2.
After that, since you are done with your for loop, you cannot call next anymore
I am not sure what would be an appropriate heading for this question and this can be a repeated question as well. So please guide accordingly.
I am new to python programming. I have this simple code to generate Fibonacci series.
1: def fibo(n):
2: a = 0
3: b = 1
4: for x in range(n):
5: print (a, end=' ')
6: #a, b = b, a+b
7: a = b
8: b = a+b
9: print()
10: num = int(input("enter n value: "))
11: print(fibo(num))
If I execute the above code as-is the result I get is as follows
enter n value: 10
0 1 2 4 8 16 32 64 128 256
If uncomment #6 and comment lines #7 and #8 the result I get is the actual fibo series.
enter n value: 10
0 1 1 2 3 5 8 13 21 34
I would like to know what is the difference between
a, b = b, a + b
and
a = b
b = a + b
Programming IDE used: PyCharm Community 2017.3
a = b
b = a + b
is actually:
a = b
b = b + b
what you want is:
a = b
b = old_value_of_a + b
When you do
a, b = b, a + b
it really is doing:
tmp_a = b
tmp_b = a + b
a = tmp_a
b = tmp_b
which is what you want
In line 7, you've already assigned the value in b to a, so in line 8, new value for b is actually double the old b's value.
While in line 6, the values on the right side of = will be using the old values, that's why you could get Fibo series.
Assignment Statements assigns reference of source variable to target variable. Let walk through an example to understand more
>>> a = 5
>>> b = 6
>>> a = b
In this example b is source variable and a is the target variable. Now memory address for both of these variables are same. We can confirm this as well
>>> hex(id(a)), hex(id(b))
>>> ('0x1002739e0', '0x1002739e0')
Another test to confirm this is to use is operator
>>> a is b
>>> True
Now coming back to your example. First statement
>>> a, b = b, a + b
Assignes b to a and (a+b) to b. This happens as a single operation so both variables are different. We can apply above tests to confirm this
>>> a is b
>>> False
>>> hex(id(a)), hex(id(b))
>>> ('0x1002739e0', '0x2008739t0')
The second statement
>>> a = b
>>> b = a + b
Assignes b to a and then (a+b) to b. These are two different statements, so at first step a and b are already identical. Thus the second statement is equivalent to b = b + b.
Thought make it simple so anyone can understand it
if you use this kind of syntax
a = 10
b = 20
a = b
b = a+b
print (a)
print (b)
after initially assigning a = 10 it will be assigning a = 20 since python is dynamically typed language it will change the value of variable a from 10 to 20
so the result will be like
a=20
b=40
but if we use
a = 10
b = 20
a,b = b,a+b
print (a)
print (b)
this will be assigning the values in a single line so the values of a and b will be exactly used from what it is initialised above it and the result will be like
which is the correct solution
a=20
b=30
I think the # line is pythonic solution. But if you got confused,you can you use a variable which is temporary. you can assign the value temp before, then you can change the values
I have started learning python and my first program on fibonacci started giving me some weird answer, I know I am missing conceptually something so need guide from some expert on this. My program looks like this
#! usr/bin/python
a,b = 0, 1
while (b < 50):
print(b)
a = b
b = a + b
output
1
2
4
8
16
32
But When i wrote like this I got correct result
#! usr/bin/python
a,b = 0, 1
while (b < 50):
print(b)
a,b = b, a + b
output:
1
1
2
3
5
8
13
21
34
Guide me pls
a,b = 0,1
a = b # a <- 1
b = a + b # b <- a + b (1 + 1 = 2)
That's two separate operations where the a in the final line has already been modified before use.
On the other hand:
a,b = b, a + b
is an atomic operation where everything on the right side of = is the original value.
Hence it's equivalent to:
a,b = 0,1
t = a # t <- 0
a = b # a <- 1
b = t + b # b <- t + b (0 + 1 = 1)
This question already has answers here:
Multiple assignment and evaluation order in Python
(11 answers)
Closed 6 years ago.
I am new to python. Can someone explain why the following two python example does not output the same thing?
Example 1
a, b = 0, 1
while b < 50:
print(b)
a, b = b, a + b
Example 2
a, b = 0, 1
while b < 50:
print(b)
a = b
b = a + b
Thanks
In the first version, when you write
a, b = b, a + b
the expression
b, a + b
is evaluated first. After it has been evaluated, it is subsequently assigned to a, b with tuple unpacking. The key point is that the entire right hand side of an assignment statement is evaluated in its entirety before performing the binding of the left-hand side names.
In the second version,
a = b
# a is now re-bound and potentially has changed value
is performed first, and then
b = a + b
# the expression a + b uses the modified value of a
happens after. By which point, a has been re-bound.
So, to illustrate with some values. The first time round the loop, after a, b = 0, 1 we have
# a == 0
# b == 1
a, b = b, a + b
Now, b, a + b is 1, 1. And so we have
a, b = 1, 1
But the alternative goes like this:
a = b
# same as a = 1
b = a + b
# same as b = 1 + 1
You ask in a comment:
How would Example 1 be rewritten into 2 statements?
The safest way is to introduce temporary variables. Like this:
a1 = b
b1 = a + b
# now we can assign to a and b
a = a1
b = b1
In this case you don't actually need two temporary variables, one will suffice, but this is the most general form.
This tuple unpacking is how you swap two values idiomatically in Python. In other languages you write:
temp = a
a = b
b = temp
In Python you write:
a, b = b, a