Home > Python > Introduction to Python

Ch. 1.5 For Loop

Chapters Index
Program 1

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 2

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 3

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 4

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

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.

Program 5

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

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).

Program 6

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 7

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 8

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 9

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

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.

Program 10

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.
Program 11

For Loop

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.