Posted on 6/18/2025 5:32:47 PM by Admin

Python Basics: Iterating Through Code with Loop Constructs

In our last lesson, you learned how to empower your Python programs to make smart decisions using conditional statements. What about the times when you need to execute some code repeatedly? The simple answer could be retyping the same code again again the number of times needed. But is it an efficient approach? Plus, what about when you need to make any changes to the code? Wouldn’t it be hectic to go through each line and make changes? Also, it would be super time-consuming. So, in conditions like these, we use loop constructs.

The loop constructs are also the control flow statements that allow you to execute a block of code multiple times based on a certain condition. In this article, we will explore loop constructs in Python.


Loops in Python

Loops, as their name depicts, are a way to execute a block of code repeatedly and efficiently without writing redundant code. They are highly useful in various situations while writing programs. For example, if you want to print numbers from 1 to 100, print multiplication tables of numbers, go through a list of items, etc.

In Python, there are 2 basic types of loops:

  1. ‘for’ loop
  2. ‘while’ loop

The ‘for’ Loop

The ‘for’ loop is used to iterate over a block of code for a known number of times. The number of iterations is known in advance.

Syntax:


for var in range(value):
    Block of code to execute

Just like conditional statements, maintaining proper indentation is crucial in loop constructs.

Iterating Through a Range

The range() function allows you to specify the stop point or beginning or stop point for your loop.

Syntax:


for variable in range(stop):
    Statement(s)

Example:


for i in range(10):
    print(i)

Output:


0
1
2
3
4
5
6
7
8
9

Note that when the lower limit is not defined in the range() function, it starts from 0 by default, and the upper limit is not inclusive.

To print numbers from 1 to 10, the following syntax will be used:

Syntax:


for variable in range(start, stop):
    Statement(s)

Example:


for i in range(1, 11):
    print(i)

Output:


1
2
3
4
5
6
7
8
9
10

If you want to skip numbers using the range() function, we use the following syntax:

Syntax:


for variable in range(start, stop, step):
    Statement(s)

Example:


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

Output:


0
2
4
6
8

Iterating Over Strings

Since a string is a sequence of characters, you can iterate through it character by character as follows:

Syntax:


for chars in string:
    Statement(s)

Example:


for chars in "Hello":
    print(chars)

Output:


H
e
l
l
o

More Practical Examples of Using ‘for’ Loop:

Here are some more practical examples for you to practice more on the ‘for’ loop. Modify these programs by imagining different conditions.

- Adding Numbers in a Range:


sum = 0
for numbers in range(1, 11): # Numbers from 1 to 10
    sum += numbers # Same as: sum = sum + num
print("The sum of numbers from 1 to 10 is: ", sum) # Output: 55

Output:


The sum of numbers from 1 to 10 is:  55

- Printing a Multiplication Table:


number = 2
for i in range(1, 11):  # We need table from 1 to 10.
    print(number, " x ", i, " = ", number*i)  # printing table of 2

Output:


2  x  1  =  2
2  x  2  =  4
2  x  3  =  6
2  x  4  =  8
2  x  5  =  10
2  x  6  =  12
2  x  7  =  14
2  x  8  =  16
2  x  9  =  18
2  x  10  =  20

- Counting Vowels in a String


sentence = "This is a test sentence."
vowels = "aeiouAEIOU"
vowel_count = 0

for char in sentence:  # for every character in the sentence
    if char in vowels: # Checks if the character is vowel
        vowel_count += 1

print("Number of vowels: ", vowel_count)  # Output: 7

Output:


Number of vowels: 7


The ‘while’ Loop

The ‘while’ loop is used to execute a block of code as long as a condition remains True. In this loop, we do not know the number of iterations in advance.


while condition:
    Statement(s)

In the while loop:

  • The loop variable must be declared before the ‘while’ statement.
  • The loop variable must be updated inside the loop body.
  • If the condition never becomes false, an infinite loop is triggered.

Simple Counter

Example:


count = 1
while count <= 5:  # Prints values from 1 to 5.
    print(count)
    count += 1

Output:


1
2
3
4
5

Printing a Series of Odd Numbers

Example:


number = 1
while number <= 10:  # Prints odd numbers
    if number % 2 != 0:
        print(number)
    number += 1

Output:


1
3
5
7
9

Another way to print odd numbers is as follows:


number = 1
while number <= 10:  # Prints odd numbers
    print(number)
    number += 2

The above code generates the same output as the previous program.


Loop Control Statements

Although loops are useful to iterate through a block of code, what if you want to break or continue the flow when a certain condition is met? The answer is loop control statements.

These are the simple statements that control the loop based on the condition. These include break, continue, and pass statements.

‘break’ Statement

The ‘break’ statement terminates the loop execution. The statement immediately following the loop is executed next.

Example:

The following loop stops as soon as 5 is encountered.


for i in range (11):
    print(i)
    if i == 5:
        break;

‘continue’ Statement

The ‘continue’ statement is used to skip an iteration and continue with the next iteration.

Example:

The following loop skips 5 and continues with 6.


for i in range (11):
    if i == 5:
        continue
    print(i)

‘pass’ Statement

The ‘pass’ statement does nothing. It is used in situations when you do not want to perform any action if a condition is met. It is useful when you are structuring the code and plan to fill in the logic later.

Example:

The following code does nothing:


for char in "Statement":
    if char == 'e':
        pass # Do nothing if the character is 'e'
    else:
        print(char)

You have now mastered another essential concept in Python programming: loops! You’ve learned how to use the ‘for’ loop to iterate over sequences and ranges when the number of iterations is known. You also learned a ‘while’ loop that allows you to repeat a code unless a condition does not turn to false. Finally, we learned how to exit a loop with a ‘break’ statement, skip an iteration with a ‘continue’, and leave a block empty with a ‘pass’ statement. These loop constructs are highly crucial for writing truly dynamic programs. In the next article, we will get started with data structures in Python.


Sharpen Your Skills with These Next Guides