Loops In Python
Last updated on 13th Oct 2020, Artciles, Blog
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 |
---|---|
1 | while loopRepeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. |
2 | for loopExecutes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
3 | nested 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.
Syntax
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.
Syntax
- 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.
Syntax
- 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.
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 |
---|---|
1 | break statementTerminates the loop statement and transfers execution to the statement immediately following the loop. |
2 | continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
3 | pass 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- Python Split Method with Example
- Identifiers in Python
- Python Tutorial
- How to Download Python
- Python Anaconda Tutorial
Related Articles
Popular Courses
- Java Training
11025 Learners
- C and C++ Training
12022 Learners
- Ruby Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know