Logo

CHiPSET

A Technical Community

Python Loops Explained | For Loop and While Loop | AIML EP-4

Learn Python loops in an easy and beginner-friendly way. This tutorial explains for loops, while loops, range(), break, continue, nested loops, syntax, examples, and outputs with simple explanations. Perfect for Python and AIML beginners.

ChakradharMay 24, 202630 min
#python loops
Python Loops Explained | For Loop and While Loop | AIML EP-4

Loops in Python

Loops are used to execute a block of code repeatedly until a condition is met or all items in a sequence are processed. They help reduce repetitive code and make programs more efficient.

The main types of loops in Python are:

  • For Loop
  • While Loop
  • For Loop

    A for loop is used to iterate over a sequence such as a list, tuple, string, or range. It executes a block of code once for each item in the sequence.

    Syntax

    Image

    Image

    Understanding range()

    Before learning loops, it is important to understand the range() function because it is commonly used with loops.

    The range() function generates a sequence of numbers.

    Example

    print(range(5))

    Image

    Image

    Python starts counting from 0, so the ending value is not included.

    Different Ways to Use range():

    Using Only Stop Value:

    Image

    Image

    However in real life you may not notice this instead your interpreter will show this

    Image

    Image

    but for understanding purpose i will be showing how the sequence would be

    Using Start and Stop Values

    Image

    Image

    Using Start, Stop and Step Values

    Image

    Image

    A for loop is used to iterate over a sequence such as a list, tuple, string, or range. It executes a block of code once for each item in the sequence.

    Image

    Image

    Explanation

  • n = 4 stores the ending value for the loop.
  • range(0, n) generates numbers from 0 to 3.
  • for i in range(0, n) iterates through each number.
  • print(i) prints the value of i.
  • Below is the flowchart of For loop:

    Image

    Image

    Iterating by Index of Sequences

    A for loop can also access elements using their index values with the help of range() and len().

    Image

    Image

    here a is list right now just know how it works ,you will later come to know about lists ,as of now know that lists are sequence of elements in python

    Explanation

  • len(a) returns the total number of elements in the list.
  • range(len(a)) generates index values from 0 to 2.
  • a[idx] accesses list elements using their index.
  • While Loop

    A while loop repeatedly executes a block of code as long as the given condition remains true. When the condition becomes false, the loop stops.

    Syntax:

    Image

    Image

    Example:

    Image

    Image

    Explanation

  • cnt = 0 initializes the counter variable.
  • while (cnt < 3) checks the condition.
  • cnt = cnt + 1 increases the counter value.
  • The loop runs until the condition becomes false.
  • Infinite While Loop

    An infinite while loop runs continuously because its condition always remains true.

    Example

    Image

    Image

    Explanation

  • while(True) creates a condition that is always true.
  • "Hello Geek" keeps printing forever.
  • Note: Infinite loops should generally be avoided because they never stop automatically.

    if you want to try you can at your own risk ,because once it starts it never concludes unless you manually interrupt or your system prunes the process

    Nested Loops

    A nested loop is a loop inside another loop. The inner loop executes completely for every iteration of the outer loop.

    Example

    Image

    Image

    Explanation

  • The outer loop controls the number of rows.
  • The inner loop runs i times for each row.
  • print(i, end=' ') prints values on the same line.
  • print() moves the cursor to the next line.
  • Image

    Image

    Break Statement

    The break statement is used to immediately stop the loop even if the loop condition is still true.

    Image

    Image

    Explanation

  • The loop starts printing numbers from 0.
  • When i becomes 5, the break statement stops the loop immediately.
  • So numbers after 4 are not printed.
  • Continue Statement

    The continue statement skips the current iteration and moves to the next iteration of the loop.

    Image

    Image

    Explanation

  • The loop runs from 0 to 4.
  • When i becomes 2, continue skips that iteration.
  • Therefore, 2 is not printed.
  • Conclusion

    Loops are one of the most important concepts in Python programming. They help automate repetitive tasks and make coding easier.

  • Use for loops for sequences and ranges.
  • Use while loops for condition-based repetition.
  • Use break to stop a loop.
  • Use continue to skip an iteration.
  • Use nested loops for patterns and repeated structures.
  • Practicing loops regularly improves programming and problem-solving skills.

    homework:

    While loop in Python | Practice | GeeksforGeeks

    Loops | HackerRank

    Write a Python program using a for loop to print all even numbers from 1 to 20.

    Write a Python program using a while loop to find the sum of numbers from 1 to 10.

    solve the problems and send answers through our instagram page

    https://www.instagram.com/chipsetsrmrmp/

    or

    through our x or LinkedIn:

    Profile / X

    (2) CHiPSET: Overview | LinkedIn

    thankyou stay tuned for next episode

    Tags:#python loops

    Written by Chakradhar on May 24, 2026