Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
16
7
95
9.3K
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr Answer: C Solution: The difference between `is` and `==` confuses many. Key is to understand how names and values work in python. Think of values as things. Like, say suitcases. And, names are like label tags put on these suitcases. The value is the actual "thing" (data) +
English
1
1
2
459
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr and the name is simply the label tag, that allows us to talk about the thing, ie, label tag is the variable name. A label is useful only if is unique, ie, a name can only refer to one value. However, you can attach multiple labels to the same suitcase, ie, +
English
1
0
0
60
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr a value can have multiple names. But a name can only refer to one value. Coming back to `is`... it checks whether two variables refer to the same value in memory. "Are these two labels on the same suitcase?" On the other hand, +
English
1
0
0
52
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr `==` doesn't care if the labels point to same or different value. It checks only for equality. "Is the content value of one variable equal to that of another?" Thus, `==` tests for equality. And `is` tests for identity. Coming back to the question, d1 and d2 are dicts +
English
1
0
0
49
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr having the same key-value pairs. Hence they are equal by ==. `d1 == d2` is True. However, they are initialized separately. So they are not two names of the same value. Hence, `d1 is d2` is False. Thus the answer is False True
English
0
0
0
45
Compartir