A helper function does one focused job for another function. Use one when a task has several parts that are easier to write and check separately. For example, analyzing friends’ phone numbers involves finding unique area codes and then looking up their states. section 23.5 section 23.6
Start by deciding what each helper needs and what it gives back. `get_unique_area_codes` examines phone numbers and returns a tuple without repeated area codes. `get_states` takes those codes and returns their corresponding states. The larger `analyze_friends` function uses the results to print how many friends there are and where they live; it does not return a value. section 23.5
Listing 23.5. Helper function to keep only unique area codes
def get_unique_area_codes():
"""
Returns a tuple of all unique area codes in phones
"""
area_codes = () #1
for ph in phones: #2
if ph[0:3] not in area_codes: #3
area_codes += (ph[0:3],) #4
return area_codes
Follow the loop: it takes the first three digits of each phone number and adds that area code to the result only if it is not already there. section 23.5
These helpers are defined inside `analyze_friends` because they serve that particular task. A nested helper can use the enclosing function’s parameters: `get_unique_area_codes` can read `phones` without receiving it again. By contrast, `get_states` receives the particular tuple of area codes it should look up. section 23.5 section 22.1
From phone numbers to states
Suppose four friends’ phone numbers have area codes 801, 609, 206, and 609. The first helper keeps 609 only once; the second looks up the states for the remaining codes. The final printout reports four friends but only three states. If a code is missing from the lookup data, `get_states` supplies `"BAD AREACODE"` for it. section 23.5
Write and test each function soon after creating it, then check that the functions work together. Aim for a self-contained task that can be reused: a function that reads one file, for instance, can be called for several files instead of being written for exactly two. section 23.6 section 29.1 section 29.2



















