Loop In Python Language
program control enters inside the
loop minimum once as the condition is checked after the execution.
In Python language, we have an only a pre-checked loop which is as follows:
while() loop
for loop
Lets first discussed for loop in
detail. The for is a very simple loop having three parts namely:
- Initialization
- Condition
- Statement to
bring change in the initial state.
Syntax:
for iterator or loop control variable
in range():
statement y
statement x
When a for loop is used a range of
sequences is specified (only once).
Lets first understand the above
syntax. The iterator is the variable whose value must be in the sequence
specified through range function. The value is checked by using 'in' an
operator. As we all know that 'in' is the membership operator which returns
True if the iterator or the variable lies in the range specified or returns
False otherwise. The 'for' loop executes until the value of the expression or
iterator is true or lies in the sequence specified.
The iterator attains values from the
sequence in the loop automatically one by one. The for loop is executed for
each item in the sequence. So the for loop executed continuously till the range
holds and repeat the block of statement y. After the loop executed for every
sequence, it gets terminated and returns to the statement x.
range() Function
The range() is the built-in function
of Python language that is used to iterate over a sequence of numbers. The
syntax of range() is as follows:
range( start, end, [step])
The range() function produces a
sequence of number from start (inclusive) and goes till it encounters the end
(exclusive) means the sequence doesn't include the last limit.
The step argument is optional and it
is used to state the value by which the sequence had to be incremented each
time iteration is completed. But if we don't give the step then sequence is
automatically incremented by a unit that is 1. So the following points can be
taken into consideration when range() is used.
- if the range()
is used using the single argument then it produces the variable value from
0 to argument-1. for example range(100) is equal to writing range(0,100).
- if the range()
is used by passing the twp arguments, it produces value from the first argument to second argument -1.
- if the range()
is used with three arguments then the third argument specifies the interval of the sequence produced. The third argument must be an integer value.
let consider some code to generate
the natural number till the user wants.
n=int(input("Enter the
Number till you want to generate the series"))
# used n+1 because of the loop
executed argument-1 times
for x in range(1,n+1):
print(x)
Considering the above code here x is
used as an iterator whose value ranges from 1 to n+1 means the limit provided
by the user plus one as the limit is exclusive.
It must be noted that every
iteration of the loop must make the loop control variable closer to the end of the
range. With every iteration, the loop variable is automatically updated. This
makes the loop variable to point to the next item in the sequence.
Another point that can be considered
in range() that we can also use negative value as the step when we have to
generate the sequence in reverse order. for example, if we have to generate the
natural number series from the inputted user value to 1.
n=int(input("Enter the Number
till you want to generate the series"))
for x in range(n,0,-1):
print(x)
This program generates the reverse
natural number series.
Nested For Loop
We can also generate the nested loop
with for. The nested loop is those loop where we require to execute the one
loop inside another loop. The main rule that must be kept in mind while creating
or handling the nested loop is that first, the inner loop required to be closed
or completed before the completion of the outer loop. Another thing that must
be remembered or cautioned is that in Python language the scope is defined by
the help of the indentation or white spaces so every loop must be well
indented.
Suppose we have to print the
following pattern:
1
1 2
1 2 3
1 2 3 4..N
N is the number of the term until the a user wants to generate the pattern.
The program for the above problem can
be written as:
n=int(input("Enter the
Number till you want to generate the pattern"))
for x in range(1,n+1):
for y in
range(1,x+1):
print(y,end=" ")
print()
In the above program, we have used
two for a loop. The first loop is used to generate the number of rows whereas
the second loop is used to work for the desirous number of the column. In the
above program, we have also used the end keyword to stop the print() to change
the line after every execution.
Loops can be nested at any level.
Post Your Ad Here
Comments