r/PythonLearning • u/clcodingupdates • 8d ago
Python Quiz of the Day.
x = 5
print((x := x + 3) * (x := x - 2))
What will be the output?
A) 24
B) 36
C) 48
D) 30
1
u/MasterpieceBusy7220 7d ago
Stop posting this shit bro you should be embarrassed with yourself
1
1
1
0
u/bra1n_gym 8d ago
That walrus operator let's x change value. first it was 5, then it becomes 8 so (5 + 3) * (8 - 2) = 48.
1
u/RNG_HatesMe 8d ago
Yep, the only real complication here is to figure out if x is updated *after* or *before* it's printed. Clearly here it's *before*, but some other languages do it differently. Specifically in c you have incrementors x++ and ++x, the former adds 1 to x *after* the operation, the latter does it before.
6
u/ConclusionForeign856 8d ago
Actual quiz of the day is "why should you never write this like that"