A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

could someone show me an example of iteration?

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

    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.

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

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

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

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

  4. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  5. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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

  6. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

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

    ok

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

    I like so far I am understanding it

  9. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  10. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

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

    thanx :-)

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

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

  13. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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

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

    ok

  15. 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...

23

  • 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.