0% found this document useful (0 votes)
28 views

For Loop & Nested Loops

For loops in Python allow you to iterate over a sequence like a list or range of numbers. A for loop takes the form: for element in iterable: # code block Nested loops involve placing one loop inside another. An outer loop iterates over rows, while an inner loop handles columns. This allows processing of tables with multiple iterations. For example, to print a multiplication table, the outer loop could iterate over numbers 1 to 10 for the rows. The inner loop would then iterate 1 to 4 times to print the columns for each row. Nested loops are useful when complex iterations over multidimensional datasets are required.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

For Loop & Nested Loops

For loops in Python allow you to iterate over a sequence like a list or range of numbers. A for loop takes the form: for element in iterable: # code block Nested loops involve placing one loop inside another. An outer loop iterates over rows, while an inner loop handles columns. This allows processing of tables with multiple iterations. For example, to print a multiplication table, the outer loop could iterate over numbers 1 to 10 for the rows. The inner loop would then iterate 1 to 4 times to print the columns for each row. Nested loops are useful when complex iterations over multidimensional datasets are required.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

For Loop & Nested Loop

1. The For Loop

A for loop in Python is a control flow statement that is used to iterate over a sequence (such as a
list, tuple, string, or range) or other iterable objects. It allows you to execute a block of code
multiple times with each iteration. The basic syntax of a for loop in Python is as follows:

for element in iterable:


# Code to be executed for each element

✔ element: This is a variable that takes on the value of each item in the iterable one by

one during each iteration of the loop.

✔ iterable: The sequence or iterable object you want to loop through.

Here is a typical use of the for loop. We want to print the balance of our savings account over a
period of years, as shown in this table:

The for loop pattern applies because the variable

year starts at 1 and then moves in constant increments until it


reaches the target:
for year in range(1, numYears + 1) :
Update balance
Print year and balance

Example 1: Looping Through a List


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

Output:

apple
banana
cherry

Example 2: Looping Through a Range

You can also use the range function to generate a sequence of numbers and iterate over them.

python
for i in range(5): # This will loop from 0 to 4 (5 times)
print(i)

Output:

0
1
2
3
4

Example 3: break and continue

● You can use the break statement to exit a for loop prematurely.

python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 4:
break
print(num)

Output:

1
2
3

● The continue statement is used to skip the current iteration and move to the next one.
python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue
print(num)

Output:

1
2
4
5

These are the basics of using for loops in Python. They are versatile and commonly used for
various tasks like data processing, list manipulation, and more

2. Multiple Loops & Nested Loops

Similarly, complex iterations sometimes require multiple or nested loops (a loop inside another
loop statement). When processing tables, nested loops occur naturally. An outer loop iterates
over all rows of the table. An inner loop deals with the columns in the current row.

In this section you will see how to print a table. For simplicity, we will print the powers of x, x n,
as in the table at right. Here is the pseudocode for printing the table:

Print table header.


For x from 1 to 10
Print table row.
Print new line.

How do you print a table row? You need to print a value for
each exponent. This requires a second loop.

For n from 1 to 4
Print xn
This loop must be placed inside the preceding loop.

We say that the inner loop is nested inside the outer loop.

There are 10 rows in the outer loop. For each x, the program prints four columns in the inner
loop. Thus, a total of 10 × 4 = 40 values are printed.

In this program, we want to show the results of multiple print statements on the same line. This
is achieved by adding the argument end="" to the print function.

Following is the complete program. Note that we also use loops to print the table header.
However, those loops are not nested.

You might also like