Lab 1: The Reverser Goal: Reverse a string using list slicing logic.
text = "DevSecOps"
# Strings support slicing too!
reversed_text = text[::-1]
print(reversed_text) # Output: spOceSveD
Lab 2: The Data sampler Goal: Extract the first 3 items and the last 3 items.
data = [10, 20, 30, 40, 50, 60, 70, 80]
first_three = data[:3]
last_three = data[-3:]
print(first_three) # [10, 20, 30]
print(last_three) # [60, 70, 80]
Lab 3: The Data Scrubber (DevSecOps Focus)
Scenario: You have a list of raw credit card numbers. You need to display them in the logs, but for security (PCI-DSS compliance), you must mask everything except the last 4 digits.
# Raw sensitive data
cc_numbers = ["1234-5678-9012-3456", "9876-5432-1098-7654"]
sanitized_logs = []
for card in cc_numbers:
# 1. Slice the last 4 visible digits
visible_part = card[-4:]
# 2. Calculate how many characters need masking (total length - 4)
# We use card[:-4] to get the part we want to hide
masked_length = len(card[:-4])
# 3. Create the mask string
mask = "X" * masked_length
# 4. Combine
sanitized_logs.append(mask + visible_part)
print(sanitized_logs)
# Output: ['XXXXXXXXXXXXXXX3456', 'XXXXXXXXXXXXXXX7654']
Lab 4: The Matrix Hacker (Nested List Manipulation)
Scenario: You have a 3×3 grid. You need to extract the diagonal numbers (top-left to bottom-right).
matrix = [
[10, 20, 30], # Row 0
[40, 50, 60], # Row 1
[70, 80, 90] # Row 2
]
diagonal = []
# Logic: The diagonal items are at indices [0][0], [1][1], [2][2]
for i in range(len(matrix)):
diagonal.append(matrix[i][i])
print(f"Diagonal Data: {diagonal}")
# Output: Diagonal Data: [10, 50, 90]
Lab 5: The “Batch Processor” (Chunking Data)
Scenario: You have 100 User IDs. Your API can only handle 10 IDs at a time. You need to slice the main list into batches of 10.
user_ids = list(range(100)) # Generates 0 to 99
batch_size = 10
# We use a range with a 'step' equal to batch_size
for i in range(0, len(user_ids), batch_size):
# Slice from current 'i' to 'i + 10'
batch = user_ids[i : i + batch_size]
print(f"Processing Batch starting at {i}: {batch}")
# This safely handles the last batch even if it has fewer than 10 items!