Python Program Doubt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python Program Doubt

# Fibonacci series: # the sum of two elements defines the next a, b = 0, 1 while b < 10: print(b) a, b = b, a+b What is the meaning of (a, b = b, a+b)? Please explain it in detail.

9th Mar 2019, 4:14 AM
Prince Gupta
Prince Gupta - avatar
3 Answers
+ 5
In Python you can set values to multiple variables in one line, separated by commas, to each value respectively. In this case, variable a becomes the value of b, and variable b then becomes the original value of a added to the value of b.
9th Mar 2019, 4:25 AM
Nathan Lewis
Nathan Lewis - avatar
+ 1
The Fibonacci Series requires a and b, combined to make the next value of b. It is a simplified version of writing: a_original = a a = b b = b + a_original For Example: a = 0 b = 1 a_original = a #0 a = b #1 b = a_original + b #0 + 1 or 1 The simplified version prevents you from needing the temporary a_original variable, and sets the variables for the next values in the sequence: a = 0 b = 1 a, b = b, a+b or a, b = 1, 0+1 #a==1 #b==1
9th Mar 2019, 6:20 AM
Nathan Lewis
Nathan Lewis - avatar
0
Could you be more precise as I could not understand till now ?
9th Mar 2019, 6:08 AM
Prince Gupta
Prince Gupta - avatar