CMPSC 100 Computational Expression
Organizing collections of data
Problem:
Solution:
Collections of items in order
Output:
red
green
green
3
len()
gives total number of itemsLoop through items:
Output:
red
blue
green
Loop with indices (when you need position):
Output:
Student 1: 85 points
Student 2: 92 points
Student 3: 78 points
Output:
Highest score: 95
Total students: 5
my_list = [item1, item2, item3]
my_list[0]
(first), my_list[-1]
(last)len(my_list)
for item in my_list:
for index in range(len(my_list)):
Remember: Indexing starts at 0!
Getting parts of a list
led_names = ["Red", "Yellow", "Green", "Blue", "White"]
flash_counts = [2, 3, 4, 5, 6, 7]
# Get first 3 items
first_three = led_names[:3]
print(first_three) # ["Red", "Yellow", "Green"]
# Get last 2 items
last_two = led_names[-2:]
print(last_two) # ["Blue", "White"]
# Reverse the entire list
reversed_list = led_names[::-1]
print(reversed_list) # ["White", "Blue", "Green", "Yellow", "Red"]
# Use in patterns
for count in flash_counts[:3]: # Only first 3 counts
print(f"Flashing {count} times")
Output:
["Red", "Yellow", "Green"]
["Blue", "White"]
["White", "Blue", "Green", "Yellow", "Red"]
Flashing 2 times
Flashing 3 times
Flashing 4 times
Converting lists to strings
led_names = ["Red", "Yellow", "Green"]
# Join with commas
colors_text = ", ".join(led_names)
print(colors_text) # "Red, Yellow, Green"
# Join with different separators
dash_format = " - ".join(led_names)
print(dash_format) # "Red - Yellow - Green"
# Useful for display summaries
print(f"LEDs used: {', '.join(led_names)}")
Output:
Red, Yellow, Green
Red - Yellow - Green
LEDs used: Red, Yellow, Green
Cycling through limited lists safely
The Problem in Lab 4:
Solution with Modulo:
Output:
Pattern 1: Chase (0 % 5 = 0)
Pattern 2: All Blink (1 % 5 = 1)
Pattern 3: Reverse Chase (2 % 5 = 2)
Pattern 4: Wave (3 % 5 = 3)
Pattern 5: Pulse (4 % 5 = 4)
Pattern 6: Chase (5 % 5 = 0) ← Cycles back!
Pattern 7: All Blink (6 % 5 = 1)
Pattern 8: Reverse Chase (7 % 5 = 2)
Why modulo? It’s elegant, safe, and handles infinite cycling automatically!
Growing your lists
Output:
Original: ['red', 'blue']
After append: ['red', 'blue', 'green']
After insert: ['red', 'yellow', 'blue', 'green']
Key methods: - list.append(item)
adds to the end - list.insert(index, item)
adds at specific position
Making lists smaller
fruits = ["apple", "banana", "orange", "banana"]
print(f"Original: {fruits}")
# Remove by value (first occurrence)
fruits.remove("banana")
print(f"After remove: {fruits}")
# Remove by index
removed = fruits.pop(1) # Remove index 1
print(f"Removed '{removed}': {fruits}")
# Remove last item
last = fruits.pop()
print(f"Removed '{last}': {fruits}")
Output:
Original: ['apple', 'banana', 'orange', 'banana']
After remove: ['apple', 'orange', 'banana']
Removed 'orange': ['apple', 'banana']
Removed 'banana': ['apple']
Key methods: - list.remove(item)
removes first occurrence of item - list.pop(index)
removes and returns item at index - list.pop()
removes and returns last item
Finding what you’re looking for
Output:
We have bananas!
No grapes available
Orange is at index 2
Key operators: - item in list
- True if item exists - item not in list
- True if item doesn’t exist - list.index(item)
- Returns position of item