Lecture-17_4 Iteration Python - nested while 24-25
Introduction to Iteration in Python
Focus: Understanding nested while loops
Topic: Iteration and control flow in Python programming using nested loops.
Nested Loops Overview
Nested loops allow loops to contain other loops, providing a way to iterate through collections or repeat tasks driven by another iteration.
Types of nested loops:
For loop with nested for loop
For loop with nested while loop
While loop with nested while loop
While loop with nested for loop
Outer Loop: The primary loop that contains another loop.
Inner Loop: The nested loop contained within the outer loop.
An outer loop can contain multiple inner loops.
Control Structures in Nested Loops
Outer Loop Control: The outer loop dictates the number of times the inner loop will execute.
Inner Loop Execution: For each iteration of the outer loop, the inner loop executes all its code, possibly multiple times.
Calculation of Total Iterations: Total iterations can be calculated as:
Total iterations = (outer loop iterations) * (inner loop iterations)
Example: Outer loop iters = 3, inner loop iters = 7 → Total iterations = 3 * 7 = 21 iterations.
Example of Nested While Loops
Use case: Grading an unknown number of student submissions.
Code Snippet:
student = input(‘Students y/n: ‘) while student == 'y': work = input ('Work to grade y/n: ') while work == 'y': grade = int(input('Enter grade: ')) work = input ('More work y/n: ') student = input (‘Another student y/n: ')
Iterating Through Data Sets
Task: Find integer 8 in a matrix of 9 rows and 38 columns.
Given data set: A matrix containing digits which needs iteration to find specified values.
Example Data (simplified):
62524262625363363309774646435353635355
...
Handling Control Flow with Break Statement
Example of using
breakin a nested while loop to terminate execution at a certain condition.nums = 10 while nums > 0: nums -= 1 if nums == 5: break print(nums)Caution:
breakonly exits the current loop but can cause issues if overused in nested structures.
Using Continue Statement
continueskips the current iteration but continues the loop.nums = 10 while nums > 0: nums -= 1 if nums == 5: continue print(nums)
While-Else Construct
The
elseblock after a while loop executes only when the loop ends due to a false condition, not via break.Example:
number = 1 while number < 6: print(number) number += 1 else: print(‘number is now 6’)Practical use of while-else is limited; clearer structure is preferred without it.
Level-2 Nesting: Combining Constructs
Example of combining while-else with if statements for user input attempts.
attempt = 3 while attempt > 0: password = input("Enter your password:") if password == 'COMP101': print("Password Accepted.") break else: attempt -= 1 else: print("Maximum attempts reached - access failed")The loop handles input attempts and employs
breakefficiently.
Python-Specific Syntax
While-else Structure: Syntax overview and notes about practical implications.
Caution against overuse due to possible logic confusion and implementation complexity.
Use of Pass Statement
passserves as a placeholder for future code, effectively doing nothing but maintaining structure.nums = 10 while nums > 0: nums -= 1 if nums == 5: pass
Flow Control Statements Categorization
Control statements in Python are categorized as:
Conditional Statements (evaluate True or False)
Transfer Statements (alter execution flow)
Iterative Statements (repeat code blocks indefinitely based on condition)
Limitations and Best Practices
Python supports a maximum of 20 levels of nested structures.
Check design choices when using significant nesting for better readability and maintenance.