Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase)of the places whose names are longer than 5 characters.

For example, Consider the following dictionary

PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}

The output should be:

LONDON

NEW YORK

Answer

Answer by student

Python Program to print elements of a Dictionary with more than 5 characters - Teachoo.png

Detailed answer by teachoo

The question asks us to write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase) of the places whose names are longer than 5 characters. The function should perform the following steps:

  • Loop through the values of the dictionary using a for loop. The values() method returns a view object that contains the values of the dictionary.
  • Check if the length of the place name is greater than 5 using an if statement. The len() function returns the number of characters in a string.
  • P rint the place name in uppercase using the print() function. The upper() method returns a copy of the string with all characters converted to uppercase. 

The final code for the function is as follows:

Python Program with comments to print elements of a Dictionary with more than 5 characters - Teachoo.png

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.