-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_nested_loops.py
More file actions
46 lines (42 loc) · 1.76 KB
/
Copy path15_nested_loops.py
File metadata and controls
46 lines (42 loc) · 1.76 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
#======================================================================================
# NESTED LOOPS
#--------------------------------------------------------------------------------------
# A nested loop means one loop inside another loop
# if:
# -outer loop runs m times
# -inner loop runs n times
# Total operations = m * n
#======================================================================================
for x in range(3): # outer loop
for y in range(2): # inner loop
print(x,y)
#--------------------------------------------------------------------------------------
# Real world uses cases
#--------------------------------------------------------------------------------------
# why we need nested loops?
# 1) crossing data
# 2) navigate hierarchy
#--------------------------------------------------------------------------------------
# crossing data
#--------------------------------------------------------------------------------------
colors = ["Red", "Blue", "Green"]
sizes = ["S","M","L","XL"]
for color in colors:
for size in sizes:
print(f"{color} - size {size}")
#--------------------------------------------------------------------------------------
# navigate hierarchy
#--------------------------------------------------------------------------------------
years = [2025, 2026]
months = ["Jan", "Feb"]
days = range(1,29)
for year in years:
for month in months:
for day in days:
print(f"report_{year}_{month}_{day}.csv")
# SELECT COUNT(*) FROM customers WHERE id IS NULL;
tables = ["customers", "orders", "products", "prices"]
columns = ["id", "create_date"]
for table in tables:
for column in columns:
print(f"SELECT COUNT(*) FROM {table} WHERE {column} IS NULL;")