anonymous
  • anonymous
could someone show me an example of iteration?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
carlsmith
  • carlsmith
Of course. for each in 'abcdef': print each This would print a b c d e f An iterator takes a sequence, in the above example, a sequence of characters, 'abcdef', and it assigns each one, in turn, to a variable, we've called it each, which is pretty conventional. So you're saying: First assign the first item in the sequence, the 'a', to `each`, then run the indented block, in our case this is one line of code, print each, but it could whatever you needed to do. The computer does what the block says, print each, with `each` evaluating to 'a'. Once it finishes the block, it returns to the top and assigns the next value, 'b', to `each` and runs the block again. Once it has run the block for every item in the sequence, the iteration is done.
carlsmith
  • carlsmith
Let me go eat my dinner and if you need a bit more, I'll be happy to discuss it with you.
anonymous
  • anonymous
sure when your back could you differentiate a while loop as oppose to a for loop, is there any difference really?

Looking for something else?

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

More answers

carlsmith
  • carlsmith
Hi there. A while loop is very similar, but a while loop checks the condition once each time it completes the block and continues indefinitely. It'll only end when the condition is False. a = 0 while a < 10: print a a += 1 The above checks if `a` is less than 10 and, if so, it executes the block, if not, the loop ends immediately. If the block is run, once it has finished, the expression a < 10 is re-evaluated and the same happens again. The difference is in that a while loop can continue forever while 1 < 2: print 'stuck here forever' because it checks to see if an expression is True every time it loops. The for loop, does not check to see if an expression is True or False, instead it takes a sequence and assign each object in the sequence to a variable, then runs the block, once for each item in the sequence. Both can be very useful and they do do different things, but they both do loops. You can often use while loops for making sure something is done before moving on. keep_looping = True while keep_looping: if a == b: some_function() keep_looping = False else: a = some_other_function() # now try again You can use for loops for going over list or strings, stuff like that. name = 'Jane Lee' for letter in name: if letter == 'L': print True alist = [1, 2, 3, 4] for item in list: print (2 + item) * 4 That kind of thing.
carlsmith
  • carlsmith
A while loop is a lot like an if conditional, but it keeps going back and checking again. The for loop only deals with sequences. You can generate a sequence of numbers with the range() function, then iterate across the list of numbers it returns. You can guess what the following would do pretty easily. for number in range(10): print number
carlsmith
  • carlsmith
Here's a really simple script that contains while, for, if and else blocks. Let me know if there's anything you don't quite get. I'll be back and forth for a few hours and it's no trouble.
anonymous
  • anonymous
ok
anonymous
  • anonymous
I like so far I am understanding it
carlsmith
  • carlsmith
Good. I'm glad I could help out. I know it can be difficult getting your head round these ideas at first, but it does get a lot easier once you get into OOP. As you define you're own objects, all the built in stuff becomes much less like magic and more like common sense. You just have to try and remember everything at first and trust that it'll work. Very soon, it'll all look plain and simple and you can focus on what you're reading and writing, rather than the language it's written in. Python seems to be especially good at this.
carlsmith
  • carlsmith
If you want an open source introduction to Python, the book I've linked to below is worth looking at. Though it's needs updating, it's a good thing to bookmark if you're just starting out. Remember, the MIT course is meant to teach computational science to math majors. It's not an introduction to Python, despite thousands of people trying to learn Python that way. It's a quality resource, but for learning CS, and then, only if you are pretty good at maths, or willing to really struggle. I don't think people do themselves any favours when trying to learn to program by solving math problems that they are simultaneously struggling to understand. It's like trying to learn physics, but in a foreign language, with the hope that you'll somehow end up fluent in both. You'll probably just go crazy. This is a link to the book anyway. http://openbookproject.net/thinkcs/python/english2e/index.html All the best.
anonymous
  • anonymous
thanx :-)
anonymous
  • anonymous
the book seems to cover most of what I am looking for
carlsmith
  • carlsmith
No worries. The book could do with fleshing out a bit and updating, but you can use this to start from and look into each topic more online, pop in here and discuss anything you can't find online. Nice one, hope to see you about rekkimini
anonymous
  • anonymous
ok

Looking for something else?

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