For Loops in Python
For Loops in Python
This is the basic syntax to write a for loop in Python:
The iterable can be a list, tuple, dictionary, string, the sequence returned by range, a file, or any other type of iterable in Python. We will start with
. The
function in Python This function returns a sequence of integers that we can use to determine how many iterations (repetitions) of the loop will be completed. The loop will complete one iteration per integer. This is the general syntax to write a for loop with
:
As you can see, the range function has three parameters:
: where the sequence of integers will start. By default, it’s
.
: where the sequence of integers will stop (without including this value).
: the value that will be added to each element to get the next element in the sequence. By default, it’s
. You can pass 1, 2, or 3 arguments to
: With 1 argument, the value is assigned to the
parameter and the default values for the other two parameters are used. With 2 arguments, the values are assigned to the
and
parameters and the default value for
is used.With 3 arguments, the values are assigned to the
,
, and
parameters (in order). Here we have some examples with one parameter:
Output:
Output:
In the example below, we repeat a string as many times as indicated by the value of the loop variable:
Output:
We can also use for loops with built-in data structures such as lists:
Output:
These are some examples with two parameters:
Output:
Code:
Output:
Code:
Output:
Code:
Now the list is:
These are some examples with three parameters:
Output:
Code:
Output:
Code:
Output:
How to Iterate over Iterables in Python We can iterate directly over iterables such as lists, tuples, dictionaries, strings, and files using for loops. We will get each one of their elements one at a time per iteration. This is very helpful to work with them directly. Let’s see some examples: Iterate Over a String If we iterate over a string, its characters will be assigned to the loop variable one by one (including spaces and symbols).
We can also iterate over modified copies of the string by calling a string method where we specify the iterable in the for loop. This will assign the copy of the string as the iterable that will be used for the iterations, like this:
Iterate Over Lists and Tuples
The output is:
Code:
Output:
Iterate Over the Keys, Values, and Key-Value Pairs of Dictionaries We can iterate over the keys, values, and key-value pairs of a dictionary by calling specific dictionary methods. Let’s see how. To iterate over the keys, we write:
We just write the name of the variable that stores the dictionary as the iterable. For example:
To iterate over the values, we use:
For example:
To iterate over the key-value pairs, we use:
If we define only one loop variable, this variable will contain a tuple with the key-value pair:
Break and Continue in Python Now you know how to iterate over sequences in Python. We also have loop control statements to customize what happens when the loop runs:
and
. The Break Statement The
statement is used to stop the loop immediately. When a
statement is found, the loop stops and the program returns to its normal execution beyond the loop. In the example below, we stop the loop when an even element is found.
The Continue Statement The
statement is used to skip the rest of the current iteration. When it is found during the execution of the loop, the current iteration stops and a new one begins with the updated value of the loop variable. In the example below, we skip the current iteration if the element is even and we only print the value if the element is odd:
The zip() function in Python
is an amazing built-in function that we can use in Python to iterate over multiple sequences at once, getting their corresponding elements in each iteration. We just need to pass the sequences as arguments to the
function and use this result in the loop. For example:
The enumerate() Function in Python You can also keep track of a counter while the loop runs with the
function. It is commonly used to iterate over a sequence and get the corresponding index. For example:
If you start the counter from
, you can use the index and the current value in the same iteration to modify the sequence:
You can start the counter from a different number by passing a second argument to
:
The else Clause For loops also have an
clause. You can add this clause to the loop if you want to run a specific block of code when the loop completes all its iterations without finding the
statement. In the example below, we try to find an element greater than 6 in the list. That element is not found, so
doesn’t run and the
clause runs.
The output is:
However, if the
statement runs, the
clause doesn’t run. We can see this in the example below:
The output is:
S.Schins BASICS
Python Basics Examples