Sitemap
Yashaswini Suryawanshi

Yashaswini Suryawanshi

Computer Engineer

Python From Scratch…

4 min readSep 16, 2020

Chapter-6:

In this post we will be looking deep into loops in python.

what is loop?

A loop is a programming structure that repeats a sequence of instructions until a specific condition is met.

Get Yashaswini Suryawanshi’s stories in your inbox

Join Medium for free to get updates from this writer.

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 −

loop

Here, we will discuss 4 types of Python Loop:

Loops in Python

1)Python For Loop:

Python for loop can iterate over a sequence of items.A for loop is a programming concept that, when it’s implemented, executes a piece of code over and over again “for” a certain number of times, based on a sequence.The structure of a for loop in Python is different than that in C++ or Java.Here In Python, we use the ‘in’ keyword.

For Loop
For loop example

Here in example range() function is used ,This function yields a sequence of numbers. When called with one argument,lets say n, it creates a sequence of numbers from 0 to n-1.

2)Python While Loop:

A while loop in python iterates till its condition becomes False. In other words, it executes the statements under itself while the condition it takes is True.

Python while loop

When the program control reaches the while loop, the condition is checked. If the condition is true, the block of code under it is executed.This continues until the condition becomes false. Then, a statement, if any, after the loop is executed.

while loop example

Be careful while using a while loop. Because if you forget to increment or decrement the counter variable in python, or write flawed logic, the condition may never become false. In such a case, the loop will run infinitely, and the conditions after the loop will starve. To stop execution, please press Ctrl+C.

3)Python Loop Control Statements:

Sometimes, you may want to break out of normal execution in a loop. For this, we have three keywords in Python- break, continue, and pass.

Press enter or click to view image in full size
control statements in python

a. break statement

When you put a break statement in the body of a loop, the loop stops executing, and control shifts to the first statement outside it. You can put it in a for or while loop.

break statement

b.continue statement:

When the program control reaches the continue statement, it skips the statements after ‘continue’. It then shifts to the next item in the sequence and executes the block of code for it. You can use it with both for and while loops.

continue statement

c.pass statement:

In Python, we use the pass statement to implement stubs. When we need a particular loop, class, or function in our program, but don’t know what goes in it, we place the pass statement in it. It is a null statement. The interpreter does not ignore it, but it performs a no-operation.

pass statement

4)Nested For Loop in Python:

You can also nest a loop inside another. You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while. Or you can put a loop inside a loop inside a loop. You can go as far as you want.

SUMMARY:In a post we have covered for ,while,nested for and loop control statements with examples.We also learnt how to nest loops, and use the range() function to iterate through a sequence of items.

In next post we will be looking into switch case statement…so stay tuned…!

--

--