Confused about this variable question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Confused about this variable question

It reads: spam=2 eggs=3 del spam eggs=4 spam=5 print (spam*eggs) The answer is 20 but I'm having trouble understanding how the final result is 20 when the only variable that was deleted and replaced was spam. Does this automatically cancel out the first variable for eggs? Thanks

19th May 2017, 3:30 PM
Kailani Bay
Kailani Bay - avatar
5 Answers
+ 1
so in the lines of code, redefining a variable replaces the value with the most recent definition. the previous lines dont compute when replaced later in the code. so 5*4 is all you have for the output function. which is to multiply. :)
19th May 2017, 4:46 PM
Imagined Self
Imagined Self - avatar
+ 1
Got it, thanks!
19th May 2017, 3:47 PM
Kailani Bay
Kailani Bay - avatar
+ 1
You dont need to use del, just asign new value. Thats why they called variables because they value can change.
19th May 2017, 4:00 PM
Szabó Gábor
Szabó Gábor - avatar
0
you asigned new value for eggs with the = operator
19th May 2017, 3:33 PM
Szabó Gábor
Szabó Gábor - avatar
0
Thanks! Okay, I guess in this case the del wasn't really necessary? I tried it without del and got the same answer.
19th May 2017, 3:40 PM
Kailani Bay
Kailani Bay - avatar