Table of contents

2. Looping (or) Iterative (or) Repetitive Statements

The purpose of looping statements is to perform a specific operation repeatedly for a finite number of times until the test condition becomes false.

In Python programming, there are two types of looping statements:

  1. for loop (or for...else loop)
  2. while loop (or while... else loop)

When working with while loops in Python, ensure the following three components are included.

  • Initialization part
  • Conditional part
  • Updation part (increment or decrement)

The purpose of a for loop is to retrieve or extract data from any iterable object.

for varname in iterable_object: # Block of statements # Other statements in the program
for varname in iterable_object: # Block of statements else: # Else block of statements # Other statements in the program
# Write a program to generate numbers from 1 to 10 num = int(input("enter the number = ")) for var in range(10): print(var)
# Write a program to generate 1 to 20 even number num = int(input("Enter the given number = ")) for var in range(0,num,2): print(var, end = " ")
# Write a program to find given number is prime or not num = int(input("enter the given number = ")) for var in range(2, num): if num % var == 0: print("Not Prime number") else: print("Prime Number")
# Write a program to find the factorial of a given number number = int(input("enter the given number = ")) fact = 1 for i in range(1,number+1): fact = fact*i print(fact)
  • The keywords for, in, and else are used.
  • varname is a programmer-defined variable.
  • An iterable_object is any object containing multiple values, such as a sequence (string, tuple), list, set, or dictionary.
  • The execution process of a for loop involves selecting each element from the iterable_object, assigning it to varname, and executing the block of statements. This process repeats for a finite number of times (equal to the length of the iterable_object) until all elements are processed.
  • When all elements of the iterable_object are processed, the for loop condition becomes false. If an else block is present, its statements are executed, followed by other statements in the program.
  • The else block is optional.

Note 1: For iterating over iterable objects, it is recommended to use a for loop.

Note 2: For repeating non-iterable objects, it is recommended to use a while loop.

# string str = "Hello" for var in str: print(var)

A nested for loop is a for loop inside another for loop, where the inner loop runs completely for each iteration of the outer loop. With strings, nested for loops are used to

  • Compare characters (e.g., finding duplicates).
  • Generate patterns (e.g., printing substrings).
  • Process combinations (e.g., anagrams or string matching).
for var1 in iterable_object1: # Outer loop block for var2 in iterable_object2: # Inner loop block # Optional outer else else: # Optional outer else block # Other statements
# Write a program to generate 1 to 100 even numbers for i in range(2,101): for j in range(2,i): if i%j == 0: break else: print(i,end = " ")