In addition to Rolando's general description I would recommend your script maintains and expected configuration definition to compare against the active process list. Rolando's steps only very connected slaves. A likely scenario a slave would fall so far behind many bin logs is replication broken or was administratively stopped and not in the masters process list at all.
Treat an event of a missing slave as an error state in which no binary logs are purged from the master.
This does add the additional admin overhead of needing to keep that config list in sync when adding/removing slaves but required for your script to work as desired.
Further consider your retention policy. If you want to spin up a new slave you might do that by restoring a recent backup and restarting replication from when the backup was taken. For the scope of this discussion we'll assume you have daily backups and are certain the binlog position stored with the backups have been proven to be working correctly.
If you have daily backups you'd probably want to keep at least 2 days of bin logs.
Edit: After rereading your post it sounds like your question is more a long the lines of "how to write scripts". Python is certainly a viable option suited for the task. Whether it's perl, python, php or other you'll need to look up how to work with their MySQL API clients. More specific questions of how-to-code in these languages are probably better placed in stackoverflow.com after googling some basic intro's to the language to gain that literacy.