Assertion(A): A dictionary is a mapping of keys to values
Reasoning(R): A dictionary can be created by using curly braces and colon-separated pairs of keys and values
Answer:
Answer by student
a) Both A and R are true and R is the correct explanation for A.
Detailed answer by teachoo
-lock-
Both A and R are true and R is the correct explanation for A is correct because both the assertion and the reasoning are true statements about Python’s dictionary data type, and the reasoning correctly explains how to create a dictionary in Python.
- A dictionary is a mapping of keys to values , which means that it stores data in pairs of unique keys and corresponding values . The keys can be used to access or modify the values in the dictionary.
- A dictionary can be created in Python by using curly braces and colon-separated pairs of keys and values. The keys can be any immutable data type, such as strings, numbers, booleans, or tuples. The values can be any data type, including other dictionaries or lists.
For example, if we want to create a dictionary that stores some information about a person, we can write:
person = {"name": "Alice", "age": 25, "gender": "female", "hobbies": ["reading", "writing", "singing"]}
- In this example, we can see that we have used curly braces to enclose the dictionary, and colons to separate the keys and values. The keys are “name”, “age”, “gender”, and “hobbies”. The values are “Alice”, 25, “female”, and a list of strings. We can access or modify the values by using the keys inside square brackets.
print(person["name"]) # prints Alice
print(person["hobbies"]) # prints ["reading", "writing", "singing"]
person["age"] = 26 # changes the value of age to 26
person["city"] = "New York" # adds a new key-value pair to the dictionary
So, the correct answer is a) Both A and R are true and R is the correct explanation for A.
-endlock-