Python For Loops


Python For Loops

A for loop is used for repetition in sequence (i.e. list, tuple, dictionary, set, or character unit).

This is similar to the for keyword in some editing languages, and serves as a repetition method as found in other object-focused editing languages.

With a for loop we can make a set of statements, as well as each item in the list, tuple, set etc.


Example

Print each fruit in a fruit list:

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

The for loop does not require variable indexing to be pre-configured.


Looping Through a String

Even cables are controllable, consisting of a series of letters:


Example

Loop through the letters in the word "banana":

for x in "banana":
  print(x)


The break Statement

With a break statement we can stop the loop before it goes into all things:


Example

Exit the loop when x is "banana":

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


Example

Exit the loop when x is "banana", but this time the break comes before the print:

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


The continue Statement

With an continue statement we can stop the current duplicate of the loop, and proceed to the following:


Example

Do not print banana:

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


The range() Function

To connect to a set of code a certain number of times, we can use the range() function,

The range() function returns the sequence of numbers, from 0 by default, to increments by 1 (by default), and ends at a certain number.


Example

Using the range() function:

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

Note that the range(6) is not a value of 0 to 6, but a value of 0 to 5.


range() function varies by 0 as the first value, however it is possible to specify the first value by adding a parameter: range(2, 6), meaning values ​​from 2 to 6 (but excluding 6):


Example

Using the start parameter:

for x in range(2, 6):
  print(x)

The range() function is automatically adjusted to increase the sequence by 1, however it is possible to specify an additional value by adding a third parameter: range(2, 30, 3):


Example

Increment the sequence with 3 (default is 1):

for x in range(2, 30, 3):
  print(x)


Else in For Loop

else keyword in the for loop specifies the code block to be used when the loop is complete:


Example

Print all numbers from 0 to 5, and print a message when the loop has ended:

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

Note: Else block will NOT be used if the loop is stopped by a break statement.



Example

Break the loop when x is 3, and see what happens with the else block:

for x in range(6):
  if x == 3: break
  print(x)
else:
  print("Finally finished!")


Nested Loops

A loop with a nest is a loop inside a loop.

"Internal loop" will be used simultaneously for each duplicate of "external loop":


Example

Print each adjective for every fruit:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)


The pass Statement

because the for loops cannot be empty, but if for some reason you have a for loop with no content, enter a pass statement to avoid finding error.


Example

Print each adjective for every fruit:

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