Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (anonymous):

could someone show me an example of iteration?

OpenStudy (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.

OpenStudy (carlsmith):

Let me go eat my dinner and if you need a bit more, I'll be happy to discuss it with you.

OpenStudy (anonymous):

sure when your back could you differentiate a while loop as oppose to a for loop, is there any difference really?

OpenStudy (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.

OpenStudy (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

OpenStudy (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.

OpenStudy (anonymous):

ok

OpenStudy (anonymous):

I like so far I am understanding it

OpenStudy (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.

OpenStudy (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.

OpenStudy (anonymous):

thanx :-)

OpenStudy (anonymous):

the book seems to cover most of what I am looking for

OpenStudy (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

OpenStudy (anonymous):

ok

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!