Locked lesson.
About this lesson
What are the different assignment operators and how are they used.
Exercise files
Download this lesson’s related exercise files.
Assignment Operators.docx58.8 KB Assignment Operators - Solution.docx
58.8 KB
Quick reference
Assignment Operators
Assignment Operators let us assing data to things.
When to use
Use them whenever you want to assign data.
Instructions
The main assignment operators are
- =
- +=
- -=
- *=
- /=
- **=
- %=
Hints & tips
- Assignment operators allow us to assign data
- They also allow us to do math and assign at the same time
- All of the math operators can become assignment operators
- 00:04 In this video, I want to look at assignment operators.
- 00:07 And we've kind already seen some assignment operators in action.
- 00:10 We had our variable first_name = John.
- 00:15 Right here, you were using an assignment operator, and
- 00:17 you probably didn't even know it.
- 00:18 It's the single = sign right here, is an assignment operator.
- 00:22 It's assigning the string John, into the variable first name.
- 00:26 So we're assigning it in.
- 00:29 And that's what that single = does.
- 00:31 So, any time you're using a list, a variable, a tuple,
- 00:35 a dictionary, number and you use this single = sign, you're signing.
- 00:39 Well that's pretty basic, pretty simple, you don't even really need to think of
- 00:42 this as an assignment operator, it's just blindingly obvious, right?
- 00:46 Well, that's true, and there are also some other assignment operators, and
- 00:49 those are what I would really want to focus on in this video.
- 00:51 What are the other assignment operators?
- 00:53 Well, a couple of videos ago, we looked at math operators.
- 00:55 And all of those math operators can be turned into assignment operators,
- 00:59 and you do that just by slapping an = sign next to them.
- 01:02 So you have +=, -=, *=,
- 01:06 /=, **= and %=, one equal to sign.
- 01:13 And so, these are all now assignment operators.
- 01:16 And what they're doing is adding and assigning, subtracting and assigning,
- 01:21 multiplying and assigning, and on, and on.
- 01:23 So, what does that mean?
- 01:24 Well, let's create a variable, and let's just call it num.
- 01:27 And I'm going to say, num = 41.
- 01:29 And we can print this out, you know how to do this, and it's just 41.
- 01:33 But what if we wanted to add 1 to our num?
- 01:37 Normally, you would just go, num = num + 1,
- 01:41 if we do this and run it we get 42.
- 01:44 because we're taking our num which is 41, we're adding 1 to it which is 42,
- 01:49 and we're taking that and assigning it back in to the variable num.
- 01:53 So from now on from line 9 down, anytime we print out num it's going to be 42.
- 01:58 Well, that's fine, you can do that, it's much easier just to go num +=1.
- 02:04 And if we save this and run it, we're going to get the same 42.
- 02:07 So, what would doing is adding in assigning, or subtracting and assigning.
- 02:12 And that's important because say we get rid of this, and
- 02:15 we just come down to our print statement and we go num + 1.
- 02:18 Well, if we save this and print it, we're still going to get 42.
- 02:21 But then if we come down below it and go num again,
- 02:25 the first time it's going to print 42.
- 02:28 The second time it's going to print 41.
- 02:30 Because right here, all we did was add,
- 02:32 we didn't then assign that back in to the variable.
- 02:35 So from here down, our num is still going to be 41,
- 02:38 even though we added 1 to it right here.
- 02:41 So, because of that, it's super useful to use this num += 1
- 02:46 assignment operator, or you could do subtraction as well.
- 02:52 Print this out 40, you do multiplication like said, all those math operators,
- 02:56 you can turn into assignment operators.
- 02:58 Let's change this to 2.
- 02:59 So 41 times 2 is going to be 82, and divide, and go through all of them.
- 03:03 So, super useful.
- 03:05 And you might be thinking well okay, that's fine, but
- 03:09 why don't we just go num = num + 1, instead of going
- 03:15 += 1.
- 03:16 Well you could do this, these two lines line 9 and 11, they end
- 03:20 up being the exact same thing, but this line here is just, it's easier to do.
- 03:24 It's much less stuff to write, and computer programmers coders.
- 03:28 We are notoriously lazy.
- 03:30 We hate typing stuff.
- 03:31 We hate writing things.
- 03:32 Anytime I can write less stuff, I'm going to do that.
- 03:35 And it makes the code easier to read more elegant.
- 03:38 We like elegance in programming and all that good stuff.
- 03:41 So, those are assignment operators, super useful.
- 03:43 You're going to use them all the time.
- 03:45 I mean, just think about it.
- 03:46 Like, if you have a shopping cart online and you're selling things,
- 03:49 if somebody wants to add a new item to their shopping cart,
- 03:51 you're going to want to increment they're running total.
- 03:54 You're going to use an assignment operator, to += that new total.
- 03:57 And just a zillion other things you can use this for.
- 03:59 So, super useful, super easy.
- 04:01 Those are assignment operators.
- 04:03 In the next video, we're going to look at lists.
Lesson notes are only available for subscribers.