4.3.4 Tuple Query Methods: count() and index()

Unit: Unit 4: Strings, Lists, and Tuples • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Query occurrences and positions in a telemetry tuple
status_codes = (200, 404, 200, 500, 200, 301, 200)

# 1. Count occurrences of 200 OK responses
success_count = status_codes.count(200)

# 2. Find the first occurrence index of error 500
server_error_index = status_codes.index(500)

# 3. Total telemetry events
total_events = len(status_codes)

print("Total events:", total_events)
print("Success (200) count:", success_count)
print("First 500 error index:", server_error_index)
Open in Live Interactive Python Sandbox →