Python Loops

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Python has only two loops: for loop  and while loop

For loop

For loop Syntax:

Note: all the statements inside for  and while loop must be indented to the same number of spaces. Otherwise SyntaxError  will be thrown.

Let’s take an example

Expected Output

here is how for loop works.

In the first iteration i  is assigned value 1  then print statement is executed, In second iteration i  is assigned value  2  then once again print statement is executed. This process continues until there are no more element in the list and for loop exists.

range(a, b) Function

range(a, b)  functions returns sequence of integers from a , a + 1 , a+ 2  …. , b - 2 , b - 1 . For e.g

Expected Output:

You can also use range()  function by supplying only one argument like this:

here range for loop prints number from 0 to 9.

range(a, b)  function has an optional third parameter to specify the step size. For e.g

Expected Output:

While loop


While loop keeps executing statements inside it until condition becomes false. After each iteration condition is checked and if its True then once again statements inside the while loop will be executed.

let’s take an example:

Expected Output:

here while  will keep printing until count  is less than 10.

break statement

break  statement allows to breakout out of the loop.

when count  equals to 5  if condition evaluates to True  and break  keyword breaks out of loop.

Expected Output:

continue statement

When continue  statement encountered in the loop, it ends the current iteration and programs control goes to the end of the loop body.

Expected Output:

As you can see when count % 2 == 0, continue statement is executed which causes the current iteration to end and the control moves on to the next iteration.

In next lesson we will learn about Python mathematical function.

Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

18 thoughts on “Python Loops

  1. Drew Freiberger

    Inconsistency in break example, either count += 1 should be before the conditional or your output should count to 5.

  2. tang

    print(“inside loop”, count)

    print(“out of while loop”)

    I get a syntax error for this break statement for the print out of while loop


    1. Roger

      I get the same syntax error. I’m using the built in python on an iMac:

      Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
      [GCC 4.2.1 (Apple Inc. build 5646)] on darwin

      My code was:

      >>> count = 0
      >>> while count < 10:
      … count = count + 1
      … if count == 5:
      … break
      … print("inside loop", count)
      … print("out of while loop")
      File "”, line 6
      print(“out of while loop”)
      SyntaxError: invalid syntax

      Can you help? Thank you!

      1. admin Post author

        Your code works fine when i tried running it from a file.

        It only shows syntax error inside the console. The reason for this is when you see ... inside console it means that it still hasn’t finished the previous block, to finish it press enter and the write your statement, which in this case is

        print(out of while loop)

  3. djvh

    In your tutorial, this seems incorrect: “here while will keep printing until count is less than 10”. Either change ‘until’ to ‘while’ or change ‘less than 10’ to ‘equal to 10’

  4. Sudheer

    >>> mylist=[1,2,3,4,5,6,7,8,9,10]
    >>> mylist
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> for i in mylist:
    … print(i)
    File “”, line 2
    IndentationError: expected an indented block

  5. Victor

    In the while section, the last line before the break statement section. I think it should read differently. Maybe: “here while will keep printing until count is not less than 10”

  6. John Dolleslager

    I am trying to learn Python and was wondering if it is possible to do something like this from the old BASIC days:

    input “a number”;n
    for x=1 to n
    print n,n^2, n-x
    next x


Leave a Reply

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