How to Create Loops in Python

Open up your shell or program., If you need to loop a definite amount of times, you need a for loop., If you need something to loop forever, or until a condition is met, you need a while loop., This will loop forever, or until the program ends., If...

10 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Open up your shell or program.

    This may be IDLE, or Stani's Python Editor (SPE).

    Make sure all subprograms are off if using IDLE.
  2. Step 2: If you need to loop a definite amount of times

    This is the structure for a for loop: for i in range (0, 10): print "Hello World"

    A method for both is shown. while True: print "Hello World"

    (True will always be True). while answer == "Yes" and grade == "6":
    As long as the variables answer and grade are Yes and 6, ,
  3. Step 3: you need a for loop.

  4. Step 4: If you need something to loop forever

  5. Step 5: or until a condition is met

  6. Step 6: you need a while loop.

  7. Step 7: This will loop forever

  8. Step 8: or until the program ends.

  9. Step 9: If you need to stop a loop

  10. Step 10: use Ctrl-C. You can always kill the loop in task manager as well.

Detailed Guide

This may be IDLE, or Stani's Python Editor (SPE).

Make sure all subprograms are off if using IDLE.

This is the structure for a for loop: for i in range (0, 10): print "Hello World"

A method for both is shown. while True: print "Hello World"

(True will always be True). while answer == "Yes" and grade == "6":
As long as the variables answer and grade are Yes and 6, ,

About the Author

H

Helen Evans

Professional writer focused on creating easy-to-follow crafts tutorials.

45 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: