Python From Scratch…
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 −
Here, we will discuss 4 types of Python Loop:
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.
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.
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.
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.
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.
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.
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.
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…!









