ProgrammingHardTCS NQT · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? x = [1, 2, 3] y = [1, 2, 3] print(x == y, x is y)
- A. False False
- B. True False
- C. False True
- D. True True
Correct answer: B. True False
== compares values (equal contents), while is compares identity; x and y are two distinct list objects with the same contents.
More Python Programming questions
- What does the following Python 3 code print? print(2 10, 2 0.5, 10 // 3, -10 // 3, 10 % 3, -10 % 3)
- What does the following Python 3 code print? print(id([1, 2]) == id([1, 2]))
- What does the following Python 3 code print? try: a = (1, 2, 3) a[0] = 9 except TypeError: print("TypeError")
- What does the following Python 3 code print? try: d = {} print(d["missing"]) except KeyError: print("KeyError")
- What does the following Python 3 code print? try: s = "abc" print(s[10]) except IndexError: print("IndexError")
- What does the following Python 3 code print? try: x = int("12a") except ValueError: print("ValueError")