swcarpentry / python-novice-gapminder

Plotting and Programming in Python
http://swcarpentry.github.io/python-novice-gapminder/
Other
163 stars 429 forks source link

Replace the basic summation example with a simple simulation of coin-… #637

Open augeas opened 1 year ago

augeas commented 1 year ago

Issue #636:Examples / exercises encourage anti-patterns in 12-for-loops.md

Replace the example somewhat heavy-handed summation over a loop variable with a simulation of coin tosses:

1) It's entirely valid for an accumulator to not depend on the loop variable, c.f. Monte-Carlo integration. 2) We shouldn't solve the problem of range starting from zero with spurious additions in a loop, just use range properly. 3) A trivial summation over the loop variable should be done with the built-in sum function. 4) The later exercise with a cumulative sum is a far better example.

Imports have been introduced previously, so it's probably okay to use random.random() Other examples in the episode use if statements, but this is just more justification to switch the conditionals and loops episodes; conditionals are more fundamental than loops, if you don't have conditional branches, you're not Turing-Complete, and you don't really have a language.

Delete the string revering exercise. 1) Strings, and other sequences are more idiomatically reversed with slice operators. 2) The other exercises with string accumulators are much better.

On branch loops_grg modified: episodes/12-for-loops.md

github-actions[bot] commented 1 year ago

:ok: Pre-flight checks passed :smiley:

This pull request has been checked and contains no modified workflow files, spoofing, or invalid commits.

It should be safe to Approve and Run the workflows that need maintainer approval.

alee commented 1 year ago

Thanks for the PR! I like this change personally but would like to gather feedback from the rest of the community as well before moving forward.

I believe the original intent of re-implementing sum was to show a novice with minimal programming languages experience how something like creating a summation from 0-N could be implemented. That said, if it were to remain as is I definitely agree a followup with the built-in sum as the clearly better solution should be included.

I don't think we introduce format strings in the lesson so there should be some explanation of this either now or earlier.

Please keep in mind that this is not a foundational computer science course and so statements of Turing-completeness as to whether conditionals should be introduced before loops may not track with the intended audience. There have been multiple suggestions to reorder the episodes and ordering and I don't know that we've ever found true consensus yet...

e.g.,

https://github.com/swcarpentry/python-novice-gapminder/pull/294 https://github.com/swcarpentry/python-novice-gapminder/issues/113 https://github.com/swcarpentry/python-novice-gapminder/issues/548

(etc)

martinosorb commented 1 year ago

I generally agree on that exercise being not great. I taught it before and left some students puzzled, even though the majority are ok with it. However I would like to point out that your example has a random.random() < 0.5 line that is not necessarily intuitive. I imagine if I taught that, I would have to stop and explain for quite a while what that is for. Perhaps np.random.choice((0, 1)) could be an option?