PYTHON:Vowel or Consonant

Check whether an alphabetic character is a vowel or a consonant.

BeginnerConditional Programs

What You'll Learn

  • Using string methods like lower() and isalpha()
  • Checking membership with in
  • Combining validation with core logic

Python Program Code

This program helps you to learn the fundamental structure and syntax of Python programming.

vowel-or-consonant.py
# Program to check whether a character is a vowel or consonant

ch = input("Enter a single alphabet character: ").lower()

if len(ch) != 1 or not ch.isalpha():
    print("Please enter exactly one alphabetic character.")
else:
    if ch in 'aeiou':
        print(ch, "is a vowel")
    else:
        print(ch, "is a consonant")
Terminal Output
Enter a single alphabet character: a
a is a vowel

Step-by-Step Breakdown

  1. 1Read a character and convert it to lowercase.
  2. 2Ensure it is exactly one alphabetic character.
  3. 3If it is in the vowel set, print vowel.
  4. 4Otherwise, print consonant.

Understanding Vowel or Consonant

We:

1.Validate that the input is exactly one alphabetic character.
2.Convert it to lowercase with .lower() to handle both upper and lower case.
3.Check membership in the string 'aeiou' to decide if it is a vowel.
4.Otherwise, it must be a consonant.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Practical Learning Notes for Vowel or Consonant

This Python program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.