Introduction to the loops

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: 

Introduction to the 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”

Introduction to the loops

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)

Introduction to the loops

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

Introduction to the loops

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.

Introduction to the loops

We can achieve the output in another way by using range method to loop.

Introduction to the loops

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.

Introduction to the loops

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 

Introduction to the loops

Introduction to the loops

Introduction to the loops

Introduction to the loops

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:

Introduction to the loops

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.

Introduction to the loops

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.

Introduction to the loops

Introduction to the loops

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.

  • for 
  • while 
  • “for” loop is iteratively execute code for each item in a pre-defined list 
  • Iterations that you know how many times it will occur (even if you can stop it before) means that you know how many times loop will run. 
  • The syntax of a for loop is for (followed by (: ) ) 
  • With the for loop we can execute a number of statements, once for each item in a list, tuple, set. 
  • Let say, you want to repeat some code multiple times but you will not care about variable I, so it will be good practice to replace I with _, this means that we will not care about this value
  • The while loop in Python will iterate over a block of code if the test expression (condition) is true. We generally use this loop when we don’t know the number of times to iterate beforehand. 
  • Ex: – let say, If we want to check the grade of every student in the class, we loop from 1 to that particular range of number. When the number of times is not known, we use a “while” loop. 
  • The while loop is used repeat a section of code an unknown number of times until a specific condition is met. Ex- say we want to know how many times a given number can be divided by 2 before it is less than or equal to 1. 
  • Condition can be a limit that how many times the while loop will run, you can see in the below example the loop will run till 9 we have restrict the iteration. 
  • Once loop will reaches 10 (x=10) then loop will not execute
  • A while loop start with while and then condition.  
  • In the below code if you notice that we have given the indentation, it must have to give the indentation means that statement is inside the loop. 
  • In this example, we add 1 in the x and value of x does not exceed the given condition (x<10). 
  • In the below code we used ‘<’ operator that means the value of x will start from 0 till 9 exclude 10. 
  • We have used one more operator i.e. ‘+=’ that means 1 will add in each of the element of the x
  • When loop will reaches 10 (x=10) then the condition no longer satisfied so the loop will not execute another iteration 
  • In the above example, the code will executed in order that means the first 1 will add in the value of x then print() function will execute. 
  • You can change the position if you want to print the value of x first then also you can add the 1 value in the x
  • You can also use the range operator instead of the comparison operator in this you need to specify the range for the iteration  
  • In this range operator starting value is inclusive but not end value 
  • In the below example, while loop runs till 5 and 5 will not include then we have increment x by 1 means that it will add 1 in each of the x element. 
  • When x reaches to 5 then it will not execute another iteration of code.
  • 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.

  • Loops: 
    • for 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

    Author: promotiondept

    Leave a Reply