Loops In Python

Loops In Python

Last updated on 13th Oct 2020, Artciles, Blog

About author

Vinoth (Sr Technical Project Manager - Python )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 12547 Ratings 2245

Loops in Python

In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement −


Python programming language provides the following types of loops to handle looping requirements.

Subscribe For Free Demo

Error: Contact form not found.

S.No.Loop Type & Description
1while loopRepeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
2for loopExecutes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
3nested loopsYou can use one or more loops inside any another while, for or do..while loop.

While Loop

A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.


The syntax of a while loop in Python programming language is −

  • while expression:
  •    statement(s)
  • Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
  • When the condition becomes false, program control passes to the line immediately following the loop.

For Loop

It has the ability to iterate over the items of any sequence, such as a list or a string.


  • for iterating_var in sequence:
  •    statements(s)
  • If a sequence contains an expression list, it is evaluated first. 
  • Then, the first item in the sequence is assigned to the iterating variable iterating_var. 
  • Next, the statements block is executed. 
  • Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

Flow Diagram


Nested Loop

The Python programming language allows one loop inside another loop. Following section shows a few examples to illustrate the concept.


  • for iterating_var in sequence:
  •    for iterating_var in sequence:
  •       statements(s)
  •    statements(s)

The syntax for a nested while loop statement in Python programming language is as follows −

  • while expression:
  •    while expression:
  •       statement(s)
  •    statement(s)
  • A final note on loop nesting is that you can put any type of loop inside of any other type of loop. 
  • For example a for loop can be inside a while loop or vice versa.
Python Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

Python supports the following control statements. Click the following links to check their detail.

Let us go through the loop control statements briefly

S.No.Control Statement & Description
1break statementTerminates the loop statement and transfers execution to the statement immediately following the loop.
2continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
3pass statementThe pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

Are you looking training with Right Jobs?

Contact Us

Popular Courses