Page 281 - CTS - CSA TP - Volume 2
P. 281

COMPUTER SOFTWARE APPLICATION - CITS


















           TASK  5: Dictionary Comprehension
           # Dictionary comprehension

           squared_numbers = {x: x**2for x inrange(1, 6)}
           print(“Squared Numbers:”, squared_numbers)
           Explanation:
           •  A dictionary is created using dictionary comprehension to store squared numbers from 1 to 5.

           Output
           Related Exercises:
           1  Write a Python program to create an empty dictionary.
           2  Create a dictionary with three key-value pairs representing student information (name, age, grade).
           3  Access and print the value associated with the ‘age’ key in the student dictionary.

           4  Modify the student dictionary to update the age to 22.
           5  Add a new key-value pair to the student dictionary for the ‘course’ with the value ‘Computer Science’.
           6  Remove the ‘grade’ key from the student dictionary if it exists.
           7  Write a loop to iterate through the key-value pairs in the student dictionary and print them.
           8  Use the keys(), values(), and items() methods to display the keys, values, and key-value pairs of the student
              dictionary.






































                                                           266

                              CITS : IT & ITES - Computer Software Application - Exercise 128
   276   277   278   279   280   281   282   283   284   285   286