2018-07-06 18:07:00 (edited by superb 2018-07-06 18:09:19)

Hello all.
I have read the Python 2.7.15 documentation on for statements, as well as a book about Python 3 on the subject, however it is still confusing trying to understand the full concept of what a 'for' statement does. I know it is used to count over  a certain number of items within a list, but how can it be used effectively in statements like this?
for i in range:
#code would go here.
That is where I am lost.
Can someone please help explain this part of the concept to me?
Thank you very much.

I love audio gaming!

Thumbs up

2018-07-06 19:12:09 (edited by pauliyobo 2018-07-06 19:13:55)

The for statement is an iterating loop.
This means that the loop iterates in a certain object until it reaches the counter.
The range function returns an object that contains a sequence of number in order until the limit you give.
So for example if you would type
range(10) the sequence would start from 0, to 10.
You can iterate through this object with the for loop.
so if you for example type:
for i in range(10):
.print(i)
The result that you would get is:
0 1 2 3 4 5 6 7 8 9, object starting is 0 based, if you give range(10) it'll  count till 9.
Hope you understood me.

PS: since I can't indentation on here I'll define an indented block with .

If you want to get in touch with me you can follow me on Twitter
have a nice day.
Paul

2018-07-06 19:56:50

Yes. That makes things a bit clearer.
Thank you.

I love audio gaming!

Thumbs up