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

COMPUTER SOFTWARE APPLICATION - CITS




              So, the complete code takes a string, filters out non-alphabetic characters, converts the remaining characters
              to uppercase, and creates a set of those uppercase characters. The output displays the original text and the
              resulting set of uppercase characters.
           Output:









           5  Dictionary  Comprehensions:
              Dictionary comprehensions allow you to create dictionaries. They use key-value pair expressions.


           TASK 7: Original list of fruits and their lengths
           # Original list of fruits and their lengths

           fruits = [‘apple’, ‘banana’, ‘orange’, ‘kiwi’, ‘grape’]
           fruit_lengths = {fruit: len(fruit) for fruit in fruits}
           # Print the original list
           print(“Original List of Fruits:”, fruits)
           # Print the dictionary created using a comprehension

           print(“Dictionary of Fruit Lengths:”, fruit_lengths)
           Explanation:
           1  fruits = [‘apple’, ‘banana’, ‘orange’, ‘kiwi’, ‘grape’]: Initializes a list of fruits.
           2  fruit_lengths = {fruit: len(fruit) for fruit in fruits}:
              •  This is a dictionary comprehension.

              •  for fruit in fruits: Iterates over each fruit in the list.
              •  {fruit: len(fruit)}: Creates a key-value pair in the dictionary, where the key is the fruit, and the value is the
                 length of the fruit.

           3  print(“Original List of Fruits:”, fruits): Prints the original list of fruits.
           4  print(“Dictionary of Fruit Lengths:”, fruit_lengths): Prints the dictionary created using the comprehension.
           Output:










           TASK 8: Temperatures in Celsius
           # Original list of temperatures in Celsius

           temperatures_celsius = {‘city1’: 25, ‘city2’: 30, ‘city3’: 22, ‘city4’: 18}
           # Dictionary comprehension to convert temperatures to Fahrenheit
           temperatures_fahrenheit = {city: (temp * 9/5) + 32 for city, temp in temperatures_celsius.items()}
           # Print the original temperatures
           print(“Original Temperatures (Celsius):”, temperatures_celsius)



                                                           295
 CITS : IT & ITES - Computer Software Application - Exercise 135  CITS : IT & ITES - Computer Software Application - Exercise 135
   305   306   307   308   309   310   311   312   313   314   315