Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
augeas opened this issue May 3, 2023 · 2 comments
Open

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

augeas opened this issue May 3, 2023 · 2 comments

Comments

@augeas
Copy link

augeas commented May 3, 2023

There are two examples/exercises whose solutions encourage anti-patterns, miss basic Python features introduced previously, and use the language non-idiomatically. We shouldn't add the confusing cognitive load of "Here's an example, but don't do it this way.".

  1. Intro to the accumulator pattern:

To be worthwhile, the accumulated values should be somewhat non-trivial, students should not leave the session thinking that a for loop is required to merely sum over an iterable:

sum_to_10 = sum(range(1, 11))

This is a missed opportunity to introduce "sum" in the previous built-in functions section.
Solving the problem of range(10) going from 0-9 would be better solved by explaining that one wanted range(1,11) rather than a superfluous extra addition.
If a cheap-and-cheerful Monte-Carlo simulation to estimate Pi is too much, maybe just count the number of heads in some coin-tosses?

  1. Reversing a String:

This is not a good use for a for loop. Slice operators have already been introduced previously, and students should leave thinking that this is how you reverse a string.
reversed_string = 'This is not a palindrome![::-1]

Easy. Yes, this demonstrates that iteratively appending items from a collection will cause them to be in reverse order, but why would anyone do that?

@alee
Copy link
Member

alee commented May 11, 2023

Thanks for the feedback! Minor pedantic note that the simplest way to sum to 10 doesn't need the first parameter, sum(range(11)) suffices.

The string reverse slice operator requires a clear explanation to remove it from the realm of magic but I think it would be a great addition to cement the conceptual model of how slicing works in Python.

There are a fair number of things missing from this lesson in its original incarnation that I believe were deliberate design decisions and that I've attempted to honor during my time as a maintainer with the awareness that this scope / vision should evolve over time alongside the instructors and learners using the course...

  1. list / dict / set comprehensions
  2. lambdas / functions as first class values, example-ridden explanation of call-by-object-reference semantics without the jargon
  3. ???

@martinosorb
Copy link
Contributor

There is also a discussion on whether to move conditionals up before for loops. For-loop exercises with conditionals should make much more sense and be more intuitive, imho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants