Python For Loops (original) (raw)

Last Updated : 10 Dec, 2024

Try it on GfG Practice redirect icon

**Python **For Loops are used for iterating over a sequence like lists, tuples, strings, and ranges.

**For Loop Example:

Python `

s = ["Geeks", "for", "Geeks"]

using for loop with string

for i in s: print(i)

`

Table of Content

Flowchart of Python For Loop

For Loops in Python

For Loop flowchart

Python For Loop Syntax

for var in iterable:

# statements

pass

**Note: In Python, for loops **only implement the **collection-based iteration.

Python For Loop with String

This code uses a for loop to iterate over a string and print each character on a new line. The loop assigns each character to the variable i and continues until all characters in the string have been processed.

Python `

s = "Geeks" for i in s: print(i)

`

Using range() with For Loop

The range() function is commonly used with for loops to generate a sequence of numbers. It can take one, two, or three arguments:

for i in range(0, 10, 2): print(i)

`

Control Statements with For Loop

Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Continue with For Loop

Python continue Statement returns the control to the beginning of the loop.

Python `

Prints all letters except 'e' and 's'

for i in 'geeksforgeeks':

if i == 'e' or i == 's':
    continue
print(i)

`

Break with For Loop

Python break statement brings control out of the loop.

Python `

for i in 'geeksforgeeks':

# break the loop as soon it sees 'e'
# or 's'
if i == 'e' or i == 's':
    break

print(i)

`

Pass Statement with For Loop

The pass statement to write empty loops. Pass is also used for empty control statements, functions, and classes.

Python `

An empty loop

for i in 'geeksforgeeks': pass print(i)

`

Else Statement with For Loops

Python also allows us to use the else condition for loops. The else block just after for/while is executed only when the loop is NOT terminated by a break statement.

Python `

for i in range(1, 4): print(i) else: # Executed because no break in for print("No Break\n")

`

Using Enumerate with for loop

In Python, enumerate() function is used with the for loop to iterate over an iterable while also keeping track of index of each item.

Python `

li = ["eat", "sleep", "repeat"]

for i, j in enumerate(li): print (i, j)

`

Output

0 eat 1 sleep 2 repeat

Nested For Loops in Python

This code uses nested for loops to iterate over two ranges of numbers (1 to 3 inclusive) and prints the value of i and j for each combination of these two loops. \

The inner loop is executed for each value of i in outer loop. The output of this code will print the numbers from 1 to 3 three times, as each value of i is combined with each value of j.

Python `

for i in range(1, 4): for j in range(1, 4): print(i, j)

`

Output

1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3

Python For Loop **Exercise Questions

Below are two Exercise Questions on Python for-loops. We have covered continue statement and range() function in these exercise questions.

**Q1. Code to implement Continue statement in for-loop

Python `

a = ["shirt", "sock", "pants", "sock", "towel"] b = [] for i in a: if i == "sock": continue else: print(f"Washing {i}") b.append("socks") print(f"Washing {b}")

`

Output

Washing shirt Washing pants Washing towel Washing ['socks']

**Q2. Code to implement range function in for-loop

Python `

for i in range(1, 8): d = 3 + (i - 1) * 0.5 print(f"Day {i}: Run {d:.1f} miles")

`

Output

Day 1: Run 3.0 miles Day 2: Run 3.5 miles Day 3: Run 4.0 miles Day 4: Run 4.5 miles Day 5: Run 5.0 miles Day 6: Run 5.5 miles Day 7: Run 6.0 miles

Test Your Knowledge - **Python For Loop Quiz