**Loops in Python: A Comprehensive Guide**
If it's negative x equals x plus 2, in each iteration we still want to print the value of x if we run this we see that the first few iterations x is less than zero so it is added two and then on the final iterations where it is greater than or equal to zero it is added one. This demonstrates a key concept in programming: loops. A loop allows us to execute a set of instructions repeatedly, based on certain conditions.
A **while** loop behaves in much the same way as an iterative process. It takes the structure of `while condition: code` and then the code to run. In this structure, the condition is checked at the beginning of each iteration, and if it's true, the code inside the loop is executed. If the condition is false, the loop ends.
A **for** loop behaves in much the same way as a while loop, except it iterates over an editable object such as a list or dictionary. It takes the structure of `for variable in iterable: code` and then the code to run. In this structure, each element in the iterable is assigned to the variable, and the code inside the loop is executed for each element.
For example, we can write a basic **for** loop here, where we already have a list defined of words and we're going to say `for word in words: print(word)`. In this case, `word` is the variable name we're assigning to each element in the list. When running this, we iterate through each element of the list and print it out.
Another technique you may want to use is to print or run a loop a certain number of times and to do so, you can use the **range** function. For example, here we're going to write `for i in range` and then we pass in the number of times we want this to run. In this case, we want it to run five times, and we're going to print "This is a loop" and also print out the value of `i`. When running this, we iterate through each iteration, starting at zero because Python is zero-indexed.
You can also iterate over different types of Python objects not just lists. Here, we're going to iterate over each key-value pair in a dictionary using the **items** method. To do so, we write `for key, value in` and in the name of the dictionary, in this case, "countries". We then call the **items** method, and then we'll print the key and the value.
When running this, we've iterated through each pair in the dictionary and printed out the key and the value. You can also use the **keys** and **values** methods to print out the key or the value individually, rather than just returning both.
Another Python object you may want to iterate through is a pandas DataFrame, which is a two-dimensional data structure. It behaves in much the same way as the **items** method we just went over. Here, we have a DataFrame with country and population information, and what we're going to do is say `for index, row in` the DataFrame.
We'll access one column using the `df['column_name']` syntax, and we'll print out the value of that column for each iteration. When running through this, we've retrieved for each row the index and the data in the country column.
Sometimes, you may want to control a loop using advanced techniques. One such technique is to use **break** and **continue** statements. Here, we have a list of duck, duck, duck, goose, and what we're going to do is iterate through each bird in this bird's list and if the bird equals "goose", we are going to use the **break** statement to terminate the loop.
When running this, it iterates through each bird and as soon as it hits "goose" it ends the loop entirely. If you just want to end the current iteration, you can use the **continue** statement. Let's copy and paste our code from above but this time if it's "goose", we're going to continue, and when running this, what we find is that it runs through each element of the list and when it hits "goose" it ends the iteration and does not print the bird, which would be "goose" and continues on.
The final thing we're going to cover today is adding an **else** statement after a loop. This will run a selected block of code after every iteration is still complete. It will not execute if a **break** is included. Here, we're going to say `for bird in birds: ... else no goose`.
This loop iterates through the birds list and if the bird is "goose", it's going to print "found a goose" and end the loop. Otherwise, it's going to print "no goose". When running this, there's a goose, so we print "found a goose". If we run this again without a goose, we print "no goose".