A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 5 years ago

"for" loops are throwing me for a loop. In the example: for numChickens in range(0, numHeads+1), I am having difficulty wrapping my head around the numChickens variable. Where is it declared or initialized? What, exactly, are we iterating here (i.e. for...what, exactly)?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    In general a loop will assign (or bind as they like to say in this course) an initial value to a 'loop variable' (which would be numChickens) then increment that variable (by 1 as a default) until the highest value in the range is reached. Thus, in examining the code you have quoted, numChickens appears to be assigned to 0, incremented by one, until it reaches numHeads (or that plus one) Whether numChickens ever is used with the value numHeads + 1 depends on the 'boundary case' which I am sure we can cover if need be... CLK

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Fydor's explanation is correct in languages like C, C++ and to some extent Java. However, in python for loops work very differently. In python the for loop has the form: for variable in container: block of statements working with variable This code is semantically equivalent to i = 0 while i < len(container): variable = container[i] block of statements working with variable i += 1 If you look at the range function, you'll see that all it does is return a list of numbers from 0 to the number you've passed as your argument. So range(5) returns [0,1,2,3,4], and range(numHeads+1) would return [0, 1, 2, ..., numHeads-1, numHeads] Though it will use the actual numbers, not the symbolic values I've described here.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Polpaks explanation offers some details of how Python assigns value to the loop variable. I suppose it is a matter of opinion to say that Python loops are 'very different' from other language loops. The following two loops produce the same output: in c: int numChickens; int numHeads = 5; for(numChickens = 0; numChickens < numHeads + 1; ++numChickens) printf("%d \n", numChickens); in Python numHeads = 5 for numChickens in range(0, numHeads + 1) : print(numchickens) Both loop forms can be generalized, and it is true that at least conceptually there is the idea in Python of iterating through a container (defined by the range operation) while in c the concept is more of defining the n+1st term of a sequence in terms of the n'th term.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...


  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.