I'm learning Python coming from some beginner-level experience with Java. It all makes sense for the most part, but one of the exercises kind of made me wonder what actually happens within Python.
import string
def ispangram(str1, alphabet=string.ascii_lowercase):
str1 = set(str1.lower().replace(' ',''))
alphabet = set(alphabet)
print(str1)
print(alphabet)
return str1 == alphabet
The goal of this function is to determine whether or not a string passed as a parameter is a pangram (uses every letter of the alphabet). Obviously the print statements aren't necessary, but I included them to try and figure out what was going on.
Input:
ispangram("The quick brown fox jumps over the lazy dog")
Output:
{'d', 'e', 'r', 'b', 't', 'i', 'l', 'a', 'y', 'o', 'v', 'p', 'z', 'c', 'g', 'n', 'f', 'q', 'x', 'm', 'h', 'w', 'k', 's', 'u', 'j'}
{'d', 'e', 'r', 'b', 't', 'i', 'l', 'a', 'y', 'o', 'v', 'p', 'z', 'c', 'g', 'f', 'n', 'q', 'x', 'm', 'h', 'w', 'k', 's', 'u', 'j'}
returns True
It prints out the exact same set, which is what is confusing to me. I know that the order of sets doesn't matter when comparing them, which makes sense, but why would str1 and alphabet be the exact same? Is it because this order is the optimal way to store these 26 lowercase letters in memory?
Finally, with more complex set comparisons, does having two sets being automatically sorted make it more efficient? How does Python do it?