If it can help you,
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
is equivalent to
class_names = []
for w in random.sample(WORDS, snippet.count("%%%")):
class_names.append(w.capitalize())
The .count() will return the number of occurence of "%%%" in the snippet string, so the random.sample will select a subset of N elements from the WORDS list where N is the element of "%%%" in the snippet string.