+ 1

Does FOR work only with lists?

I mean does it always have to be: for [variable] in [list]: do something

29th Apr 2017, 6:01 PM
Kaspar L. Palgi
Kaspar L. Palgi - avatar
3 Answers
+ 6
for is an iterating loop. You can let it go over any iterator you can think of - a list, a range, a dictionary, a string... and so on. The iterating variable will assume the value or reference to iterator's elements consecutively. There are certain features when it comes to details. For example range(x, y, z) can be iterated from x to y (not inclusively) with the step of z. So range(10, 3, -2) will go over (10, 8, 6, 4).
29th Apr 2017, 6:25 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 13
If the term list you are using means 'iterables' then the answer is yes. And with that you can do everything you need.
29th Apr 2017, 6:16 PM
Ulisses Cruz
Ulisses Cruz - avatar
0
no you can also do for i in range (5, 10)
29th Apr 2017, 6:13 PM
Edward