I want to create a function that checks if an email has a username that is 5 to 20 characters long, starts with an alphabet letter and ends with one domain names of (.org, .net, .org). It also corrects, if the user enters a capital letter. I am fully aware of that the code does not make sense as employing regular expressions is the best way, but this is just an exercises for me to built better codes in python. So I was wondering, which would the best way if a code needs to check multiple things and eventually returns True/False (like in this example).
def is_valid_email(raw_email): email = raw_email.lower() try: name, domain_name = email.split('@') except ValueError: return False else: name_check = all([len(name) < 20, len(name) > 5, name.isalpha()]) domains = (".com", ".net", ".org") domain_check = any([domain_name.endswith(domain) for domain in domains]) return name_check and domain_check print(is_valid_email('[email protected]'))