lettura simple

Python for Loops Statement

The 'for' loop in Python is an elegant mechanism to traverse a sequence or any iterable object such as lists, dictionaries, sets, strings, and so on.

Here's how you'd write a typical 'for' loop:

  1. for item in sequence:
  2. # code to execute for each element

Here, 'item' is the variable that takes on the value of each element in the sequence, one at a time.

The 'sequence', of course, is the iterable that you wish to iterate over.

Keep in mind that in Python, indentation is crucial. Each line of code that falls under the 'for' loop, and is to be executed during each iteration, must be indented. If you're new to this concept, take a moment to learn about Python's indentation rules before moving on.

Here's a Basic Example

Consider a list of numbers. Here's how you could print out each number using a 'for' loop:

  1. list = [1, 2, 3, 4, 5]
  2. for number in list:
  3. print(number)

Running this code will display

1
2
3
4
5

Working with the range() Function

The range() function can be invaluable when using a 'for' loop, as it allows you to generate a sequence of numbers.

For instance, to execute a loop five times, you could use range(5) as follows:

  1. for i in range(5):
  2. print(i)

The result will be the numbers 0 through 4:

0
1
2
3
4

Note that range(5) produces numbers from 0 to 4, not up to 5, because range() starts at 0 by default and stops one number short of the value provided.

The 'continue' Statement

The 'continue' statement instructs Python to skip the rest of the code in the current loop iteration and move on to the next one.

Here's how it works:

  1. for i in range(5):
  2. if i == 3:
  3. continue
  4. print(i)

This 'for' loop goes through numbers from 0 to 4.

However, when the loop reaches 3, the 'continue' statement is triggered, causing Python to skip the print(i) statement for that iteration.

So the output will be:

0
1
2
4

The number 3 is absent from the output because the 'continue' statement caused Python to bypass the print(i) statement when 'i' was equal to 3.

The 'break' Statement

The 'break' statement is used to terminate the current loop and move the execution to the next statement outside the loop.

Here's an example:

  1. for i in range(5):
  2. if i == 3:
  3. break
  4. print(i)

In this case, the loop is set to iterate from 0 to 4.

But it terminates early when 'i' reaches 3, due to the 'break' statement. Hence, the output is:

0
1
2

Despite the original range of 0 to 4, the loop ends when 'i' hits 3.

The 'else' Clause

An 'else' clause can be included in a 'for' loop.

The 'else' block is executed once, just after the 'for' loop exhausts the iteration over the sequence. Here's how it works:

  1. for i in range(5):
  2. print(i)
  3. else:
  4. print("Loop has ended")

This code prints the numbers from 0 to 4 and then the message "Loop has ended".

0
1
2
3
4
Loop has ended

Notice that if a 'break' statement is used to exit the loop prematurely, the 'else' clause won't be executed. For example:

  1. for i in range(5):
  2. if i == 3:
  3. break
  4. print(i)
  5. else:
  6. print("Loop has ended")

Here, the 'break' statement interrupts the loop at 'i' equals 3.

0
1
2

In this case, the 'else' clause is skipped, so "Loop has ended" is not printed.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin