Why are loops in Python so confusing


Seriously, the syntax is pretty much straightforward in C++, you choose a variable i, iterate it n times and in a string, you access each alphabet using string[i] inside the for loop.
But in Python, you can do it in different, either using “for i in string” or by using “for i in range(len(string))” (here individual elements of the string are accessed using string[i] while in the former, it can be directly accessed using i) and ■■■■, just look at all the confusion, and while using multiple loops, these two methods collide and then end up in errors and … it ends up being a mess
Can someone explain this to me?
Like… this is why I feel loops in C++ are easier to understand!
Okay okay maybe I’ve gotten used to C++ but come on… there’s got to be some logic to this!


Python is supposedly easy compared to other languages, but it’s fine if you feel confused. Here, let me tell you how temporary loops work:

for i in range(13):
    print("Spam and eggs!")

Okay, so for means it’s going to start a loop. i is the iteration of the loop, or what it may use inside the loop. range is how much the loop should go, and (13) is the argument of range.

You can replace 13 with almost anything, including functions that return integers:

a = "Hello World!"
for i in range(len(a)):

print("a is " + str(len(a)) + " characters long")

You don’t have to include methods like len() or str() in your code, just use easy things like i or print()!


actually, getting the values directly from a list or a string can be very useful, makes for easy to read code although in the beginning it might take some getting used to if you come from a C family language

but python is generally a lot easier, C++ is very complicated and even more massive. Just comparing loops is not comparing the languages at all


Thanks for your explanation, yes I did get it but yeah it does confuse me every now and then, and also

a = “Hello World!”
for i in range(len(a)):

this prints the numbers from 0-11 right?


True that, in some cases so far I do feel Python is a lot easier!


range isn’t a part of a for-loop. This is like saying addition is complicated because you have a really complicated expression as one of the terms:

1 + (3/2*2**10)

Python’s for-loop has an equivalent in cpp: foreach

If something is already iterable it’s pretty weird to iterate through numbers [0…] just to look up elements. You may as well create a range to iterate through your range too, and a range for that, and so on


So does that mean I can’t make a for loop iterate from m to n, where m is non zero? like I can’t iterate from 5-9 using a for loop? And can’t I use “for i in range(m,n)” for that?


You can describe those numbers as a range and then iterate through it.
But if you start using those numbers as indices then you should probably be iterating over that substring instead - so the real question is why you’d need the numbers 5 through 9


No I didn’t ask that in a practical sense but… I was just thinking about it