Locked lesson.
About this lesson
How to assign items to variables (and other things).
Exercise files
Download this lesson’s related exercise files.
Assignment Operators.docx60.1 KB Assignment Operators - Solution.docx
58.8 KB
Quick reference
Assignment Operators
Assignment operatros "assign" things to other things.
When to use
Use them for assinging values to variables and other things.
Instructions
There are several assignment operators. Here's a list:
= Default
+= Add and Assign
-= Subtract and Assign
*= Multiply and Assign
/= Divide and Assign
**= Exponent and Assign
%= Remainder and Assign
Hints & tips
- Assignment Operators assign things!
- 00:04 In this video I want to talk about assignment operators.
- 00:07 In the last video we talked about variables, and
- 00:09 to create a variable we just name it and then put this equal to and
- 00:12 then whatever we want to put into the variable.
- 00:15 Well, this equal to is an assignment operator.
- 00:18 So we are assigning the string John Elder to this variable.
- 00:24 If you remember a couple of videos ago, we had comparison operators and
- 00:29 one of them was equal to.
- 00:31 So 5 is equal to 5.
- 00:33 The reason why we had double equal to signs there is because a single equal to
- 00:37 sign is an assignment operator, we're assigning this to this variable.
- 00:42 Here, we don't want to assign anything, we're not assigning 5 to 5,
- 00:46 we're comparing, and so we use double equal to.
- 00:49 That's just a little interesting bit of trivia there.
- 00:51 I wanted to mention since I mentioned it a couple of videos ago, but this is
- 00:54 the assignment operator, very important, it's not the only assignment operator.
- 00:57 There's a lot of other ones.
- 00:58 So let's go through here and make a list Assignment Operators.
- 01:03 You're going to notice a theme assignment operators, comparison operators,
- 01:08 these operators,
- 01:09 sort of a computer sciencey term that you're going to see over and over again.
- 01:14 So obviously the equal to sign, that's our main assignment operator.
- 01:18 There's a bunch of other ones too.
- 01:19 So we can go +=.
- 01:21 We can go -=.
- 01:23 We can go *=, /=, %=, **=.
- 01:29 Basically our math operators, we can turn them into assignment operators.
- 01:33 So what in the world does this mean?
- 01:35 Let's come up here and let's go price = $5, all right?
- 01:41 So puts price.
- 01:44 If we save this and run it obviously, it's just going to output 5.
- 01:47 Well, what if we want to change that and we want to add $3?
- 01:52 Well, normally you would have to go price = price + 5, what did we say, $3.
- 01:59 So if we puts price now, save this and run it, we see the first time,
- 02:04 it's 5, and then it becomes 8.
- 02:06 Well, this is an awful lot of stuff to write, and
- 02:09 whenever you're writing computer code, you want to be as elegant as possible.
- 02:13 You want to write as little code as possible, mostly because we're all lazy
- 02:17 and we just don't like to type, but also because it's easier to read as you go on.
- 02:20 So an assignment operator would work great here.
- 02:23 And to do that we just price += 3.
- 02:27 So what we're doing here is we're assigning and adding.
- 02:31 We're adding and assigning.
- 02:33 So the adding sign is first, followed by the assigning operator, so add and assign.
- 02:38 So we take our price which is 5, we add 3, and then assign that total back to price.
- 02:44 So 5 becomes 8 if we've done this correctly.
- 02:48 And sure enough, 5 and 8, change this to 2, 5 and 7.
- 02:54 So, like I've said, we can do this with all of our math operators,
- 02:58 we can subtract and assign just as easily. 5 and 3, and do all the rest.
- 03:02 So those are assignment operators, very useful.
- 03:04 Like I said, I mean just look at this right here,
- 03:09 price = price - 2 or price -= 2.
- 03:13 This is just much less stuff to type.
- 03:15 That's very useful for a lot different reasons.
- 03:17 So those are assignment operators.
- 03:19 In the next video, we're going to look at getting the user input.
Lesson notes are only available for subscribers.