Python Learning (Part 4)
In this article we will get to know how we can use Loops in Python and also cover the topic like IF, ELSE and ELIF statement.
Loops in Python :
A loop is a programming function that iterates a statement or condition based on specified boundaries.
A specific statement or a group of instructions is continuously executed until a specific loop body or boundary condition is reached.
Two types of Loop:
1. For Loop
2. While Loop
For Loop in Python
A For Loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly.
For-loops are typically used when the number of iterations is known before entering the loop.
Example of For loop in Python:
Output :
Here by using For loop condition, I got 5 numbers from 0–4 as output shown above.
For loops can iterate over a sequence of numbers using the “range” function.
Nested Loops :
A nested loop is a loop inside a loop.
The inner loop will be executed one time for each iteration of the outer loop.
Get Arjun Sharma’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
The following example will show each act for each stage as shown below
Output:
While Loop in Python
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
Example of While loop in Python:
Output:
IF, IF ELSE and ELIF statement
IF statement in Python :
If statement is a conditional statement, if the statement is true then the action is performed or displays the information.
Example of IF statement in Python:
Output:
IF Else statement in Python :
The Else statement is used to specify block of code to be executed if the condition is false of IF (condition).
Example of IF ELSE in Python:
Output:
ELIF statement in Python :
The ELIF keyword in python says that “if the previous conditions were not true, then try this condition”.
Example:
Output:
Learn Python project oriented approach with below video: -








