Python_Code_Examples.txt
Document Details

Uploaded by momogamain
Full Transcript
1. Nested For Loops with Dynamic Range in Inner Loop for i in range(1, 6): # Outer loop sets the limit for the inner loop for j in range(1, i + 1): # Inner loop range depends on the current value of i print(f“{j}”, end=’’) # Print j values on the same line print() # Newline after each outer loop com...
1. Nested For Loops with Dynamic Range in Inner Loop for i in range(1, 6): # Outer loop sets the limit for the inner loop for j in range(1, i + 1): # Inner loop range depends on the current value of i print(f“{j}”, end=’’) # Print j values on the same line print() # Newline after each outer loop completes 2. Using Slicing and Range Together in Nested Loops numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90] for i in range(1, 5): print(f“Slice up to {i}:”, end=’‘) for num in numbers[:i]: # Slicing the list up to index i print(num, end=’ ’) print() # Newline after printing the sliced list 3. While Loop Nested in a For Loop for i in range(3): j = i while j < 3: print(f“Pair (i={i}, j={j})”, end=’ ’) j += 1 print() # New line after each completion of the inner loop 4. Nested While Loops with Dependency on Outer Variable i = 0 while i < 5: j = 0 while j <= i: print(f“({i}, {j})”, end=’ ’) j += 1 print() # New line after each iteration of the outer loop i += 1 5. Combining For and While Loops with Range and Logical Operators for i in range(2, 5): j = 0 while j < i and i + j < 7: print(f“Values: i={i}, j={j}”, end=‘;’) j += 1 print() # New line after each complete iteration of the inner loop