Determining whether a letter is either a vowel or a consonant is perhaps among the most common elementary problem in programming. This doesn't seem to have apparent application, but who knows when you'll need it?
This is intended for beginner python programmers who have not yet mastered the "pythonic ways" as they say. Although intermediate and advanced programmers are also welcome.
The conventional approach to this type of problem is to individually check if a letter equals to any of the vowels or consonants. To simplify the code, a series of logic 'or' is used. To show and easily understand this approach, a python code is shown below.
is_vowel(letter):
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
return True
I wouldn't explain any further. Instead, I'd like you to imagine what the code would look like if it were for consonants. Moreover, what the code would look like if we were to consider uppercase letters.
Here's how it would look like in a "pythonic" solution.
is_vowel(letter):
if letter.lower() in 'aeiou':
return True
is_consonant(letter):
if letter.lower() in 'bcdfghjklmnpqrstvwxyz':
return True
This particular vowel/consonant problem doesn't seem to have apparent application as it seems too basic. However, this technique can very well be applied to other types of problems.