Loops in Python
Definition of Loop
Loop is used to repeat a set of instructions multiple times. Repetition of a set of statements in a program is
done using loop constructs or iterative constructs. A block of code executed multiple times is called a loop.
In python we can create loop using the for and while statements.
Sequence Type
Sequence Type
String List Tuple
a. String – In python strings are a sequence of characters.
Example:
# Using single quotes
string1 = 'Hello, world!'
# Using double quotes
string2 = "Python is fun"
# Using triple quotes for multiline strings
string3 = """This is a
multiline string"""
# Empty string
string4 = ""
b. List – It is a sequence of values of mixed data types enclosed in square brackets. The values are
separated using a comma.
Example:
marks = [78, 88, 98, 35, 15]
fruits = ["Orange", "Banana", "Apple", "Mango"]
score = ["Group A", 30, "Group B", 45]
c. Tuple – It is a sequence of values of mixed data types separated by commas, but the values are
enclosed in round brackets ( ).
Example:
marks = (78, 88, 98, 35, 15)
fruits = ("Orange", "Banana", "Apple", "Mango")
mixedT = (1, "Hello", 3.14, True)
range( ) function
The built-in function range( ) in Python is used to generate a sequence of numbers. This function can take
a maximum of three integer arguments.
Syntax:
range([start], stop, [step])
Argument Description
Optional argument. An integer number specifying the position at which to start
start
the sequence. Default value is 0.
Required argument. An integer number specifying the position at which to stop
stop
the sequence. The stop value is not included in the sequence.
Optional argument. An integer number specifying the incrementation. Default
step
value is 1.
Examples to understand the use of range( ) function.
Example Sequence Generated Explanation
Start value: 0 (default value)
range(5) 0,1,2,3,4
Stop value: 5
Step value: 1 (default value)
Start value: 1
range(1, 10) 1,2,3,4,5,6,7,8,9
Stop value: 10
Step value: 1 (default value)
Start value: 10
range(10, 21, 2) 10,12,14,16,18,20
Stop value: 21
Step value: 2
Start value: 10
range(10, 0, -3) 10,7,4,1
Stop value: 1
Step value: -3
Output of the above examples on the Python prompt in the IDLE.
for Loop
The for loop is used to iterate over a range of values or a sequence. In other words, the for loop is used to
repeat a block of statements for the different values in a range or a sequence (string, list or tuple).
Syntax:
for <Control-variable> in <sequence/range>:
Body of the loop
➢ Control-variable – It takes values from sequence or range one by one.
➢ Sequence/range – Collection of values.
➢ Body of the loop – Loop body must be indented. It is executed for each value in the sequence or range.
Example1:
Code Output
➢ The loop runs 5 times as there are 5 elements in the list number.
➢ In each run or iteration, the loop control variable i takes a value from the list. The loop control
variable i takes the value 45 in the first run, then it takes the values 50, 34, 56 and 99.
➢ In the loop body, the value of i is displayed using the print( ) function.
Example2:
Code Output
➢ The loop runs 5 times as there are 5 characters in the string country. The loop control variable,
letter takes one character of the string at a time.
Example3:
Code Output
➢ In this example, range(1, 6) generates the values 1, 2, 3, 4, 5. The loop control variable takes these
values one at a time.
Programs:
Program1:
#Program to display a series of numbers from 1 to 5.
Code Output
Program2:
#Program to display a series of numbers from 5 to 1.
Code Output
Program3:
#Program to display the square of first 5 numbers.
Code Output
OR
Program4:
#Program that accepts a number from user and display the table of that number.
Code Output
Program5:
#Program that displays all the even numbers from 1 to 10.
Code Output
Program6:
#Program that displays the series 1,2,3,4,5
Code Output
while Loop
The while loop statement in Python executes a block of code repeatedly as long as the test condition is true.
The loop comes to an end only when the test condition becomes false.
Syntax:
Initialisation
while condition:
Body of the loop
Increment/ Decrement of loop variable
➢ Initialisation – To assign initial value for the loop variable.
➢ Condition – Loop runs only when the condition is true.
➢ Body of the loop – Loop body must be indented. It is executed as long as the condition is true.
➢ Increment/ Decrement – To increase or decrease the value of loop variable.
The execution of the loop begins only if the condition is True. If the condition of the while loop is False at
the beginning, then the loop is not executed even once.
After each iteration of the loop, the condition is tested again, and the loop continues as long as the condition
remains True. When the condition becomes False, the control is transferred to the statement immediately
following the body of the while loop.
The statements within the body of the while loop must ensure that the condition eventually becomes false;
otherwise, the loop will become an infinite loop. An infinite loop is a loop that does not end.
Example1:
Coding Output
Initialisation
Condition
Loop body (statement)
Increment
Program1:
#Program to display a series of numbers from 1 to 5.
Code Output
Program2:
#Program to display a series of numbers from 5 to 1.
Code Output
Program3:
#Program to display all the odd numbers from 1 to 10.
Code Output
Program4:
#Program to take as many as numbers user wants and display their sum.
Code Output