229k views
5 votes
Use other operators to change a variable

Python doesn't just come with += to easily change a
variable, we also get *- and /= :
=
my_var
=
my_var
-=
5 subtracts 5 and stores the result in my_var
my_var / 2 divides by 2 and stores the result in my_var-
my_var *= 4 multiplies by 4 and stores in my_var
==
15 stores the value 15 in my_var
? 1. What is the value of my_var in the example
instruction code above after all 3 changes?
D

1 Answer

3 votes

The code sequence modifies my_var using three operations:

  • Subtracting 5 from the current value of my_var.
  • Dividing the updated value of my_var by 2.
  • Multiplying the resulting value by 4.

What would be the final value?

The final value of my_var depends on its initial value.

Without knowing the starting value of my_var, it's impossible to determine its final value solely based on the sequence of operations provided: my_var -= 5, my_var /= 2, my_var *= 4.

User Bier Hier
by
7.9k points