+ 3

For Loop Explanation

I finished all basic 'for' loop sections and I still don't have a great grasp on the concept. I don't want to continue the python 3 tutorial unless I have a perfect understanding of the for loop.

16th Nov 2019, 3:21 AM
Riggonatio
Riggonatio - avatar
9 Réponses
+ 5
Hi Sebastion __IAS__ has given a good answer, but I believe you are looking for a pythonic answer. range(x) => means a group of numbers which start from 0 to the designation (x) which will be a number. 5 for example. for i in range(5): print("hello) This code is saying, " For the iterations of the numbers from 0 to 4, print the word hello" So the code runs through the iterations: 0 => hello 1 => hello 2 => hello 3 => hello 4 => hello
16th Nov 2019, 8:49 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
For loop executes the code inside it's body for a number of times you want. Imagine you want to print hello 5 times. (All codes below are in pseudo. They may or may not match python syntax) You could do this. print("hello") print("hello") print("hello") print("hello") print("hello") Or you can do the same thing with this for(int i = 0; i < 5; i++) { print("hello") } Also you have the 'i' which tells you which count you are currently in. if you were to change the code to this: for(int i = 0; i < 5; i++) { print("hello" + i) } First time it will print hello0 Second time it will print hello1 This 'i' (or index) can be useful. Imagine you wanna set all members of an array to zero. int array[5] for(int i = 0; i < 5; i++) { array[i] = 0 } As you can see this code has the same effect as array[0] = 0 array[1] = 0 array[2] = 0 array[3] = 0 array[4] = 0
16th Nov 2019, 4:32 AM
__IAS__
__IAS__ - avatar
+ 4
In some languages there are ways to use for loop to get each member of an array, map etc like this for( element : array) { element = 0 } but don't worry about it now. It has the same effect as for(int i = 0; i < 5; i++) { array[i] = 0; }
16th Nov 2019, 4:34 AM
__IAS__
__IAS__ - avatar
+ 4
Rik Wittkopp and __IAS__ Thank you for all your help and telling me how to tag someone. I really appreciate it. All questions were answered perfectly, thank you. I wish I could give you both the best answer because you each explained them very well. Again thank you and I wish you luck on your future programming adventures.
16th Nov 2019, 2:08 PM
Riggonatio
Riggonatio - avatar
+ 3
Hi Sebastian Rohrer If you put @ before someone's name, they get a notification of your post. You can use this to attract their attention. 👍
16th Nov 2019, 9:14 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
_IAS_ I understand the three argument way for this, but when I was going through the course, it had lines such as for i in range(x): This makes no sense and I don't understand what's it's reading, what the "I" is for and what range means
16th Nov 2019, 5:31 AM
Riggonatio
Riggonatio - avatar
+ 2
Sebastian Rohrer sorry I didn't get the notification. Somehow I wasn't tagged properly in your answer. Rik has explained it really well.
16th Nov 2019, 9:08 AM
__IAS__
__IAS__ - avatar
+ 2
Sebastian Rohrer really glad to help you. Wish you the same. Also feel free to contact anytime either through private messages or tagging if you need help. :)
16th Nov 2019, 2:14 PM
__IAS__
__IAS__ - avatar
+ 2
Sebastian Rohrer Thanks buddy. Keep coding 👍
16th Nov 2019, 9:53 PM
Rik Wittkopp
Rik Wittkopp - avatar