Day: February 28, 2021

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

(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 a dictionary …

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

(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 Coding Tutorials …

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

(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 3 Green …

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

(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 a Python …

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.

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

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)

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

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

(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 a Python …

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

(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 in the …

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

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