PDF download Download Article
Learn how to create a loop using Python
PDF download 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.
3

Write a while loop.

PDF download Download Article

Community Q&A

Search
Add New Question
  • Question
    How do you do a nested loop?
    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?
    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?
    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
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Learn a Programming LanguageLearn a Programming Language
Use Windows Command Prompt to Run a Python FileUse Windows Command Prompt to Run a Python File
Update Pip Upgrade Pip: Quick Guide for Beginners & Experts
Open a Python FileOpen a Python File
Comment Out Multiple Lines in Python Comment Out Multiple Lines in Python: Formatting & Shortcuts
Pip Uninstall Package Uninstall a Python Package With Pip: Step-by-Step Guide
Install Pip on Mac3 Easy Ways to Download & Install Pip on Your Mac
Change the Font Size in Python ShellChange the Font Size in Python Shell
Start Programming in PythonStart Programming in Python
Check Python Version on PC or Mac See the Python Version on Mac, Windows, and Linux
Program a Game in Python with PygameProgram a Game in Python with Pygame
Uninstall PythonUninstall Python
Make a Countdown Program in Python Code a Python Countdown Timer: Step-by-Step Guide
Write a Coin Flipping Program on PythonWrite a Coin Flipping Program on Python
Advertisement

About This Article

wikiHow 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 79,025 times.
How helpful is this?
Co-authors: 19
Updated: February 23, 2024
Views: 79,025
Categories: Python
Thanks to all authors for creating a page that has been read 79,025 times.

Is this article up to date?

Advertisement