Find the output of the following Python code. def MessageGame(Msg, Nums): Msg…
2026
Find the output of the following Python code.
def MessageGame(Msg, Nums):
Msg = Msg + "!"
Nums.append(100)
print("Func Msg:", Msg) # Output 1A
print("Func List:", Nums) # Output 1B
Text = "Hello"
MyList = [10, 20]
MessageGame(Text, MyList)
print("Final Msg:", Text) # Output 1C
print("Final List:", MyList) # Output 1D
Show answer & explanation
In function
MessageGame(Msg, Nums),Msgis an immutable (string) whileNumsis a mutable (list).Msg = Msg + "!"⇒"Hello"becomes"Hello!", but this is only a local change.Nums.append(100)⇒[10, 20] → [10, 20, 100](this modifies the original list globally).Outputs inside the function:
Func Msg: Hello!
Func List: [10, 20, 100]
After function call:
Textremains unchanged ⇒ HelloMyListis modified ⇒ [10, 20, 100]
Final Outputs:
Final Msg: Hello
Final List: [10, 20, 100]