Download Article
Learn how to create a loop using Python
Download Article

In Python, and many other programming languages, you will need to loop commands several times, or until a condition is fulfilled. It is easy, and the loop itself only needs a few lines of code.

Things You Should Know

  • If you are using IDLE, make sure all subprograms are off.
  • For a loop that goes for a definite amount of times, you will need a for loop, and for a loop that goes on forever you'll need a while loop.
  • You can use the break command to forcibly stop a loop.
  • Remember the "if", "elif" and "else" words for your loops. They are useful keywords that you can use on your on your loop.
1

Open your shell or program.

Download Article
  1. How.com.vn English: This may be IDLE or any program.
    Make sure all subprograms are off if using IDLE.
  2. Advertisement
2

Write a for loop.

Download Article
  1. How.com.vn English: If you need to loop a definite amount of times, you need a for loop.
    This is the structure for a for loop. The example prints hello, world! 10 times:
>>> for i in range(0, 10): # The first number specifies the start and the last one specifies the end>>>      print("hello, world!")
3

Write a while loop.

Download Article
  1. How.com.vn English: If you need something to loop forever, or until a condition is met, you need a while loop.
    A method for both is shown. That will throw a SyntaxError unless you use Python version 2. It is recommended to use the above example of the print statement.
  2. Advertisement
>>> while True: # This loop goes on forever>>>       print "Hello World" # Prints the message which will not work on recent versions
4

Make the while loop last until a condition is met.

Download Article
  1. How.com.vn English: This will loop forever, or until the program ends.
    In this example, True will always be True as long as the variable's answer and grade are Yes and 6. If they never changed, it will run forever so the while loop just makes the code loop while something is happening.
>>> while answer == "Yes" and grade == "6": # While the answer is equal to the string Yes and the grade to 6
5

Stop a loop.

Download Article
  1. How.com.vn English: If you need to stop a loop, use Ctrl+C.
    You can always kill the loop in Task Manager as well. The other way is using the break command or using while loops.
  2. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How do you do a nested loop?
    How.com.vn English: Adam Blalock
    Adam Blalock
    Community Answer
    Here is a loop within a loop in Python: x = [0,1,2,3,4], y= [10,11,12,13]. For i in x: For ii in y: print i + ii. (Make sure to do it as a single tab before the second "For" and two tabs for the "print" statement.)
  • Question
    What about web development?
    How.com.vn English: Community Answer
    Community Answer
    You need to learn about Django, the Python framework for web development.
  • Question
    How do you use a while loop to repeat a calculator program?
    How.com.vn English: Community Answer
    Community Answer
    First create a function. For example, if you wanted to square numbers 1 through 100 and print the results to the screen, I would write: def square(x): return x*x for i in range(1, 101): # stop = 101 because the stop value is not included. This will go from 1-100. print(square(i)).
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      Tips

      • You can exit a loop prematurely with the break command. This might be needed, for example, if you're iterating over a list to find something and can stop looking once you've found it.
      • You can use the continue command to skip to the top of the loop. This is useful if you wanted to skip certain code if a condition is met.
      • Avoid using the while True loop without any "break" commands or keywords that end it.
      Show More Tips
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 19 people, some anonymous, worked to edit and improve it over time. This article has been viewed 75,293 times.
      How helpful is this?
      Co-authors: 19
      Updated: February 23, 2024
      Views: 75,293
      Categories: Python
      Thanks to all authors for creating a page that has been read 75,293 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement