csaccept.com is a computer awareness website dedicated to providing reliable and easy-to-understand information about computer technology and digital safety. The website focuses on educating students, beginners, and general users about computer basics, cyber security, emerging technologies, and practical IT skills. Through informative articles, quizzes, and real-life examples, csaccept.com aims to increase digital literacy and help users stay safe and confident in today’s technology-driven world.
 Facebook   ………………..      Instagram   ……………..      Twitter ( X )      ……………..     YouTube


Table of Contents

Loops in Python: While Loop, For Loop, Nested Loop and Pass Statement

Python provides several types of loops to handle different looping requirements in a program. Loops allow a block of code to be executed repeatedly as long as a specific condition is satisfied or for each element in a sequence. They reduce code repetition, improve readability, and make programs more efficient.

Python mainly provides the following types of loops:

  1. While Loop
  2. For Loop
  3. Nested Loops

Additionally, Python provides a special statement called the pass statement, which is often used with loops, functions, and classes.


1. While Loop in Python

The while loop is used to repeatedly execute a block of code as long as a given condition remains true. The condition is checked before every iteration. If the condition becomes false, the loop stops executing.

Syntax:

while condition:
    # code block

How it Works:

  • The condition is evaluated first.
  • If the condition is True, the loop body is executed.
  • After execution, the condition is checked again.
  • This process continues until the condition becomes False.

Example:

count = 0
while count < 5:
    print(count)
    count += 1

Output:

0
1
2
3
4

Explanation:

  • Initially, count is 0.
  • The condition count < 5 is true, so the loop executes.
  • The value of count is printed and then incremented.
  • When count becomes 5, the condition becomes false and the loop terminates.

2. For Loop in Python

The for loop is used to iterate over a sequence such as a list, tuple, string, or range. It automatically handles the loop variable and makes iteration simpler and cleaner.

Syntax:

for item in sequence:
    # code block

Here, item is the loop variable that takes the value of each element in the sequence one by one.


Example 1: Iterating Over a List

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

Output:

apple
banana
cherry

Explanation:

  • The loop iterates through each element of the list.
  • The variable fruit stores one item at a time.
  • Each fruit name is printed on a new line.

Example 2: Iterating Using range()

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

Output:

1
2
3
4

Explanation:

  • The range(1, 5) function generates numbers from 1 to 4.
  • The loop prints each number one by one.

3. For Loop Using range() Function

The range() function is commonly used with the for loop to generate a sequence of numbers.

Syntax:

range(start, stop, step)

Parameters:

  • start – starting value (inclusive)
  • stop – ending value (exclusive)
  • step – difference between consecutive values

Example 1: Basic Range

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

Output:

0
1
2
3
4

Explanation:

  • The range starts from 0 by default.
  • It stops at 4 because the stop value is exclusive.

Example 2: Range with Step Value

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

Output:

1
3
5
7
9

Explanation:

  • The loop starts from 1 and increases by 2 each time.
  • Only odd numbers are printed.

4. Nested For Loop in Python

A nested loop is a loop inside another loop. The inner loop executes completely for every single iteration of the outer loop. Nested loops are commonly used for working with matrices, patterns, and multiplication tables.

Syntax:

for outer_variable in outer_sequence:
    for inner_variable in inner_sequence:
        # inner loop code

Example: Multiplication Table Using Nested Loop

for i in range(1, 6):
    for j in range(1, 6):
        print(i * j, end=" ")
    print()

Output:

1  2  3  4  5
2  4  6  8  10
3  6  9  12  15
4  9  12  16  20
5  10  15  20  25

Explanation:

  • The outer loop runs from 1 to 5.
  • For each value of i, the inner loop runs from 1 to 5.
  • The current values of i and j are multiplied.
  • The result is printed in matrix format.

5. Pass Statement in Python

The pass statement is a null statement in Python. It does nothing when executed and is used as a placeholder where a statement is syntactically required but no action is needed.

Why Use Pass Statement?

  • To maintain code structure
  • To write empty functions, classes, or loops
  • To allow future implementation
  • To avoid syntax errors during development

Example: Using Pass Statement

def my_function():
    pass

if x < 10:
    pass

class MyClass:
    pass

Explanation:

  • The function, condition, and class are defined but contain no logic.
  • The program runs without errors.
  • Code can be added later when needed.

Advantages of Using Loops in Python

  • Reduces code repetition
  • Improves program efficiency
  • Enhances readability
  • Makes programs dynamic and scalable

Conclusion

Loops are a fundamental concept in Python programming. The while loop is condition-based, the for loop is sequence-based, and nested loops allow multiple levels of iteration. The pass statement helps maintain code structure during development. Understanding these concepts is essential for writing efficient and professional Python programs.


Short Question–Answer (Python Loops)

Q1. What is a loop in Python?
A loop is used to execute a block of code repeatedly.

Q2. Name the loops available in Python.
While loop and For loop.

Q3. What is a while loop?
A while loop executes code as long as a condition is true.

Q4. What is a for loop?
A for loop iterates over a sequence such as list, string, or range.

Q5. What is a nested loop?
A loop inside another loop is called a nested loop.

Q6. What is the range() function?
It generates a sequence of numbers.

Q7. Is the stop value included in range()?
No, the stop value is excluded.

Q8. What is the pass statement?
It is a null statement that performs no action.

Q9. Why is pass used in Python?
To maintain code structure without implementation.

Q10. Where can pass be used?
In loops, functions, classes, and conditional blocks.


MCQ Quiz –

1. Which loop runs while a condition is true?

a) for
b) do-while
c) if
d) while
Answer: d

2. Which loop is used to iterate over a list?

a) while
b) for
c) if
d) pass
Answer: b

3. What does range(5) return?

a) 1 to 5
b) 1 to 4
c) 0 to 4
d) 0 to 5
Answer: c

4. Which statement does nothing?

a) break
b) continue
c) return
d) pass
Answer: d

5. How many times will this loop execute?

for i in range(1,6):
a) 4
b) 5
c) 6
d) Infinite
Answer: b

6. What is a nested loop?

a) Loop without condition
b) Loop with error
c) Loop inside another loop
d) Infinite loop
Answer: c

7. Which keyword exits a loop immediately?

a) continue
b) stop
c) break
d) pass
Answer: c

8. Which loop checks condition before execution?

a) for
b) while
c) nested
d) none
Answer: b

9. Default start value of range() is:

a) 1
b) -1
c) 0
d) None
Answer: c

10. Which symbol is used to increase value?

a) ++
b) add
c) +=
d) =+
Answer: c

11. Which loop is best when number of iterations is unknown?

a) for
b) while
c) nested
d) pass
Answer: b

12. What will range(1,10,2) generate?

a) Even numbers
b) All numbers
c) Odd numbers
d) Prime numbers
Answer: c

13. Which keyword skips the current iteration?

a) break
b) continue
c) stop
d) pass
Answer: b

14. for loop works on:

a) Condition
b) Boolean
c) Sequence
d) Function
Answer: c

15. Can pass be used in an empty function?

a) No
b) Error
c) Yes
d) Sometimes
Answer: c

16. Which loop is used for multiplication tables?

a) while
b) for
c) nested for
d) pass
Answer: c

17. Which loop is easier for lists?

a) while
b) for
c) nested
d) none
Answer: b

18. Infinite loop occurs when:

a) Condition is false
b) Loop ends
c) Condition never becomes false
d) pass used
Answer: c

19. Which symbol is required after loop statement?

a) ;
b) .
c) :
d) ,
Answer: c

20. Which is not a loop?

a) for
b) while
c) nested
d) if
Answer: d