# Python Loops

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.

15 Comments on "Python Loops"

Notify of

Guest
Sudheer
1 month 15 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

Guest
Sudheer
1 month 15 days ago

Hi Friend,

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

Guest
djvh
7 months 24 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’

Guest
tang
10 months 29 days 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?

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

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

I dont get any result. why is this so?