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...
Step-by-Step Guide
-
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. -
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, , -
Step 3: you need a for loop.
-
Step 4: If you need something to loop forever
-
Step 5: or until a condition is met
-
Step 6: you need a while loop.
-
Step 7: This will loop forever
-
Step 8: or until the program ends.
-
Step 9: If you need to stop a loop
-
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
Helen Evans
Professional writer focused on creating easy-to-follow crafts tutorials.
Rate This Guide
How helpful was this guide? Click to rate: