Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Better example of for looping through a list #141

Closed
wants to merge 1 commit into from

6 participants

@benhoyt

I strongly believe examples should show best practices -- for summing numbers, it's Pythonic and much faster/simpler to use the built-in sum(). The example would be just as simple and show the same thing if it was changed to multiplying a list of numbers to get their product.

Also, my example doesn't shadow the builtins sum and list.

@benhoyt benhoyt Better example of for looping through a list
I strongly believe examples should show best practices -- for summing numbers, it's Pythonic and much faster/simpler to use the built-in sum(). The example would be just as simple and show the same thing if it was changed to multiplying a list of numbers to get their product.
5181223
@alex
Collaborator

This looks good to me, but doesn't merge cleanly anymore, can you merge/rebase.

@peterlandry
Collaborator

Samples are in the database now. Can you push as a change to fixtures/codesamples.json?

@benhoyt

@peterlandry I'm not sure I want to update the JSON directly by hand here -- is that done via the Django admin? Are you able to update for me? I don't care about commit credit, I'd just like to see the example updated. :-)

@peterlandry
Collaborator

If you update the fixture, we'll ask the hosting team to load it. I'm just trying to prevent the fixture from getting out of synch with the database. Ultimately I'd like to be able to support community-curated examples, which is why they've moved to the database (you may have noticed the amount of critique directed at their contents!).

Editing it by hand is fine, as long as it will import cleanly with manage.py loaddata

@johanovic

I think the list example should be even easier. For complete newbies the idea of dynamically reassigning a new value to a variable might already be too much of a challenge.

+>>> numbers = [2, 4, 6, 8]
+>>> product = 5
+>>> for number in numbers:
+>>> print(product * number)

Sure, it is very basic, but I think the carrousel is trying to be basic, right?

@benhoyt

@peterlandry , thanks -- however, I'm afraid I don't have time to action this just now (I just moved half way round the world with my family :-). Anyone else who's interested, please go ahead and do it.

@peterlandry
Collaborator

I can probably take care of that this weekend. Sorry to put up barriers to better examples! I'll push a PR referencing this one with your amended code.

@benhoyt

Sounds great, thanks!

@merwok merwok commented on the diff
templates/python/index.html
@@ -74,12 +74,12 @@
<div class="slide-code">
<pre><code><span class="comment"># For loop on a list</span>
->>> list = [2, 4, 6, 8]
->>> sum = 0
->>> for num in list:
->>> sum = sum + num
->>> print("The sum is:", sum)
-<span class="output">The sum is: 20</span></code></pre>
+>>> numbers = [2, 4, 6, 8]
+>>> product = 1
+>>> for number in numbers:
+>>> product = product * number
@merwok
merwok added a note

Is it normal that the prompt is not a secondary prompt here?

You should still print product, right?

@merwok
merwok added a note

I don’t understand the question. What I meant was: is it normal that this line is a block but uses the primary prompt >>> instead of the usual secondary prompt ...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@benhoyt

@peterlandry -- any update on this?

@berkerpeksag
Collaborator

Superseded by #526.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 20, 2014
  1. @benhoyt

    Better example of for looping through a list

    benhoyt authored
    I strongly believe examples should show best practices -- for summing numbers, it's Pythonic and much faster/simpler to use the built-in sum(). The example would be just as simple and show the same thing if it was changed to multiplying a list of numbers to get their product.
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 6 deletions.
  1. +6 −6 templates/python/index.html
View
12 templates/python/index.html
@@ -74,12 +74,12 @@
<div class="slide-code">
<pre><code><span class="comment"># For loop on a list</span>
->>> list = [2, 4, 6, 8]
->>> sum = 0
->>> for num in list:
->>> sum = sum + num
->>> print("The sum is:", sum)
-<span class="output">The sum is: 20</span></code></pre>
+>>> numbers = [2, 4, 6, 8]
+>>> product = 1
+>>> for number in numbers:
+>>> product = product * number
@merwok
merwok added a note

Is it normal that the prompt is not a secondary prompt here?

You should still print product, right?

@merwok
merwok added a note

I don’t understand the question. What I meant was: is it normal that this line is a block but uses the primary prompt >>> instead of the usual secondary prompt ...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+>>> print('The product is:', product)
+<span class="output">The product is: 384</span></code></pre>
</div>
<div class="slide-copy">
Something went wrong with that request. Please try again.