Python Lists Lab

Lab 1: User Access Management (The “Admin” Scenario)

Scenario: You are a System Administrator. You have a list of active employees, and you need to onboard new hires and remove employees who have left the organization.

Concepts Covered: .append(), .remove(), if/else logic.

# 1. Current Active Users List
active_users = ["raj_admin", "priya_dev", "amit_ops", "sara_test"]

# 2. New Joiners to be added
new_hires = ["vikram_sec", "neha_ds"]

# 3. Employee who resigned
resigned_user = "amit_ops"

print(f"--- Original Team: {active_users} ---")

# Step A: Onboarding (Adding Users)
# We use .extend() because we are adding multiple people at once.
# Official Doc: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
active_users.extend(new_hires)
print(f"After Hiring: {active_users}")

# Step B: Offboarding (Removing Users)
# Always check if the user exists before removing to avoid errors!
if resigned_user in active_users:
    active_users.remove(resigned_user)
    print(f"User '{resigned_user}' has been removed successfully.")
else:
    print(f"User '{resigned_user}' not found in the system.")

print(f"--- Final Team List: {active_users} ---")

Lab 2: Server Health Monitoring (Data Analysis)

Scenario: You have a list of CPU utilization percentages collected every minute. You need to find the peak load, the lowest load, and the average load to generate a health report.

Concepts Covered: max(), min(), sum(), len().

# CPU Load readings (in percentage)
cpu_usage = [12, 45, 67, 89, 15, 99, 23, 56, 12, 45]

# Step A: Find Peak Load (Maximum)
peak_load = max(cpu_usage)

# Step B: Find Idle Load (Minimum)
idle_load = min(cpu_usage)

# Step C: Calculate Average Load
# Formula: Sum of all items divided by count of items
average_load = sum(cpu_usage) / len(cpu_usage)

print("--- Server Health Report ---")
print(f"Peak CPU Usage: {peak_load}% (Critical Alert if > 90)")
print(f"Lowest CPU Usage: {idle_load}%")
print(f"Average Load: {average_load:.2f}%") 

# Logic Check:
if peak_load > 90:
    print("WARNING: Server hit critical load! Check logs immediately.")
else:
    print("Status: Server is healthy.")

Lab 3: Cleaning Network Data (Sanitization)

Scenario: You pulled a list of IP addresses from a firewall log. However, the logs are messy—there are duplicate entries because some IPs were blocked multiple times. You need a clean, unique list of blocked IPs sorted numerically.

Concepts Covered: set() (to remove duplicates), .sort(), Type Conversion.

# Raw Firewall Logs (Notice duplicates)
blocked_ips = [
    "192.168.1.5", 
    "10.0.0.1", 
    "192.168.1.5", 
    "172.16.0.50", 
    "10.0.0.1"
]

print(f"Raw Count: {len(blocked_ips)}")

# Step A: Remove Duplicates
# Converting list to a Set removes duplicates automatically.
# Then we convert it back to a list.
unique_ips = list(set(blocked_ips))

# Step B: Sort the IPs
# Note: This does string sorting, not numeric sorting, but it works for basic organization.
unique_ips.sort()

print(f"Cleaned Count: {len(unique_ips)}")
print(f"Unique Block List: {unique_ips}")

Lab 4: Job Queue Simulation (FIFO – First In, First Out)

Scenario: You are building a simple “Task Scheduler” for a DevOps pipeline. Jobs come in and must be processed in the order they arrived.

Concepts Covered: .pop(0) vs .pop(), while loops.

# Pending Tasks Queue
deployment_queue = ["Build_Image", "Run_Tests", "Deploy_to_Staging", "Notify_Slack"]

print(f"Initial Queue: {deployment_queue}")

# Process the queue until it is empty
while len(deployment_queue) > 0:
    # .pop(0) removes the item at index 0 (The first one)
    # This simulates a Queue (FIFO). If we used .pop(), it would be a Stack (LIFO).
    current_task = deployment_queue.pop(0)
    
    print(f"Processing Task: {current_task}...")
    # Simulate work...
    print(f"Task '{current_task}' Completed!")
    print(f"Remaining Queue: {deployment_queue}")
    print("-" * 30)

print("All tasks finished. Pipeline idle.")

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top