A pangram is a sentence that contains all the letters of the English alphabet at least once, for example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to check a sentence to see if it is a pangram or not.
from string import ascii_lowercase #This version creates a string with all lower case characters called alphabet #Each time a character is found in sentence that character is #removed from the alphabet string. #If we, when we have iterated through the sentence string, don't have any characters left #in the alphabet string all characters must have been seen at least once def is_pangram_v1(sentence): alphabet = "abcdefghijklmnopqrstuvwxyz" for letter in sentence: if letter in alphabet: alphabet = alphabet.replace(letter, "") return not alphabet #This is a more compact version #Here we use the ascii_lowercase to create the alphabet string #All is a function that returns true if all items in the iterable passed to it #is true or if the iterable is empty #Here we pass a comprehension to it. First we iterate over the alphabet getting a letter #If that letter is found in the sentence the comprehension will insert True in the resulting #iterable. If the letter is not found it will insert False. #If a single False is found in the resulting iterable all will return False, if not it will return #True and we have a pangram def is_pangram_v2(sentence): alphabet = ascii_lowercase # list of letters a-z return all(letter in sentence.lower() for letter in alphabet) def main(): print(is_pangram_v1("The quick brown fox jumps over the lazy dog")) print(is_pangram_v2("The quick brown fox jumps over the lazy dog")) print(is_pangram_v1("It is sunny today")) print(is_pangram_v2("It is sunny today")) if __name__ == '__main__': main()