Introduction to the loops
Python has a powerful conceptcalled loops.Loops makes use of called looping (iteration), which helps cut out our repetitive code. This is very helpful when you want to do something times simply write the initial code then you can repeat multiple times till a particular condition will not met.The purpose of loops is to repeat the same, or similar, code several times. This number of times could be specified to a certain number, or the number of times could be dictated by a certain condition being met.
A conditional loop has the potential to become an infinite loop when nothing in the loop’s body can affect the outcome of the loop’s conditional statement. The While loop and the For loop are the two common types of conditional loops. You can also provide a numerical range(sequence) of values to control how many times the code will execute.
In Python, there are two primary structures for loops:
Where can we use for loop?
A for loop is used for iterating over a sequence (either a list, a tuple, a dictionary, a set, or a strings).
for loop are used for sequential traversal. Ex- traversing a list or strings or array etc.
Syntax:
for i in sequence:
Body of for
“All values inside the sequence will take i variable”
In this above example, we have created list i.e A having strings values, “for” is the syntax of for loop followed by “:”, then there is a variable i that is called placeholder it will take all the values from the sequence i.e. A and then it will print all the values inside the placeholder i.e. i, print(i)
In above example, we have created list B in that we took non-sequential order integer value, however loop will execute on each of the item, in the same manner in which we are defined in the list B.Placeholder(i) is the unique variable inside the for loop it will represent all the values inside the sequence i.e. B, i is not a pre-defined variable so in place of i we can use anything means that any variable, anystrings, except numeric value we can use anything.
In the below example you will better understand how this placeholder will work, in place of placeholder we can use X variable and completely unrelated strings also we can put
Hope you will better understand that whichever placeholder you will put it does not matter. In the above example, list B contains only numeric values, but you can use other type as well like text strings.
In the example below, a list called B is defined with two text strings that represent two statement. The for loop will run iteratively on each text strings (represented by the placeholder strings in each iteration). List B contains two text strings i.e. “This is str” and “This is boolean” so in the first iteration “This is str” will print then in the next iteration “This is boolean” will print.
We can achieve the output in another way by using range method to loop.
In the below example, we have created a list i.e. List, that contains other lists i.e Months and Income. Placeholder flist represent all the elements from the List and List contains two lists i.e Months and Income so it will repeat one by one in which you have defined.
In the first iteration Months will print and in the second iteration Income will print.
A loop is used to repetitively execute program until a defined condition will not met
Iterations that you will observe something else to decide when to stop an iteration – those are the while loops.While loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition will not satisfied then, after the loop in the program will execute.If the test expression is true, statements inside the body of while loop will execute. Then, the test expression is evaluated again. The process goes on till the test expression is evaluated to false.
A while loop can also terminate when a break or return within the statement body is executed. You can use continue to terminate the current iteration without exiting the while loop. continue it will pass control to the next iteration of the while loop. The termination condition will evaluate at the top of the loop.
Where can we use while loops?
while condition:
body_of_while
You can notice that structure of the while loop will remains same except condition and indentation
nested loopsare loopswhich run within another loop.
Where can we use nested loops?
Nested loops are useful when for each pass through the outer loop, you need to repeat some action on the data in the outer loop. Ex- you read a file line by line and for each line you must count how many times the word “the” is found.
Syntax:
Here, iterating_varis the variable that takes the value of the item inside the sequence on each iteration.
These are constructed like:
In the above example, the program first runs the outer loop, execute the first code then it will runs the inner loop i.e. nested loop, nested loop will runs till sequence then program return to the top of the outer loop, run the next iteration and again run the nested loop till sequence and again program return back to top of the outer loop it will execute till the sequence will not complete.
In the below example you can see how nested for loop will work, we have created two lists, A consist of numeric value and B consist of strings value. First program will runs the outer loop and print 1 then program will runs the nested loop, it will run till the sequence will not complete and print a, b, c then program return back to the top of the outer loop and run the next iteration and print 2 then program will run the nested loop till the sequence will not complete and print a, b, c etc. This will execute till the sequence of the outer loop will not complete.
When the while loop is present inside another while loop then it is called nested while loop.
Syntax:
A nested while loop will present while statement inside another while statement. In a nested while loop, first iteration of the outer loop is first executed, after which the inner loop will execute. Once the condition of the inner loop is satisfied, the program moves to the next iteration of the outer loop.
In this post, we have learnt about
for loop will iteratively execute code for each item in a pre-defined list
Nested for loop
A nested loop is a loop that will occurs within another loop
while loop
A while loop is used to iteratively execute code until a pre-defined condition is no longer satisfied (i.e. results in a value of False)
Nested while loop
When the while loop is present inside another while loop then it is called nested while loop.
You can notice that structure of the while loop will remains same except condition and indentation
nested loopsare loopswhich run within another loop.
Nested loops are useful when for each pass through the outer loop, you need to repeat some action on the data in the outer loop. Ex- you read a file line by line and for each line you must count how many times the word “the” is found.
When the while loop is present inside another while loop then it is called nested while loop.
A nested while loop will present while statement inside another while statement. In a nested while loop, first iteration of the outer loop is first executed, after which the inner loop will execute. Once the condition of the inner loop is satisfied, the program moves to the next iteration of the outer loop.
- for loop
Nested for loop
while loop
Nested while loop
Research & References of Introduction to the loops|A&C Accounting And Tax Services
Source
0 Comments