r/PythonLearning • • 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

0 Upvotes

10 comments sorted by

6

u/ConclusionForeign856 8d ago

Actual quiz of the day is "why should you never write this like that"

1

u/MasterpieceBusy7220 7d ago

Stop posting this shit bro you should be embarrassed with yourself

1

u/clcodingupdates 4d ago

ok. what you want to learn in Python?

1

u/MasterpieceBusy7220 3d ago

Well you could start by posting something that’s actually useful

3

u/RngdZed 8d ago

This AI slop account is so annoying. It posts everyday

1

u/Fluffy_white_fox 7d ago

Better question is who uses it like that? The answer is 48

1

u/osamas_den 3d ago

The "python" is no longer dynamic

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.

2

u/lurgi 7d ago

There’s also the question of whether Python evaluates the left and right hand sides of operators in a defined order or if it is, like C, undefined. It’s defined, first left, then right.