Python Function Program — Display Book Names Starting with a Consonant (Dictionary Program with Output)
In this Python program, we will write a function dispBook(BOOKS) that takes a dictionary as an argument and displays the names of those books in uppercase whose names start with a consonant.
This program is useful for Class 12 Computer Science (083) – Python Functions & Dictionary Programs.
📌 Question
Write a function dispBook(BOOKS) in Python, that takes a dictionary BOOKS as an argument and displays the names in uppercase of those books whose name starts with a consonant.
Example dictionary:
BOOKS = {1 : "Python", 2 : "Internet Fundamentals",
3 : "Networking", 4 : "Oracle sets",
5 : "Understanding HTML"}
Expected Output:
PYTHON NETWORKING
🧠 Algorithm / Logic
- Traverse all values (book names) in the dictionary
- Extract the first character of each name
- Check whether it is a consonant (not a vowel)
- If yes → print the book name in uppercase
- Ignore book names starting with vowels (A, E, I, O, U)
🧾 Python Program — Function Definition
def dispBook(BOOKS):
vowels = "AEIOUaeiou"
for key in BOOKS:
name = BOOKS[key].strip()
if name != "" and name[0] not in vowels:
print(name.upper(), end=" ")
▶️ Sample Call & Output
BOOKS = {
1 : "Python",
2 : "Internet Fundamentals",
3 : "Networking",
4 : "Oracle sets",
5 : "Understanding HTML"
}
dispBook(BOOKS)
Output:
PYTHON NETWORKING
📝 Explanation of Code
vowelscontains all vowel charactersfor key in BOOKS:loops through dictionary keysstrip()removes extra spacesname[0] not in vowelschecks if the first letter is a consonantupper()converts book name to uppercaseend=" "prints names in the same line
⚠️ Common Mistakes Students Make
❌ Checking consonant incorrectly
❌ Forgetting to strip spaces
❌ Using lowercase-only comparison
❌ Printing dictionary keys instead of values
💡 Practice Exercises (Try Yourself)
1️⃣ Modify the function to print names that start with a vowel
2️⃣ Store consonant-starting books in a list and return it
3️⃣ Count how many book names start with consonants
🔗 Related Python Programs
- Dictionary traversal programs
- String and function-based programs
- Programs using conditions and loops
🎯 Conclusion
This program helps students understand:
- Dictionary traversal
- String processing
- Function definition
- Conditional logic
It is an important CBSE Class 12 Python function program frequently asked in exams and practical files.
In every board examination cbse must asked the programming questions based on CBSE Class 12 Functions, File Handling, Stack and Python MySQL connectivity Questions