π Day 24/150 – Check Vowel or Consonant in Python
One of the simplest and most important beginner problems in Python is checking whether a character is a vowel or a consonant. It helps you understand conditions, strings, and user input.
π What is a Vowel?
Vowels in English are:
a, e, i, o, u
(Also consider uppercase: A, E, I, O, U)
Everything else (alphabets) is a consonant.
πΉ Method 1 – Using if-else
char = 'a'
if char.lower() in 'aeiou':
print("Vowel")
else:
print("Consonant")
π§ Explanation:
- char.lower() converts input to lowercase.
- 'aeiou' contains all vowels.
- in checks if the character exists in that string.
π Best for: Clean and readable logic.
πΉ Method 2 – Taking User Input
char = input("Enter a character: ")
if char.lower() in 'aeiou':
print("Vowel")
else:
print("Consonant")
π§ Explanation:
- Takes input from user.
- Works for both uppercase and lowercase.
π Best for: Interactive programs.
πΉ Method 3 – Using Function
def check_vowel(char):
if char.lower() in 'aeiou':
return "Vowel"
else:
return "Consonant"
print(check_vowel('e'))
π§ Explanation:
- Function makes code reusable.
- Returns result instead of printing directly.
π Best for: Structured programs.
πΉ Method 4 – Using Lambda Function
check = lambda c: "Vowel"
if c.lower() in 'aeiou'
else "Consonant"
print(check('b'))
π§ Explanation:
- Short one-line function.
- Uses inline if-else.
π Best for: Quick checks.
⚡ Key Takeaways
- Use in keyword for easy checking
- Convert to lowercase using .lower()
- Always validate user input
- Vowels = aeiou
π‘ Pro Tip
Try extending this:
- Count vowels in a string
- Check vowels in a sentence
- Build a mini text analyzer
Keep going π


0 Comments:
Post a Comment