Welcome back. So, we've seen how to assign variables to different values,
for example, on line one here,
we have code that assigns the variable x to the value six.
What that does is,
you can imagine a variable's values table,
that says x now points to six.
So, what that means is that,
after we've assign x to six on line one,
if we print out the value of x on line two,
then that should print out six,
because Python asks what's the value of x?
It looks in this variable's values table,
it sees that x points at six and it gets six.
Now, what happens if we say something like on line three,
x equals x plus one?
Now, on first blush,
this might look almost like a contradiction,
but the way that Python evaluates this expression is,
it first computes the value of this expression
and then it takes the value of this expression and assigns it to the new value for x.
So, in other words, what Python is first going to do is,
it's going to ask what's the value of x?
Python is going to get six,
and then it takes that and adds one to it.
So, the value of this expression,
x plus one is going to be seven.
So, all Python does,
is it computes the value of x plus one to which we get
seven and then it puts that as the new value for x.
So, x is no longer six,
x is now seven.
So, now when we print x on line four,
then this prints out seven.
So, if we run our code,
you will see that when we print out the value of x on line two we get six, but then,
after reassigning x to be x plus one on line three,
then when we print out x on line four,
then we print out seven.
Now, this kind of operation where we say x equals x plus one
or whatever x's previous value was,
we want to add one to it and reassign that to x.
That's actually really common and for that reason,
Python includes a shortcut for incrementing and decrementing like that.
So, let's run through this code.
So, on line one,
we assign x to have the value six and then we
print out the value of x which is going to print out six.
On line three, we have this special syntax where we say x plus equals three.
X plus equals three is a shortcut for saying x equals x plus three.
Now, it allows us to just say the name of the variable x only once here.
So, it's a little bit shorter in
less variables especially if we have much longer variable names.
So, again, this increments x by three and reassigns it.
So, if we have our variables values table here.
On line one we have assigned x to be six and then we print out that.
On line three, we say x equals x plus three by saying x plus equals three.
So, x is no longer six x instead becomes nine.
Then, when we print out x this prints out nine,
then we can do the same thing with subtraction.
So, we can say x minus equals one as a shortcut for saying x equals x minus one,
and so that's going to take x from nine and it's going to
reassign it to the value eight and now when we print out the value of x,
then we are going to print out eight.
So, you can see x goes from six to nine to eight.
So, we can do reassignment as many times as we want.
So, we can say s equals one,
and then we can add two to it,
then we can add three to that,
and four to that etc.
If I run this code, you'll see the different values of s. So,
s starts out as one,
when we add two to that it becomes three,
when we add three to that it becomes six,
when we add four to that it becomes ten and so on.
Later on in this course,
you will find a much shorter way to actually do something like this.
Now, let's run through some multiple choice questions,
what gets printed out when the following statements execute?
So, here, we assign x to be 12.
So, x has the value 12 and then we say x equals x minus one.
So, the value of this expression we replace x with 12 and we
subtract one from 12 to get 11 and we take 11 and that's x's new value.
So, x is no longer 12,
x is now 11.
So, when we print out x we are going to print out 11 or C. Next question,
what gets printed when the following statements execute?
So, here we first assign x to be 12.
Then we say, x equals it's previous value minus three,
sin x goes from 12 to nine.
Then we say it's previous value plus five.
So, x goes from nine to 14.
So, then we say x equals x plus one,
so it goes from 14 to 15.
So, x ends up with the value 15 or C. Next question,
construct code that will result in the value 134 being printed.
So, the first thing that we want to do,
is we want to assign an initial value for bank balance.
If I try to put this first,
then Python when trying to evaluate the value of
this expression would say that it didn't find a variable named mybankbalance.
So, I know that this line can't come first.
Here, I can do the assignment first,
so I can assign my bank balance to 100 and then I can reassign it like I do here.
So, this looks valid and by the end of running these two lines,
then mybankbalance is going to have the value 134,
and then if we print out the value of my bank balance on line three,
then we're going to print out 134.
Next question, which of the following statements are equivalent?
So, I see this statement reassigns x to it's previous value plus y.
This statement assigns y instead of x.
So, I know that this can't be equivalent to
statement A. X plus equals x plus y looks like it's similar to statement A,
but what that's really saying because we have plus equals here,
is x equals x plus x plus y.
So, here we have that extra x plus,
so that's not equivalent to statement A,
but statement D is.
Then statement E just won't work.
So, statement's A and D are equivalent.
That's all for now until next time.