Loop in Python
Loop in Python
Chapter 7
Loops in Python
Introduction:
By default, programs in any programming language are executed in a sequential order
where each statement is executed one after the other. However, there may be situations
where a block of code needs to be repeated multiple times.
To address this, programming languages provide various types of loops that allow a set of
instructions to be repeated until a specific condition is met.
Here, we will discuss the different types of looping statements available in Python, the
syntax of different looping statements in Python along with examples for a better
understanding
Loop:
Execute a set of statements repeatedly until a particular condition is satisfied.
Q. How many types of loops are available in Python?
Python offers three types of looping Statements in Python:
1. For loop
2. While loop
3. Nested loop
Example:
fruits = ["apple", "kiwi", "banana", "pears"]
for fruit in fruits:
print(fruit)
Output:
apple
kiwi
banana
pears
The range( ) function:
It generates a list of numbers, which is generally used to iterate over with for loop. range( ) function
uses three types of parameters, which are:
• start: Starting number of the sequence.
• stop: Generate numbers up to, but not including last number.
• step: Difference between each number in the sequence.
Python use range( ) function in three ways:
a. range(stop)
b. range(start, stop)
c. range(start, stop, step)
a. range(stop): By default, It starts from 0 and increments by 1 and ends up to stop, but
not including stop value.
Example:
for x in range(4):
print(x)
Output:
0
1
2
3
b. range(start, stop): It starts from the start value and up to stop, but not including stop value.
Example:
for x in range(2, 6):
print(x)
Output:
2
3
4
5
c. range(start, stop, step): The third parameter specifies to increment or decrement the
value by adding or subtracting the value.
Example:
for x in range(3, 8, 2):
print(x)
Output:
3
5
7
Explanation of output: 3 is starting value, 8 is stop value and 2 is step value. First print 3 and increase
it by 2, that is 5, again increase is by 2, that is 7. The output can’t exceed stop-1 value that is 8 here. So,
the output is 3, 5, 8.
Q. What is a while loop? Explain with an example.
While loop: With the while loop we can execute a set of statements as long as a condition is true. It
requires to define an indexing variable.
Syntax of while Loop in Python
while test_expression:
Body of while
Example:
1. Write a Python program to print each item in the sequence until you reach
the end of the given list:
fruits = ["apple", "kiwi", "banana", "pears"]
2. Write a Python program to print the First 10 natural numbers using a while
loop.
3. Write a program to print a multiplication table of a given number
4. Write a Python program to print all even numbers from 1 to 100 natural
numbers using a while loop.
5. Correct the following program to print the following pattern.
Code:
rows = 7
for i in range(3, rows + 1):
print("+" * i)
Output:
*
**
***
****
*****