Question

write a program in python for following x = { 'a' : 1 , 'b':2 ,...

write a program in python for following

x = { 'a' : 1 , 'b':2 , 'c':3, 'd':4 }

For the following key,value pairs of dictionary display those which are even values i.e for which the value is even.

i am trying like this but getting confused.

(k:v for k,v in x.items() if v%2 != 0

Homework Answers

Answer #1

#Python Code:-

def main():
  
# Given dictionary
x = { 'a' : 1 , 'b':2 , 'c':3, 'd':4 }
  
dict1={}
  
# Traversing dictionary to find out the even values and then add to new dictionary called dict1
for key,value in x.items():
if value%2==0:
dict1[key]=value
  
# Printing elements of dictionary having even value.
print("The items having even value in dictionary: ")
print(dict1)

if __name__=="__main__":
main()
  
  
  

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python programming Write a program that prompts the user to input the three coefficients a, b,...
Python programming Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equationax2+bx+c= 0.The program should display the solutions of this equation, in the following manner: 1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with4 digits printed out after the decimal place. 2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solutions, each displayed with4 digits printed...
Write a program in python reverse each and every word of the sentence using function taking...
Write a program in python reverse each and every word of the sentence using function taking string as input parameter from user and passing that string into function and also explain the logic ======================================= input: 'Exam is gonna be good' output: 'maxE si annog eb doog' ================ i am trying like this but getting confused '.'join([w::-1] for w in s.split()]) i need more focus on explanations because it is making me confuse i am not clear with concepts so please...
Which of the following strings P is a Python program. (b) P = “def f(x): x...
Which of the following strings P is a Python program. (b) P = “def f(x): x = 'am I a Python program?'” (d) P = “def f(x,y,z): return y” (e) P = “def f(x): return y” For each of the following Python programs P and input strings I, give the output P(I), (f) P = “def f(x): return str(len(x+x+'x'))”, I = “GAGAT” (g) P = “def f(x): return str(len(x))”, I=P (h) P = “def f(x): return str(1/int(x))”, I = “0”
Write a Python program to create a dictionary from two lists without losing duplicate values. If...
Write a Python program to create a dictionary from two lists without losing duplicate values. If there is more values in the key list, then provided key should return an empty set if there is no match. (Hint: use defaultdict) Example: class_list = ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII','Class-IX'] id_list = [1, 2, 2, 3] Output: assuming you store the values in a data structure named temp print(temp["Class-V"]) # {1} print(temp["Class-IX"]) # set() can you please try and do on google colab
10. Which of the following statements about python dictionary is correct? A. You can have different...
10. Which of the following statements about python dictionary is correct? A. You can have different types of data as keys in a dictionary. B. You can have variables as keys in a dictionary. C. The values (key-value pairs) in a dictionary must be the types of int or float. D. You cannot have a list as key in a dictionary.
1. Write a program in python for the following: (a) Find f(x)=(e-sinx+esinx)/2 for different values of...
1. Write a program in python for the following: (a) Find f(x)=(e-sinx+esinx)/2 for different values of x F(x)= for different values of x (b) f(x)=log(ex)-sin2x+cos(logx) Find f’(x) and f’(10)
Write a Python program to ask how many elements do users want to create in a...
Write a Python program to ask how many elements do users want to create in a list, then let the user create 2 lists with the number of elements previously entered. Then create and display all combinations of letters, selecting each letter from a different key in a dictionary. Example: How many elements? 2 List 1 = ['a', 'b'] List 2 = ['c', 'd'] Output: ac ad bc bd
Hello, I am trying to write a program in python that will determine if a sentence...
Hello, I am trying to write a program in python that will determine if a sentence has every letter in the alphabet, regardless if it is upper or lower case. The program needs to indicate if all letters are present or not, and print out the missing letters if there are any. Thanks for any help in advance.
Python Programming 1. Write the following Python expressions in mathematical notation. a. dm = m *...
Python Programming 1. Write the following Python expressions in mathematical notation. a. dm = m * (sqrt(1 + v / c) / sqrt(1 - v / c) - 1) b. volume = pi * r * r * h c. volume = 4 * pi * r ** 3 / 3 d. z = sqrt(x * x + y * y) 2. What are the values of the following expressions? In each line, assume that s = "Hello" t =...
Python The following program contains a number of minor typos. Write the correct code and comments...
Python The following program contains a number of minor typos. Write the correct code and comments about what was wrong. The type function returns the type for the value specified as a parameter. lex = {} lex{’a’} = ’hej’ lex[’b’] = [5 9] for k in lex.keys: print(’Information for key {k}’) print f’Val: {lex[k]}, Type: {type(lex[k])}’