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
MyLab Programming with Pearson eText -- Access Code Card -- for Starting Out with Python (My Programming Lab)
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)
- The get(key) function for a dictionary will return _____ if the key does not exist. In python. A. “ B. {} C. “Null” D. None arrow_forwardPYTHON PROGRAMMING Given:lunch_order = {'food': 'pizza', 'price':9.99 } 1) Update the value of 'price' from 9.99 to 6.99 in the dictionary lunch_order.2) Access the values in the dictionary lunch_order to print the following.Make sure to precisely match the output format below. Example OutputToday's lunch special: Pizza $6.99.arrow_forwardExercise: Least Dictionary Value Description In this exercise you will find the value in a dictionary with the least value. Least means that the value is less than or equal to all other values in the dictionary. Remember that the 2 Assistant Object inspectorarrow_forward
- 1. Course informationWrite a program that creates a dictionary containing course numbers and the room numbers ofthe rooms where the courses meet. The dictionary should have the following key-value pairs:Course Number (key) Room Number (value)CS101 3004CS102 4501CS103 6755NT110 1244CM241 1411The program should also create a dictionary containing course numbers and the names of theinstructors that teach each course. The dictionary should have the following key-value pairs:Course Number (key) Instructor (value)CS101 HaynesCS102 AlvaradoCS103 RichNT110 BurkeCM241 LeeThe program should also create a dictionary containing course numbers and the meeting timesof each course. The dictionary should have the following key-value pairs:Course Number (key) Meeting Time (value)CS101 8:00 a.m.CS102 9:00 a.m.CS103 10:00 a.m.NT110 11:00 a.m.CM241 1:00 p.m.The program should let the user enter a course number, then it should display the course’s roomnumber, instructor, and meeting time.arrow_forwardExercise: empty_dictionary Description In this exercise, your function will receive no parameters. It will create an empty dictionary and return it. Function Name empty_dictionary Parameters None Return Value An empty dictionary. Examples empty_dictionary() > 0)arrow_forwardJavascript Write a function named create_kv that does not have any parameters. Your function should return a new object/dictionary containing the following key-values pairs: "moral": 9, "household": -1, and "delay": 2.arrow_forward
- Python Programming Assignment: Create a dictionary that takes a basketball team's information (that includes team's name and points) as parameter, creates a teams_dict and returns it. Also, create a parameter for write_file parameter that opens current results text file in write mode and writes the dictionary information to file as per format and closes the file.arrow_forwardThe following statement creates an empty dictionary: mydct = {} True or Falsearrow_forwardExercise: Check Monster Character Exists Description In this series of exercises, you will create functionsto create, modify and examine dictionaries thatrepresent characters in an animated film, and thecast members who voice the characters. Thekeys of the dictionary will be character names.The values in the dictionary will be voice actornames. For this exercise, you will create a function thatchecks if a character is already in the dictionary. Files • monsterfunctions.py : set of functions to work with monster cast dictionaries. Function Name has_character Parameters • monsters: a dictionary • character: a string, the name of a character Action Checks if character is a key in monsters. Return Value True if character is a key in monsters, otherwise False. Examples monsters = create_monster_cast()monsters = add_cast_member(monsters, "Mike", "William Crystal")has_character(monsters, "Mike") -> Truehas_character(monsters, "Sully") -> Falsearrow_forward
- Assume 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_forwardCreate 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_forwardOverview This program will have you use a loop to prompt a user for three foods and their country of origin and then store them in a dictionary called food_dict). Then, it will prompt the user for a dish that they like and then will tell them the country of origin. Expected Output Example 1 What is good to eat? Tacos What country is that from? Mexico What is good to eat? Pho What country is that from? Vietnam What is good to eat? Burgers What country is that from? America What dish do you like? Burgers Burgers is from America Example 2 What is good to eat? Pizza What country is that from? Italy What is good to eat? Spaghetti What country is that from? Italy What is good to eat? Gelato What country is that from? Italy What dish do you like? Pizza Pizza is from Italy Specifications • You should submit a single file called M4A2.py • It should follow the submission standards outlined here: Submission Standards • Your program should start by defining an empty dictionary called food_dict •…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education