Find the output of the following Python code. Assume data.bin does not exist…
2026
Find the output of the following Python code. Assume data.bin does not exist initially.
import pickle
def BinaryMysteryIn():
Info = {"A": 100, "B": 200}
Colors = ["Red", "Green"]
# First Write
with open("data.bin", "wb") as F:
pickle.dump(Info, F)
pickle.dump(Colors, F)
def BinaryMysteryOut():
with open("data.bin", "rb") as F:
Obj1 = pickle.load(F)
Obj2 = pickle.load(F)
print(type(Obj1).__name__)
print(Obj2[0])
print(len(Obj1))
try:
pickle.load(F)
except EOFError:
print("EndReached")
BinaryMysteryIn()
BinaryMysteryOut()
Attempted by 15 students.
Show answer & explanation
Function
BinaryMysteryIn()writes two objects usingpickle.dump()⇒ a dictionary and a list into binary file.BinaryMysteryOut()reads them sequentially usingpickle.load().First object (
Obj1) is a dictionary ⇒type(Obj1).__name__⇒ dictSecond object (
Obj2) is a list ⇒Obj2[0]⇒ Redlen(Obj1)⇒ dictionary has 2 keys ⇒ 2Third
pickle.load()raises EOFError as file ends, handled byexceptblock.Final Outputs:
dict
Red
2
EndReached