Tag Archives: python example

Python Example – Write a Python program to multiply all the items in a dictionary

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to multiply all the items in a dictionary.   Sample Solution:- Python Code: my_dict = {‘data1′:100,’data2′:-54,’data3’:247} result=1 for key in my_dict: result=result * my_dict[key] print(result) Sample Output: -1333800   Write a Python program to multiply all the items in …

Python Example – Write a Python program to sum all the items in a dictionary

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sum all the items in a dictionary.   Sample Solution: Python Code: my_dict = {‘data1′:100,’data2′:-54,’data3’:247} print(sum(my_dict.values())) Sample Output: 293   Write a Python program to sum all the items in a dictionary Free Machine Learning & Data Science …

Python Example – Write a Python program to iterate over dictionaries using for loops

Hits: 1 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to iterate over dictionaries using for loops.   Sample Solution Python Code: d = {‘Red’: 1, ‘Green’: 2, ‘Blue’: 3} for color_key, value in d.items(): print(color_key, ‘corresponds to ‘, d[color_key]) Sample Output: Red corresponds to 1 Blue corresponds to …

Python Example – Write a Python script to merge two Python dictionaries

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python script to merge two Python dictionaries.   Sample Solution: Python Code: d1 = {‘a’: 100, ‘b’: 200} d2 = {‘x’: 300, ‘y’: 200} d = d1.copy() d.update(d2) print(d) Sample Output: {‘x’: 300, ‘y’: 200, ‘a’: 100, ‘b’: 200}   Write …

Python Example – Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys.

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys.   Sample Solution: Python Code: d=dict() for x in range(1,16): d[x]=x**2 print(d) Sample Output: {1: 1, 2: …

Python Example – Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x)

Hits: 2 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x).   Sample Solution: Python Code: n=int(input(“Input a number “)) d = dict() for x in range(1,n+1): d[x]=x*x print(d) Sample Output: …

Python Example – Write a Python program to iterate over dictionaries using for loops

Hits: 3 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to iterate over dictionaries using for loops.   Sample Solution: Python Code: d = {‘x’: 10, ‘y’: 20, ‘z’: 30} for dict_key, dict_value in d.items(): print(dict_key,’->’,dict_value) Sample Output: y -> 20 z -> 30 x -> 10   Write …

Python Example – Write a Python program to check whether a given key already exists in a dictionary

Hits: 2 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether a given key already exists in a dictionary.   Sample Solution: Python Code: d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60} def is_key_present(x): if x in d: print(‘Key is present …

Python Example – Write a Python program to concatenate dictionaries to create a new one

Hits: 0 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to concatenate following dictionaries to create a new one.   Sample Solution: Python Code: dic1={1:10, 2:20} dic2={3:30, 4:40} dic3={5:50,6:60} dic4 = {} for d in (dic1, dic2, dic3): dic4.update(d) print(dic4) Sample Output: {1: 10, 2: 20, 3: 30, 4: …

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

Hits: 10 (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 …

Python Example – Write a Python program to create a 3X3 grid with numbers

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

Python Example – Write a Python program to round every number of a given list of numbers

Hits: 6 (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 …

Python Example – Write a Python program to insert an element at a specified position into a given list.

Hits: 13 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to insert an element at a specified position into a given list.   Sample Solution: Python Code: def insert_spec_position(x, n_list, pos): return n_list[:pos-1]+[x]+n_list[pos-1:] n_list = [1,1,2,3,4,4,5,1] print(“Original list:”) print(n_list) kth_position = 3 x = 12 result = insert_spec_position(x, n_list, …

Python Example – Write a Python program to extract a given number of randomly selected elements from a given list

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to extract a given number of randomly selected elements from a given list.   Sample Solution: Python Code: import random def random_select_nums(n_list, n): return random.sample(n_list, n) n_list = [1,1,2,3,4,4,5,1] print(“Original list:”) print(n_list) selec_nums = 3 result = random_select_nums(n_list, selec_nums) …

Python Example – Write a Python program to split a given list into two parts where the length of the first part of the list is given

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to split a given list into two parts where the length of the first part of the list is given.   Sample Solution: Python Code: def split_two_parts(n_list, L): return n_list[:L], n_list[L:] n_list = [1,1,2,3,4,4,5, 1] print(“Original list:”) print(n_list) first_list_length …