Python

 

Introduction

In Python, loops are used to iterate over a sequence (like a list, tuple, or string) or execute a block of code repeatedly. The two main types of loops in Python are the for loop and the while loop. Understanding how to use these loops is crucial for controlling the flow of your program and handling repetitive tasks efficiently.

Objective

In this tutorial, you will learn the following:

  • How to use a for loop to iterate over a sequence of items.
  • How to use a while loop to repeat a block of code until a condition is met.
  • How to control loop execution with break, continue, and else statements.

Code Examples

For Loop Example

# For loop to iterate through a list of numbers
numbers = [1, 2, 3, 4, 5]
for number in numbers:
    print(number)

While Loop Example

# While loop to print numbers from 1 to 5
counter = 1
while counter <= 5:
    print(counter)
    counter += 1

Program Explanation

The two loops serve different purposes:

  • For Loop: The for loop iterates over each item in a sequence (in this case, a list of numbers). The loop starts by assigning the first item in the list to the variable number and executes the indented block of code. This continues until all items in the list have been processed.
  • While Loop: The while loop continues to execute the indented block of code as long as the given condition is true. In this example, it prints the value of counter until counter exceeds 5. Each time the loop runs, counter is incremented by 1 using counter += 1.

How to Run the Program

To run the provided Python program:

  1. Open your Python editor or IDE (like PyCharm, Visual Studio Code, or even a simple text editor with Python support).
  2. Copy and paste the provided code into a new Python file (e.g., loops.py).
  3. Save the file and run it using the Python interpreter by typing python loops.py in the command line or running the file directly from your IDE.
  4. You should see the numbers 1 through 5 printed in the terminal or output window for both loops.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

2 thoughts on “Python Loops: For and While Loops”
  1. I visited multiple blogs except the audio feature for
    audio songs present at this website is genuinely wonderful.

    https://www.motors-addict.com/fr/article/general/pfaff-motorsports-passe-a-lamborghini-pour-2025/67600126e40232eace00fb8b
    I could not refrain from commenting. Exceptionally well written!
    https://www.elprogreso.es/articulo/comunicados/bingo-online-vs-casino-online-cual-es-mejor/202306151041301673597.html
    Undeniably believe that which you said. Your favorite justification seemed to be on the
    web the easiest thing to be aware of. I say to you, I certainly
    get irked while people consider worries that they plainly do not know
    about. You managed to hit the nail upon the top as
    well as defined out the whole thing without
    having side effect , people can take a signal.
    Will likely be back to get more. Thanks
    https://www.footballdatabase.eu/fr/article/-/2943-ces_joueurs_reveles_lors_des_precedentes_coupes_du_monde_des_17_ans
    Your means of describing everything in this piece of writing is in fact nice, all be able to easily be aware of it, Thanks a lot.

    https://dicodusport.fr/blog/sport-et-jeux-en-ligne-une-nouvelle-dimension-du-divertissement/
    When someone writes an paragraph he/she maintains the idea of a user
    in his/her brain that how a user can understand it. So that’s why this piece of writing is outstdanding.
    Thanks!
    ss
    Hola! I’ve been reading your site for a long time now and finally got the
    courage to go ahead and give you a shout out from
    New Caney Tx! Just wanted to mention keep up the good job!

    https://www.doingbuzz.com/5-criteres-pour-choisir-votre-casino-en-ligne/
    Great article. I’m going through a few of these issues as well..

    https://www.playpilot.com/fr/blog/les-nouveaux-jeux-de-casino-inspires-par-la-cultur/
    Hello everyone, it’s my first pay a quick visit at this website, and post is in fact
    fruitful for me, keep up posting these content.
    https://www.directmag.com/uncategorized-fr/info9998/les-canadiens-et-leurs-loisirs-comment-les-activites-de-detente-faconnent-lidentite-canadienne.html
    I was suggested this web site by my cousin. I’m now not positive whether or not this publish is written by him as
    no one else recognize such specific approximately my trouble.
    You’re incredible! Thanks!
    ss
    We are a group of volunteers and opening a new scheme in our community.
    Your website provided us with valuable info to work
    on. You’ve done an impressive job and our whole community
    will be thankful to you.
    casino en ligne

  2. Nicely put, Thanks a lot!
    casino en ligne
    Reliable data, With thanks!
    casino en ligne
    Wonderful posts Kudos!
    casino en ligne
    Superb posts, Thanks a lot!
    casino en ligne
    Thanks a lot. Valuable stuff!
    casino en ligne fiable
    Thanks a lot. Wonderful stuff.
    casino en ligne fiable
    Cheers, I like this.
    casino en ligne
    Seriously loads of valuable advice!
    casino en ligne
    With thanks. Plenty of advice!
    casino en ligne fiable
    Incredible a good deal of helpful material.
    casino en ligne

Leave a Reply to casino en ligne Cancel reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)