For loops
1. For loops
We've learned how to work with individual values and data collections. Now, let's explore a key development skill: automating repetitive tasks with loops.2. Individual comparisons
Developers often need to process submissions and validate values. If we had to write separate code for each item, it would become very lengthy and difficult to maintain. For example, if we have a list of ingredient quantities, we could check each one by index, but what if there are 100 ingredients?3. For loop syntax
Python provides for loops to handle this efficiently. A for loop says, "For each value in a sequence, perform this action". Notice the action is indented because the line before ends with a colon. The sequence is known as an iterable, as it can be iterated over. It can be any data structure with multiple elements. The value is known as an iterator. It is a placeholder we can name however we like. It's common to use i as shorthand for the index or item.4. Print individual values
For our recipe scaler, let's loop through the ingredients list and print each one. We write: for ingredient in ingredients, then print the ingredient. Python takes each item from the list, one at a time, and runs the indented code. Here, ingredient is the iterator, and ingredients is the iterable. Python interprets this as "print the first ingredient, then the second," and so on, showing each on its own line in the output.5. Conditional statements in for loops
In development work, we often need to make decisions or take specific actions based on the findings. This is where combining loops with conditionals becomes powerful. Let's check our ingredient quantities and categorize them.6. Conditional statements in for loops
We loop through the quantities and use an if statement to check whether each one is greater than 500 grams, at least 100 grams, or below 100 grams. This is the same if/elif/else syntax we learned before, but it is indented inside the for loop.7. Conditional statements in for loops
The output shows which quantities fall into each category, giving us actionable information about our ingredients.8. Looping through strings
Loops aren't just for lists. We can also loop over strings. Developers use this for tasks like validating passwords character by character, checking for special characters in usernames, or analyzing text input. This code prints each letter in our ingredient name, one at a time.9. Looping through dictionaries
To loop through a dictionary's keys and values we can use the .items() method. As this produces tuples of keys and values, we need to provide two iterators, one for the keys, item, and another for the values, qty. The print statement here tells Python to print several pieces of information together: the value of item, a colon, the value of qty, and the word "grams".10. Looping through dictionaries
Inside the loop, we can use both variables. For example, if we want to scale our recipe by a factor of 2, we can calculate the new quantities. We create a variable scaled_qty, which equals qty multiplied by 2.11. Looping through dictionaries
If we just want the keys or values we can loop over the .keys() or .values() methods respectively, using a single iterator.12. Range
One final and common for loops use case is repeating an action a set number of times with the built-in range() function. It starts at one number and stops before the end number, so the last value is one less than what we provide. range() is often used to generate or modify values. For example, to print numbers 1 through 5, use range(1, 6).13. Let's practice!
Let's loop into some exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.