You can use the __________ operator to determine whether a key exists in a dictionary.
a. &
b. in
c. ^
d. ?
The “in” operator is used to check that key exists in the dictionary.
Hence, the correct answer is option “B”.
Explanation of Solution
Operator “in”:
- In Python dictionary, an “in” operator can be used to check if a key exists in the dictionary.
- The “in” operator returns true if the key exists in the dictionary.
- Otherwise, the “in” operator returns false.
Syntax:
In Python, the “in” operator can be expressed as follows:
key in dictionary_name
In the above syntax,
- The “dictionary_name” refers to the dictionary.
- The “key” represents the key to be searched in the dictionary.
Example program:
# Create a dictionary Student
Student={'Joanne':'CS-1234','Chris':'CS-1235','Jacob':'CS-1236'}
# Check if Joanne exists in the dictionary
if 'Joanne'in Student:
# Print the value corresponding to key 'Chris'
print("Value corresponding to key Joanne: " +Student['Joanne'])
Explanation:
In the above Python code, the first line creates a new dictionary “Student” with key-value pair elements “Joanne:CS-1234”, “Chris:1235”, and “Jacob:CS-1236”. The second line checks if the key “Joanne” exists in the dictionary “Student”. The third line prints the value corresponding to “Joanne” if the key “Joanne” exists in the dictionary “Student”.
Explanation for incorrect options:
Operator “&”:
In Python, the “&” operator is used for bitwise “And”.
Hence, option “A” is wrong.
Operator “^”:
In Python, the “^” operator is used for bitwise “Exclusive Or”.
Hence, option “C” is wrong.
Operator “?”:
The “?” is not a built- in operator in Python.
Hence, option “D” is wrong.
Output:
Value corresponding to key Joanne: CS-1234
Want to see more full solutions like this?
Chapter 9 Solutions
Starting Out with Python (4th Edition)
Additional Engineering Textbook Solutions
Database Concepts (7th Edition)
Java How To Program (Early Objects)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Modern Database Management (12th Edition)
Software Engineering (10th Edition)
Starting Out with Java: Early Objects (6th Edition)
- Assume that d refers to a dictionary. Write a statement in Python that retrieves from the dictionary the value that is associated with the key 'answer', and assigns this value to the result variable.arrow_forwardA ValueError exception is raised if you try to retrieve a value from a dictionary using a nonexistent key. True or Falsearrow_forwardSuppose a dictionary named inventory exists. What does the following statement do?del inventory[654]arrow_forward
- Your program should prompt the user to enter names and salaries from the user. Create a dictionary called information which should be initialized by the values from the two lists that you created above. Your program should pass this dictionary to a function named insights which would calculate and print the names and salaries for each employee. Before printing the salary, your function should multiply the salary by a random number and then print it. [26] For example, name = Employee01, salary = 100 new_salary = salary * random number between 1 and 10arrow_forward‘Write a program to maintain grades for a class. The program will repeatedly prompt the user with the following menu, and use functions to perform the tasks ofuser's choice. Exit 1. Enter student info 2. Print grades for a student 3. Print class max, min, and average grades The program should use a dictionary to keep information about a student, including the student names and grades for three exams: midterm 1, midterm 2 andfinal exam. Assume the grades are positive integers between 0 and 100. The program should keep information about the students in a list for the entire class. ‘Assample run of the program is as follows. Exit 1. Enter student info 2. Print grades for a student 3. Print class max, min, and average grades Enter your choice: 1 Do you want to enter student info (y/n):Enter the student name: Adams Enter midterm 1 grade: 85 Enter midterm 2 grade: 87 Enter final exam grade: 90 Do you want to enter student info (y/n!Enter the student name: Mary Enter midterm 1 grade: 78 Enter…arrow_forwardAssume the following list exists: names = ['Chris', 'Katie', 'Joanne', 'Kurt'] Write a statement that uses a dictionary comprehension to create a dictionary named X in which each element contains the length of a name from the list as the key, and the corresponding name's initial character as its value.arrow_forward
- Create a static dictionary with a number of users and with the following values: First name Last name Email address Password Ask the user for: 5. Email address 6. Password Loop (for()) through the dictionary and if (if()) the user is found print the following: 7. Hello, first name last name you have successfully logged in 8. Notify the user if the password and email address are wrong 9. Additional challenge: if you want the program to keep asking for a username and password when the combination is wrong, you will need a while() loop. 10. Save the file as assignment03yourlastname.pyarrow_forwardYou can create an empty dictionary with dictionary() [ ] ( ) { } This data structure stores a collection of objects in an unordered manner where each object must be unique dictionary set tuple listarrow_forwardThe following statement creates an empty dictionary: mydct = {} True or Falsearrow_forward
- Which part of a dictionary element must be immutable?arrow_forwardCreate a dictionary (order_dict) that the key is the burger number, and the value is a list of the quantities. For example: order_dict which is {'1': [10, 3, 78], '2': [35, 0, 65], '3': [9, 23, 0], '4': [0, 19, 43], '5': [43, 0, 21]} Create a dictionary (total_order_dict) that the key is the burger number, and the value is the total of the quantities for that burger. For example: total_order_dict which is {'1': 91, '2': 100, '3': 32, '4': 62, '5': 64} Use "assert to test the values of the total_order_dict For example: expected_result_ototal_order_dict = {'1': 91, '2': 100, '3': 32, '4': 62, '5': 64} assert actual_result_ototal_order_dict['1'] == expected_result_ototal_order_dict['1'] , "The actual result is not the \same as expected result for the order number 1!" (and test the other elements) The function can be like: def test_sum(): #Testing assert actual_result_ototal_order_dict['1'] == 91, "The actual result is not the \ same as expected result for the…arrow_forwardreturn_dict() that takes the name of a CSV file as the input parameter and returns a dictionary, where each key is the Reporting_PHU_ID and the value is a list containing the following data. name of CSV file is "data23.csv"arrow_forward
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning