Code viewer for World: Control Flow (Loops): Loop...
"""
Topic: Control Flow (Loops): Looping Through Lists
Purpose: Learn how to iterate over items in a list
Output: Shows how Python processes list elements one by one
"""

# -----------------------------
# 1. Example list
# -----------------------------
numbers = [10, 20, 30, 40, 50]

# -----------------------------
# 2. Basic loop through a list
# -----------------------------
# Each iteration gives one item
for num in numbers:
    print(num)

# -----------------------------
# 3. Using index while looping
# -----------------------------
# range() with len() to access positions
for i in range(len(numbers)):
    print("Index:", i, "Value:", numbers[i])

# -----------------------------
# 4. Conditional logic in list loop
# -----------------------------
# Checking values while looping
for num in numbers:
    if num > 25:
        print(num, "is greater than 25")
    else:
        print(num, "is 25 or less")

# -----------------------------
# 5. Modifying values during loop
# -----------------------------
# Creating a new list from an existing one
squared_numbers = []

for num in numbers:
    squared_numbers.append(num * num)

print("Squared list:", squared_numbers)

# -----------------------------
# 6. Notes
# -----------------------------
# 1. Lists are iterable collections.
# 2. for loops are the most common way to loop through lists.
# 3. Index-based looping gives access to positions.
# 4. Avoid modifying the same list you're looping over.
# 5. List loops are fundamental in data processing.