For loop

19 August 2020

Code

Option # 1: Using in

x = ['tree', 'plant', 'seed', 'grass']

# For loop
for i in x:
    print(i)
tree
plant
seed
grass

.

Option # 2: Using range()

# Create a list
x = ['tree', 'plant', 'seed', 'grass']

# For loop
for i in range(len(x)):
    print(x[i])
tree
plant
seed
grass

.

Option # 3: Nested for loop

# Create a list
x = ['tree', 'plant', 'seed', 'grass']
y = ['This', 'That']

# Nested For loop
for i in x:
    for j in y:
        print(j + ' ' + i)
This tree
That tree
This plant
That plant
This seed
That seed
This grass
That grass






Any errors in code above?
Please send a message.