I am new to Python (and have basic knowledge of Java) and learning it on my own at the moment by using the Head First Python, 2nd Edition book.
In one of the examples in the book, it shows how the .intersection() method of Sets is used. It does so in the following way:
if __name__ == '__main__':
def search_for_vowels(word: str) -> set:
"""Return any vowels found in supplied word."""
vowels = set('aeiou')
return vowels.intersection(set(word))
print(search_for_vowels('hitch-hiker'))
print(search_for_vowels('sky'))
However, when I tried this on my own, I accidentally forgot the 'set' part in the above code (above the print statements), hence it became:
if __name__ == '__main__':
def search_for_vowels(word: str) -> set:
"""Return any vowels found in supplied word."""
vowels = set('aeiou')
return vowels.intersection(word)
print(search_for_vowels('hitch-hiker'))
print(search_for_vowels('sky'))
The code, however, did run without any problems and returned the correct output. This seems kind of strage to me, as I would be comparing a set to a string, and not a set to a set. My question therefore is: Does Python automatically convert a string to a set when performing the intersection() method of Set?
Thanks in advance, David