Python has a bunch of different ways of doing loops that come in handy at different times.

While Loop

Let's start with the most basic: the while loop. This is quite like you'd be used to in other languages; you just have a boolean expression that keeps the loop going if it evaluates to True. For example, if we wanted to loop until we found a number that was divisible by both 3 and 7, we'd write the following loop:

Of course, the while loop can be used for anything. So we can use it in a scenario where we want to loop up to a particular range, even though a for loop is technically better than that. For example, an equivalent to the C++ or Java loop

for (int i = 10; i < 100; i+= 2) {

could be written as

Actually, a quick note here that python unfortunately does not have the var++ notation.

So if you wanted to increment by 1, you'd have to say i += 1

If you want to stop here, that's fine! I don't really care if you do things the most "pythonic way," because this course is more about data structures than python style. But if you want to make your code more succinct by using more appropriate loops, read on

For Loop

The for loop in python is more flexible than you're probably used to in other languages, and it may take a moment to really understand what's going on behind the scenes. But let's start with a scenario that's similar to a for loop that you'd do in Java or C++. If you had the code

for (int x = 174; x < 271; x+=10) {

the python equivalent would be

If you only want to increment by 1 every time, you can omit the third parameter to range

And if you want to start at 0, you can omit the first parameter

But as it turns out, there's a lot more you can do with for loops in python. For example, traditional way of looping through an array in Java would look like

for (int i = 0; i < arr.length; i++){

You can certainly follow this pattern in python using

But notice how we only ever use the variable i to index the array, and we're going through the entire array here. A more "pythonic" way to do this is with a for loop that doesn't store an index directly, but which simply loops through the elements in sequence:

So if you don't actually need to use the array index for anything and you just need to loop through some elements of a collection in sequence, this syntax is much more succinct.

If you do want to keep track of an index, there's a slightly more "pythonic" way of doing it using something called "enumerate"

In this case, the loop actually loops through two variables at once: the index i and the element x of the array. This saves you the ugly syntax of having to actually index the array, which can make code much cleaner. But you still know what the index is

Advanced Looping Through Tuples And Zip

If you have a list of tuples, you can write a really slick loop to unpack the elements of the tuple directly without having to index anything. For example

If you have two parallel arrays, you can turn them into an array of tuples using the python command zip. For example

(Side note: I had to put the list() around the zip because zip actually returns an iterator, which only returns items in sequence when we ask it to)

Putting it all together, we can loop through two arrays in parallel with

And if we want to get really fancy and pythonic, we can also use enumerate to keep track of our index as we go

Just a reminder that you can accomplish all of this with more traditional C++/Java-esque approaches:

But oftentimes the code ends up looking nicer inside the loop if we don't have to throw array indexing in everywhere