-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced-Loops-10mins.py
More file actions
51 lines (30 loc) · 1.57 KB
/
Advanced-Loops-10mins.py
File metadata and controls
51 lines (30 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# "Loops are powerful for repeating tasks, but they can do much more when combined with
# advanced techniques like nested loops and list comprehensions."
# Nested Loops
"A loop inside another loop lets you work with grids or combinations."
# Example Code:
for row in range(3): # Outer loop iterates over rows
for col in range(3): # Inner loop iterates over columns
print(f"({row}, {col})", end=" ") # Print the current row and column, stay on the same line
print() # Print a newline after each row
# Output:
# Output of the nested loop:
(0, 0) (0, 1) (0, 2) # First row: columns 0, 1, 2
(1, 0) (1, 1) (1, 2) # Second row: columns 0, 1, 2
(2, 0) (2, 1) (2, 2) # Third row: columns 0, 1, 2
# List Comprehensions
"A shorthand way to create lists."
# Example Code:
squares = [x**2 for x in range(5)] # List comprehension to create a list of squares of numbers from 0 to 4
print(squares) # Print the list of squares
# Output:
[0, 1, 4, 9, 16]
# 🛠️ Student Activity:
# Create a nested loop to print a 5x5 grid of numbers.
# Use a list comprehension to create a list of even numbers from 0 to 20.
for row in range(3): # Outer loop iterates over rows
for col in range(3): # Inner loop iterates over columns
print(f"({row}, {col})", end=" ") # Print the current row and column, stay on the same line
print() # Print a newline after each row
squares = [x**2 for x in range(5)] # List comprehension to create a list of squares of numbers from 0 to 4
print(squares) # Print the list of squares