Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Concept: Python loops

Login or purchase this course to watch this video and the rest of the course contents.
0:00 C# has the foreach loop.
0:02 Python has the for in loop.
0:05 So here's a list of numbers
0:08 that we were working with
0:09 we want to just loop through them
0:10 or any collection, anything that is iterable
0:13 in Python's terms.
0:14 We just say for n in nums:
0:17 and then we work with n.
0:18 We just loop over a collection like objects.
0:22 These can be lists or arrays.
0:25 These can be strings
0:27 these can actually be classes
0:28 that just implement the right interfaces
0:31 and we can treat them like this.
0:34 If you want the idx
0:36 that goes along with an item in your for in loop
0:38 we just use enumerate.
0:39 So, enumerate(nums, start=1)
0:41 instead of just for n in nums.
0:45 Then we have this tuple projection
0:47 we get a tuple back when we assign it to two variables
0:49 its elements at two variables
0:51 the idx and the n
0:52 and here, we can say, we can work with the idx
0:54 n in each time through the loop.
0:57 We also saw that Python does not have a numerical for loop
1:01 but we can get back
1:02 to something really, really close to that, super easy.
1:04 We can just say for i in range
1:07 and we give it a range, zero to 100.
1:09 And that'll go from 0 to 99 stepping by one, right?
1:13 And then, we just work with i
1:15 and change things like the step and so on
1:16 so there's a lot of flexibility there.
1:18 But there's no traditional for i = value
1:22 i < such and such
1:23 that you get from C#, C++, Java and so on.
1:27 Alright, that's it.
1:28 Looping in Python is a joy.