Python Programming Looping Techniques, For Loop in Python Programming and While Loop in Python Programming | Python Programming
- Get link
- Other Apps
Python Programming Looping Techniques, For Loop in Python Programming and While Loop in Python Programming
For loop in Python Programming
What is for loop in Python Programming?
The for loop in Python Programming is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.
Syntax of for Loop
Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
Flowchart of for Loop
Example: Python for Loop
when you run the program, the output will be:
The range() function
We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers).
We can also define the start, stop and step size as a range(start, stop, step size). step size defaults to 1 if not provided.
This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, and step size and generates the next number on the go.
To force this function to output all the items, we can use the function list().
The following example will clarify this.
We can use the range() function in for loops to iterate through a sequence of numbers. It can be combined with the len() function to iterate though a sequence using indexing. Here is an example.
When you run the program, the output will be:
For loop with else
A for loop can have an optional else block as well. The else part is executed if the items in the sequence are used in for loop exhausts.
the break statement can be used to stop a for loop. In such a case, the else part is ignored.
Hence, a for loop's else part runs if no break occurs.
Here is an example to illustrate this.
When you run the program, the output will be:
Here, the for loop prints items of the list until the loop exhausts. When the for loop exhausts, it executes the block of code in the else and prints
While Loop in Python Programming
What is a while loop in Python?
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
We generally use this loop when we don't know beforehand, the number of times to iterate.
Syntax of while Loop in Python
In a while loop, the test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.
Body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.
Flowchart of while Loop
Example: Python while Loop
When you run the program, the output will be:
In the above program, the test expression will be True as long as our counter variable i is less than or equal to n (10 in our program).
We need to increase the value of the counter variable in the body of the loop. This is very important (and mostly forgotten). Failing to do so will result in an infinite loop (never-ending loop).
Finally, the result is displayed.
While loop with else
Same as that of for loop, we can have an optional else block with a while loop as well.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement. In such a case, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.
Here is an example to illustrate this.
Output
Here, we use a counter variable to print the string Inside the loop three times.
On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.
Python Looping Technique
Python programming offers two kinds of the loop, the for loop and the while loop. Using these loops along with loop control statements like break and continue, we can create various forms of loop.
The infinite loop
We can create an infinite loop using a while statement. If the condition of while loop is always True, we get an infinite loop.
Example #1: Infinite loop using while
Output
Loop with the condition at the top
This is a normal while loop without break statements. The condition of the while loop is at the top and the loop terminates when this condition is False.
Flowchart of Loop With Condition at Top
Example #2: Loop with the condition at the top
When you run the program, the output will be:
Loop with the condition in the middle
This kind of loop can be implemented using an infinite loop along with a conditional break in between the body of the loop.
Flowchart of Loop with Condition in Middle
Example #3: Loop with the condition in the middle
Output
Loop with the condition at the bottom
This kind of loop ensures that the body of the loop is executed at least once. It can be implemented using an infinite loop along with a conditional break at the end. This is similar to the do...while loop in C.
Flowchart of Loop with Condition at Bottom
Example #4: Loop with the condition at the bottom
Output
- Get link
- Other Apps
Comments
Post a Comment
Subscribe Us and Thanks for visiting blog.