Day: February 27, 2021

Python Example – Write a Python program to sort (ascending and descending) a dictionary by value

(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 – Write a Python program to create a 3X3 grid with numbers

(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 – Write a Python program to round every number of a given list of numbers

(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: “, …