Table of Contents
A Python loop will repeat over the sequence. The sequence can either be a list, tuple, dictionary, set, or a string. In the Python Programming language, it has two types of loops. Such as For Loop and While Loop. This article has listed about the Loops In Python.
Even though Loop and While Loop will differ in basic functionality and their syntax.
What is While Loop in Python?
A ‘while loop‘ mainly functions to run the block of statements iterate till the condition is completed. However, when the condition becomes false, the line after the loop in the program will begin to run.
While Loop Syntax in Python
The Loop Syntax is provided below
while expression:
statement(s)
The syntax indicates that the statements will be indented by the same number of character spaces even after the programming construct and this will be included in the single block of code. So, Python will use indentation and will be considered as a method of grouping statements.
Example
The code given below will use the ‘while loop’ to print the Skillvertex. This Python code will print the output ie, Skill vertex three times by adding a variable known as ‘count’ from 1 to 3.
count = 0
while (count < 3):
count = count + 1
print("Skillvertex")
Output
Skill vertex
Skill vertex
Skill vertex
How to use Else Statement with While Loop in Python?
The else clause will run only when the while condition comes false. But, if the loop has a breakout, then the program won’t be executed.
Syntax of ‘while Loop’ with an else statement
Syntax of ‘while Loop‘ with the else statement is given below
while condition:
# execute these statements
else:
# execute these statements
Example
In the example provided below, the Python code will print ‘Hello Skillvertex three times with the ‘while’ loop. Hence, after the loop is executed, then it will print ”In Else Block”.
However, it will print the ”In Else Block”as there happens to be an ” else ” block associated with the ‘while.
count = 0
while (count < 3):
count = count + 1
print("Skillvertex")
else:
print("In Else Block")
Output
Hello Skillvertex
Hello Skillvertex
Hello Skillvertex
In Else Block
What is Infinite While Loop in Python?
The while loop in Python functions to run the block of code an infinite number of times.
However, the Python code will use the ‘while‘ loop along with the condition (count==0). The Loop will then execute according to the count that will be equal to 0. The count will be given as 0, Thus the loop will run the program since the condition will be true.
count = 0
while (count == 0):
print("Hello Geek")
Note: It is recommended not to use this loop type as it denotes a never-ending loop. Due to this, the condition will be true and then you need to stop the terminator.
What is For Loop in Python?
In Python, for Loop functions for the sequential traversal. Such as the traversing of a list, string, or array. The ”for in” will work similarly to the for each loop in the other programming languages.
Let us look at the example of how to use ‘For loop‘ the sequential traversal.
Syntax of ‘For Loop’
for iterator_var in sequence:
statements(s)
Example
The Python code provided below will print the ‘i’ in every iteration of the loop. It will also use the ‘for loop‘ as it will repeat the values from 0 to 3 and denoted by the range(0,n).
n = 4
for i in range(0, n):
print(i)
Output
0
1
2
3
What is Nested Loops in Python?
The nested Loops indicate the existence of one loop inside another loop.
Syntax of Nested Loops
for iterator_var in sequence:
for iterator_var in sequence:
statements(s)
statements(s)
Syntax of the nested while loop statement in the Python Programming language is given below:
while expression:
while expression:
statement(s)
statement(s)
Example
Here, it is possible to create nested loops with the triangular pattern of numbers. It will repeat from 1 to 4 according to the iteration number. The final output will look like a Pyramid-like pattern of numbers.
# Example 1: Nested for loops
for i in range(3):
for j in range(4):
print(f"({i}, {j})")
Output
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(1, 0)
(1, 1)
(1, 2)
(1, 3)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
What is Continue Statement in Python?
The continue statement will return with the control to the beginning of the loop. It will neglect the rest of the code inside the loop for the current iteration and then go to the next iteration.
Example
for i in range(5):
if i == 2:
print("Skipping iteration for i =", i)
continue
print("Processing iteration for i =", i)
Output
Processing iteration for i = 0
Processing iteration for i = 1
Skipping iteration for i = 2
Processing iteration for i = 3
Processing iteration for i = 4
What is a Break Statement in Python?
The break statement in Python will bring the control out of the Loop. It functions to exit the loop prematurely.
Example
# Example using break statement
for i in range(5):
if i == 3:
print("Breaking loop for i =", i)
break
print("Processing iteration for i =", i)
Output
Processing iteration for i = 0
Processing iteration for i = 1
Processing iteration for i = 2
Breaking loop for i = 3
What is a Pass Statement in Python?
The Pass Statement in Python will write the empty loops. It functions for empty control statements, functions, and classes.
Example
# Example using pass statement
for i in range(5):
if i == 2:
print("Doing nothing for i =", i)
pass
else:
print("Processing iteration for i =", i)
Output
Processing iteration for i = 0
Processing iteration for i = 1
Doing nothing for i = 2
Processing iteration for i = 3
Processing iteration for i = 4
Conclusion
To conclude, this article summarizes the use of Loops in Python Programming Language. It has also included ‘For loop‘, ‘while loop‘, break statement, and Nested Loops along with the example for your reference.
Python For Loops- FAQs
Q1. What are I and J in Python for loop?
Ans They are referred to as the variable name in Python.
Q2. What is the I in a loop called?
Ans. I is referred to the variable name that has a current array index in each of the loop iterations.
Q3. What is meant by list in Python?
Ans. The list is considered as the data structure in Python that is mutable, changeable, and ordered sequence of elements.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together