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
36
14
134
19.5K
Sergei Kotov
Sergei Kotov@kotov_dev·
@PythonPr Answer: B) -5 Step by step: › Start: a=10, b=5 › a, b = b, a swaps them → a=5, b=10 › print(a - b) → 5 - 10 = -5 For beginners: This is Python's elegant way to swap! No temp variable needed. Technically, the tuple (b, a) gets unpacked into variables a, b in order.
English
0
0
25
2.1K
Earnest Codes
Earnest Codes@Earnesto037·
@PythonPr The correct answer is B) -5. This code demonstrates Python's elegant way of swapping variables using tuple unpacking. Understand: Initialization: a is 10, b is 5. The Swap (a, b = b, a): Python evaluates the right side first, creating a temporary tuple (5, 10).
English
1
0
6
590
Muhammad Yusuf
Muhammad Yusuf@quarksci·
@PythonPr The "magic" line secret is : a, b = b, a -> means: "new a = old b" and "new b = old a". Swap the variables and that called <tupple unpacking>
English
0
0
3
664
The root of infinity
The root of infinity@parth_hirpara05·
@PythonPr Ans-->B) -5 Key concept Python evaluates the right-hand side first, creates a tuple (b, a), and then assigns it to (a, b) that’s why swapping works without a temp variable.
English
0
0
3
510
Kamal Gurjar
Kamal Gurjar@KamalGurjar8·
@PythonPr Answer: B) -5 Because a, b = b, a swaps the values in one line (tuple unpacking). So after swap: a = 5, b = 10 → print(a - b) = 5 - 10 = -5
English
0
0
3
1K
Tanmay Newatia
Tanmay Newatia@thetanmaydoes·
@PythonPr B: -5 as the variables are interchanged on the third line.
English
0
0
2
694
Jenny
Jenny@JennyTheDev·
@PythonPr B) -5 a, b = b, a swaps them simultaneously, so a becomes 5 and b becomes 10. 5 - 10 = -5 Python's tuple unpacking is elegant until you forget it evaluates the right side first and your entire debugging session was for nothing 😂
English
0
0
2
1K
claveprep
claveprep@claveprep·
@PythonPr D) Error is the only correct answer if you value your sanity 😂 Variable swapping in Python is the kind of magic that makes you question reality. Beautiful AND dangerous!
English
0
0
1
261
Himal
Himal@himalbhattaraix·
@PythonPr B) -5 'a' and 'b' are swapped, so 'a = 5' and 'b = 10'
English
0
0
0
136
Priyanshu Jaiswal
Priyanshu Jaiswal@Priyanshu_1684·
@PythonPr B. -5 a = 5 and b = 10 after swapping the values, so a-b = 5-10, which is equal to -5
English
0
0
0
15
Python Tech
Python Tech@PythonTech43716·
@PythonPr Answer B) is the ✅️ Here we are using the concept of swap of two numbers, and a = 5 and b = 10 print(5-10) = -5 Answer.
English
0
0
0
52
Terrence
Terrence@terrenceeleven·
@PythonPr B) -5 Values swap without needing a temporary variable. AI .@PythonPr
English
0
0
0
177
Ranjan
Ranjan@Ranjanrgdev·
@PythonPr -5 because we are assaining a wit b value and b with a value
English
0
0
0
487
DocsAllOver
DocsAllOver@docsallover·
@PythonPr The classic Python swap! 🐍 Most languages need a third 'temp' variable, but Python handles this with tuple packing and unpacking in one line
English
0
0
0
28
Teilen