Advertisements
Advertisements
प्रश्न
Write a detail note on for loop?
Advertisements
उत्तर
for loop:
- for loop is the most comfortable loop. It is also an entry check loop.
- The condition is checked in the beginning and the body of the loop
(statements-block 1) is executed if it is only True otherwise the loop is not executed.
Syntax:
for counter_variable in
sequence:
statements – block 1
[else: # optional block statements – block 2]
- The counter, variable mentioned in the syntax is similar to the control variable that we used in the for loop of C++ and the sequence refers to the initial, final, and increment value.
- Usually in Python, for loop uses the range () function in the sequence to specify the initial, final, and increment values, range () generates a list of values starting from start till stop-1.
The syntax of range() follows:
range (start, stop, [step])
Where,
start – refers to the initial value
stop – refers to the final value
step – refers to increment value,
this is an optional part.
Examples for range():
range (1,30,1) – will start the range of values from 1 and end at 29 range (2,30,2) – will start the range of values from 2 and end at 28 range (30,3,-3) – will start the range of values from 30 and end at 6E range (20) – will consider this value 20 as the end value ( or upper limit) and starts the range count from 0 to 19 (remember always range () will work till stop -1 value only)
Example-Program:
#Program to illustrate the use of for loop – to print single digit even number
for i in range (2,10,2):
print (i, end=”)
Output:
2468
Flowchart-for loop execution:

For loop execution
APPEARS IN
संबंधित प्रश्न
The condition in the if statement should be in the form of ______
What is the output of the following snippet?
i=1
while True:
if i%3 ==0:
break
print(i,end='')
i +=1
Define control structure.
Write a note on the range () in the loop?
Write a program to display.
A
A B
A B C
A B C D
A B C D E
Write a note on if..else structure.
List the differences between break and continue statements.
Write a detailed note on if..else..elif statement with suitable example.
Write a program to display all 3 digit odd numbers.
Write a program to display a multiplication table for a given number.
