New to python and I need some help with loops.
What's the closest thing to a C++ for loop in Python?
if you playing with your for loop counter variable outside of the loop header, you are doing it wrong, c++ or not.
For loops are for cases that can be clearly defined in advance, it's while loops that are supposed to be more flexible about their control variables.
provide an example where this proves useful and has no cleaner equivalent.
More on reddit.com[Python] Why would I use a for loop instead of a while loop?
for means "do something with each of these things".
while means "keep doing this until something is no longer true".
So... say what you mean. Readability counts.
ELI5 Python "for" loops
Do you understand the basic concept of loops? You use a loop when you want to repeat an operation a certain number of times.
You can divide loops up into two different categories if you like (it's not essential). The first one is where you know, before you even hit the loop, exactly how many times you'll need to repeat the operation. If, for example, you want to add the numbers 1-10 together (or even 1-n) then you can compute how many times you'll need to loop before you even do the loop. The second is where you don't know how many times you'll need to loop, but you know the ending condition. An example here would be a program that accepts numbers typed in by the user until the user enters '-1'. Then, stop. How many times are we going to loop? Beats me. Could be 1 time or 1 million times. We'll stop when we stop.
As it turns out, you don't really need to distinguish between these two cases. In both cases it's "Do this this until this ending condition happens", but some languages have special constructs that make it easier to break them up this way.
With me so far?
That's the general idea of loops. There are also some Python specific ideas, which may be what you are getting hung up on. Can you give an example of something that doesn't make sense and maybe we can help you with that.
More on reddit.comVideos
For the past 2 months I have been doing the Codecademy Python 3 course and last month I went into loops and functions and I was incredibly overwhelmed just like I was in Java. Functions are easy nothing special, at least yet, but loops are made me take that month break.
I know the theory of loops. I think I know what commands should be used where and when but for the love of god I can not write a simple loop, even after countless errors. Could anyone point me in the right direction? I really dont want to quit another language for the same reason.
Edit: a user pointed out that I need to elaborate on "simple loops". For example if I had a list and wanted to print a sentence as many times as the length of the list I know I would use len and range and have the print statement inside the loop but I can't implement it