(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to add a key to a dictionary. Sample Solution: Python Code: d = {0:10, 1:20} print(d) d.update({2:30}) print(d) Sample Output: {0: 10, 1: 20} {0: 10, 1: 20, 2: 30} Write a Python program to add a key to …
Day: February 27, 2021
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to sort (ascending and descending) a dictionary by value. Sample Solution: Python Code: import operator d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} print(‘Original dictionary : ‘,d) sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1))) print(‘Dictionary in ascending order by …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to create a 3X3 grid with numbers. Sample Solution: Python Code: nums = [] for i in range(3): nums.append([]) for j in range(1, 4): nums[i].append(j) print(“3X3 grid with numbers:”) print(nums) Sample Output: 3X3 grid with numbers: [[1, 2, 3], [1, …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to round every number of a given list of numbers and print the total sum multiplied by the length of the list. Sample Solution: Python Code: nums = [22.4, 4.0, -16.22, -9.10, 11.00, -12.22, 14.20, -5.20, 17.50] print(“Original list: “, …