Responsive Navbar with Google Search
Introduction to Python: For Loop
Python runs in your browser. Heavy or infinite loops may freeze your browser tab. Use "Stop" if needed; for heavy jobs, run locally.
Program 1

For Loop

Output 1
Program 2

For Loop

Output 2
Program 3

For Loop

Output 3
Program 4

For Loop

Understanding Python's range() Function

range(6): Generates a sequence of integers from 0 to 5 (inclusive of 0 but exclusive of 6).

Iteration with for Loop:

for number in range(6): Iterates over each integer in the sequence.

Print the Current Integer:

print(number): Prints the current integer on each iteration.

Output 4
Program 5

For Loop

range(1, 10, 2): Generates a sequence of integers starting from 1 up to (but not including) 10, with a step of 2. This means it will include every second number starting from 1 to 9.

for number in range(1, 10, 2): Iterates over each integer in the sequence produced by range(1, 10, 2).

Output 5
Program 6

For Loop

Output 6
Program 7

For Loop

Output 7
Program 8

For Loop

Output 8
Program 9

For Loop

Outer Loop (for i in range(1, 4)):

The outer loop goes through numbers \(1\), \(2\), and \(3\). range(1, 4) means "start at \(1\) and go up to (but not including) \(4\)." So, it will produce the numbers \(1\), \(2\), and \(3\).

Inner Loop (for j in range(10, 13)):

For each value of \(i\) from the outer loop, this inner loop goes through numbers \(10\), \(11\), and \(12\). range(10, 13) means "start at \(10\) and go up to (but not including) \(13\)." So, it produces the numbers \(10\), \(11\), and \(12\).

Output of Nested Loops

First Group: When i = 1:

It prints 1 with 10, 11, and 12.

Second Group: When i = 2:

It prints 2 with 10, 11, and 12.

Third Group: When i = 3:

It prints 3 with 10, 11, and 12.

Output 9
Program 10

For Loop

Output 10
Program 11

For Loop

Output 11