Viewing a response to: @fabiyamada/re-scipio-learn-python-series-intro-20180226t163303937z
Thx Fabi! :-) PS, don't get confused by the `a, b = 0, 1` or the `a, b = b, a + b`notations in the `fibonacci()` function! That's a very good remark of yours (well, you didn't explicitly mention it, but I was hoping already somebody would ask me a question about it, because explaining it in-depth inside the article body would have made the Intro episode even longer! ;-) ). But let's look at it closely... * At variable initiation, `a,b = 0,1` could be re-written as: ``` a = 0 b = 1 ``` * However, the same thing **cannot** be said about re-writing `a, b = b, a + b` **inside the `while` loop**! ``` a = b b = a + b ``` ... would result in the sequence `[0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512]`, which is not a Fibonacci sequence (but setting 0 to 1 and then doubling it every iteration). The expression `a, b = b, a + b` is happening "at the same time", in **parallel** and not **sequentially**. At the first iteration `a=0` and `b=1`, so in the loop `a` then is set to what `b` **was** and `b` is set to what `a` **was** plus what `b` **was**. ``` # iteration abbreviated to i # i(0) => a = 0 and b = 1 # i(1) => a = 1 and b = 0 + 1 = 1 ... and add 0 to seq [0] # i(2) => a = 1 and b = 1 + 1 = 2 ... and add 1 to seq [0,1] # i(3) => a = 2 and b = 1 + 2 = 3 ... and add 1 to seq [0,1,1] # i(4) => a = 3 and b = 2 + 3 = 5 ... and add 2 to seq [0,1,1,2] # i(5) => a = 5 and b = 3 + 5 = 8 ... and add 3 to seq [0,1,1,2,3] # i(6) => a = 8 and b = 5 + 8 = 13 ... and add 5 to seq [0,1,1,2,3,5] # etc. etc. etc ``` So, we could have re-written, inside the `while` loop `a, b = b, a + b` to: ``` # first "freeze" the current values a_old = a b_old = b # then change a and b a = b_old b = a_old + b_old ``` PS: this series is **not** about me demonstrating how great **my Python** is. It's about -a- showing how cool Python, the language, is, and -b- developing **your** Python SuperPowers! So: have fun using Python, my Learn Python Series, and **feel free to ask me questions**! @scipio
author | scipio |
---|---|
permlink | re-fabiyamada-re-scipio-learn-python-series-intro-20180227t100433348z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"users":["scipio"],"app":"steemit/0.1"} |
created | 2018-02-27 10:04:33 |
last_update | 2018-02-27 10:19:36 |
depth | 2 |
children | 3 |
last_payout | 2018-03-06 10:04:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 1.992 HBD |
curator_payout_value | 0.582 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,001 |
author_reputation | 34,229,826,851,736 |
root_title | "Learn Python Series - Intro" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 40,830,820 |
net_rshares | 451,465,981,672 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
someguy123 | 0 | 301,170,297,160 | 17% | ||
antidorsessions | 0 | 3,087,214,714 | 100% | ||
knowledges | 0 | 5,922,410,917 | 100% | ||
fabiyamada | 0 | 12,249,738,719 | 27% | ||
scipio | 0 | 109,319,842,194 | 100% | ||
amosbastian | 0 | 19,716,477,968 | 100% |
:o! It is a bit clearer! I think I need to experiment with this! (I will google for more practical examples)... What I understand is you can set the value of 2 vars in the same line a, b = 0, 1. Which is so new for me! And seems that is what makes the magic happen... And you create a second line to take the old values and make it work... Oh maybe I am not making any sense x.x I will study sensei!!
author | fabiyamada |
---|---|
permlink | re-scipio-re-fabiyamada-re-scipio-learn-python-series-intro-20180227t123607422z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-02-27 12:36:15 |
last_update | 2018-02-27 12:36:15 |
depth | 3 |
children | 2 |
last_payout | 2018-03-06 12:36:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.184 HBD |
curator_payout_value | 0.058 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 402 |
author_reputation | 55,606,801,081,779 |
root_title | "Learn Python Series - Intro" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 40,859,013 |
net_rshares | 42,649,000,416 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
scipio | 0 | 42,649,000,416 | 44% |
You are making sense, and yes that's exactly how `a, b = 0, 1` works! Initializing two variables with their own value in one line. PS: in case you would have multiple variables all set to the same value, you could do this: ``` a = b = c = d = 'fabiyamada' print(a, b, c, d) # fabiyamada fabiyamada fabiyamada fabiyamada ```
author | scipio |
---|---|
permlink | re-fabiyamada-re-scipio-re-fabiyamada-re-scipio-learn-python-series-intro-20180303t175105590z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-03-03 17:51:06 |
last_update | 2018-03-03 17:52:12 |
depth | 4 |
children | 1 |
last_payout | 2018-03-10 17:51:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.392 HBD |
curator_payout_value | 0.125 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 326 |
author_reputation | 34,229,826,851,736 |
root_title | "Learn Python Series - Intro" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 41,926,303 |
net_rshares | 120,926,948,140 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
fabiyamada | 0 | 14,711,235,126 | 25% | ||
scipio | 0 | 106,215,713,014 | 100% |
Thanks scip!
author | fabiyamada |
---|---|
permlink | re-scipio-re-fabiyamada-re-scipio-re-fabiyamada-re-scipio-learn-python-series-intro-20180303t184550437z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-03-03 18:45:51 |
last_update | 2018-03-03 18:45:51 |
depth | 5 |
children | 0 |
last_payout | 2018-03-10 18:45:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 12 |
author_reputation | 55,606,801,081,779 |
root_title | "Learn Python Series - Intro" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 41,936,445 |
net_rshares | 0 |