#
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. Python has the for in loop. So here's a list of numbers that we were working with we want to just loop through them

0:11
or any collection, anything that is iterable in Python's terms. We just say for n in nums: and then we work with n.

0:19
We just loop over a collection like objects. These can be lists or arrays. These can be strings these can actually be classes

0:29
that just implement the right interfaces and we can treat them like this. If you want the idx that goes along with an item in your for in loop

0:39
we just use enumerate. So, enumerate(nums, start=1) instead of just for n in nums. Then we have this tuple projection

0:48
we get a tuple back when we assign it to two variables its elements at two variables the idx and the n and here, we can say, we can work with the idx

0:55
n in each time through the loop. We also saw that Python does not have a numerical for loop but we can get back

1:03
to something really, really close to that, super easy. We can just say for i in range and we give it a range, zero to 100.

1:10
And that'll go from 0 to 99 stepping by one, right? And then, we just work with i and change things like the step and so on

1:17
so there's a lot of flexibility there. But there's no traditional for i = value i < such and such that you get from C#, C++, Java and so on.

1:28
Alright, that's it. Looping in Python is a joy.