Python For Loop

Python For Loop

for Loops are used to iterate over sequences (i.e., lists, tuples, dictionaries, sets, or strings).

This is different from for Keywords are not very similar, but more like iterator methods in other object-oriented programming languages.

By using for Loops, we can execute a group of statements for each item in a list, tuple, set, etc.

Example

Print each fruit in the fruits list:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

Run Example

Tip:for Loops do not require an index variable to be set in advance.

Loop through the string

Even strings are iterable objects that contain a series of characters:

Example

Loop through the letters in the word "banana":

for x in "banana":
  print(x)

Run Example

the break statement

By using break Statement, we can stop the loop before traversing all items:

Example

If x is "banana", exit the loop:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x) 
  if x == "banana":
    break

Run Example

Example

When x is "banana", exit the loop, but this time interrupt before printing:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    break
  print(x)

Run Example

the continue statement

By using continue Statement, we can stop the current iteration of the loop and continue to the next:

Example

Not to print banana:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

Run Example

the range() function

To repeat a block of code a specified number of times, we can use range() Function,

range() The function returns a sequence of numbers, starting from 0 by default and incrementing by 1 (by default), and ending with the specified number.

Example

Using range() Function:

for x in range(10):
  print(x)

Run Example

Note:range(10) The value is not from 0 to 10, but from 0 to 9.

range() The function defaults to 0 as the starting value, but you can specify a starting value by adding a parameter:range(3, 10), which means the value ranges from 3 to 10 (but does not include 10):

Example

Using the starting parameter:

for x in range(3, 10):
  print(x)

Run Example

range() The function defaults to incrementing the sequence by 1, but an increment value can be specified by adding a third parameter:range(2, 30, 3):

Example

Use an increment sequence of 3 (the default value is 1):

for x in range(3, 50, 6):
  print(x)

Run Example

Else in For Loop

in for loop else The keyword specifies the code block to be executed when the loop ends:

Example

Print all numbers from 0 to 9 and print a message at the end of the loop:

for x in range(10):
  print(x)
else:
  print("Finally finished!")

Run Example

Nested Loops

Nested loops are loops within loops.

The 'outer loop' iterates once, and the 'inner loop' will execute once:

Example

Print each adjective of each fruit:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
  for y in fruits:
    print(x, y)

Run Example

Pass Statement

For statement cannot be empty, but if you have written an empty for statement for some reason, please use the pass statement to avoid errors.

Example

for x in [0, 1, 2]:
  pass

Run Example