Python Loops

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

Syntax:

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

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!

Leave a Reply

15 Comments on "Python Loops"

Notify of
avatar

Sort by:   newest | oldest | most voted
Sudheer
Guest
Sudheer
5 months 25 days ago

>>>
>>> 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
print(i)
^
IndentationError: expected an indented block

Sudheer
Guest
Sudheer
5 months 25 days ago

Hi Friend,

When i am trying practicing for loop i am getting this error.

Can you please help

djvh
Guest
djvh
1 year 3 days ago

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’

tang
Guest
tang
1 year 3 months ago

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

y?

Roger
Guest
Roger
11 months 25 days ago
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!
Kuruvila M Jacob
Guest
Kuruvila M Jacob
1 year 3 months ago

>>> my_list = [1,2,3,4]
>>> for i in my_list :
… print(i)

I dont get any result. why is this so?

wpDiscuz