1 00:00:08,748 --> 00:00:10,070 Welcome back. 2 00:00:10,070 --> 00:00:12,460 If you write an expression in a computed value, 3 00:00:12,460 --> 00:00:15,910 then you probably want to store that value somewhere. 4 00:00:15,910 --> 00:00:19,160 Variables are useful because one, they allow us to store 5 00:00:19,160 --> 00:00:22,930 the values of expressions to be used later on in our programs. 6 00:00:22,930 --> 00:00:26,850 And two, because they allow us to give us human readable names for 7 00:00:26,850 --> 00:00:29,840 values that we can then reuse later on. 8 00:00:29,840 --> 00:00:34,470 So for example, in math class, if you ever have done geometry, 9 00:00:34,470 --> 00:00:36,640 then you might be familiar with pi. 10 00:00:36,640 --> 00:00:41,760 And when you use pi, you usually don't write out 3.14159, etc. 11 00:00:41,760 --> 00:00:44,720 Instead, you just use the symbol pi. 12 00:00:44,720 --> 00:00:47,310 So, pi is almost like a variable. 13 00:00:47,310 --> 00:00:49,820 It has a name and a value. 14 00:00:49,820 --> 00:00:54,520 And Python has an equivalent to variables in its programming language. 15 00:00:56,400 --> 00:00:59,420 So every variable has a name and a value. 16 00:01:00,420 --> 00:01:03,990 So in this program, we create three variables. 17 00:01:03,990 --> 00:01:05,955 The first variable is called message. 18 00:01:08,297 --> 00:01:10,632 And it's created on line one. 19 00:01:10,632 --> 00:01:15,870 So message has the value What's up, Doc, which is a string. 20 00:01:17,240 --> 00:01:25,269 And then we create a second variable whose name is n and whose value is 17. 21 00:01:25,269 --> 00:01:30,342 And then we create a third variable whose name is pi and 22 00:01:30,342 --> 00:01:33,540 whose value is 3.14159. 23 00:01:33,540 --> 00:01:36,165 Now, the first three lines are setting variables. 24 00:01:42,801 --> 00:01:47,530 And then we reference or use those variables in the last three lines. 25 00:01:55,955 --> 00:02:00,663 So if I run my code and then you'll see that I get three things printed out from 26 00:02:00,663 --> 00:02:02,438 lines five, six and seven. 27 00:02:05,872 --> 00:02:10,110 So when we print out message, then we print out What's up, Doc. 28 00:02:10,110 --> 00:02:13,271 When we print out n, then we print out 17. 29 00:02:13,271 --> 00:02:18,539 When we print out pi, then we print out the value 3.14159. 30 00:02:20,180 --> 00:02:24,550 Now, it's useful to be able to kind of the execution of programs like 31 00:02:24,550 --> 00:02:25,740 this in your head. 32 00:02:25,740 --> 00:02:29,730 And in order to do that, we have what's called a variables values diagram or 33 00:02:29,730 --> 00:02:30,740 a reference diagram. 34 00:02:32,060 --> 00:02:34,310 Now, to draw a reference diagram, 35 00:02:34,310 --> 00:02:39,690 what we do is we go line by line from top to bottom. 36 00:02:39,690 --> 00:02:44,640 So we start at the top, line one, and then go down and for 37 00:02:44,640 --> 00:02:49,390 every line, we're going to say what variable is set to what value. 38 00:02:49,390 --> 00:02:51,670 A reference diagram has two columns. 39 00:02:51,670 --> 00:02:59,176 It has variables, And values. 40 00:03:09,045 --> 00:03:14,530 So what we do is we go line by line from the top of the program to the bottom and 41 00:03:14,530 --> 00:03:18,620 we're going to say what variable is set to what value. 42 00:03:20,050 --> 00:03:23,180 So in this program on line one, 43 00:03:23,180 --> 00:03:28,240 then we set the variable message to the value the string What's up, Doc. 44 00:03:28,240 --> 00:03:32,269 So in our variables values diagram, we say the variable message, 45 00:03:36,707 --> 00:03:40,800 Is set to the value What's up, Doc. 46 00:03:52,250 --> 00:03:58,864 On line two, we set the variable n to the value 17. 47 00:04:01,799 --> 00:04:07,489 On line three, we set the variable 48 00:04:07,489 --> 00:04:12,992 pi to the value 3.14159. 49 00:04:15,513 --> 00:04:20,300 And now by the time we get to line five, when we say print out message. 50 00:04:20,300 --> 00:04:24,472 Then this expression is an expression whose value 51 00:04:24,472 --> 00:04:29,280 evaluates to whatever message it's pointing to. 52 00:04:29,280 --> 00:04:33,400 So when Python says print message, it looks at this and 53 00:04:33,400 --> 00:04:35,550 it says this is an expression, 54 00:04:35,550 --> 00:04:39,530 specifically it's an expression that references the variable message. 55 00:04:39,530 --> 00:04:44,410 And so the Python interpreter looks in this variables values diagram and 56 00:04:44,410 --> 00:04:47,170 it asks what's the value of message. 57 00:04:47,170 --> 00:04:52,750 We see a message here, and we see that messages value is What's up, Doc. 58 00:04:52,750 --> 00:04:56,600 And so what that means is that when we print out message, 59 00:04:56,600 --> 00:04:59,960 then we print out What's up, Doc. 60 00:04:59,960 --> 00:05:04,140 And same thing for when we actually look up the value of n. 61 00:05:04,140 --> 00:05:09,626 So when we say print out the value of n, then Python is going to first look 62 00:05:09,626 --> 00:05:14,671 in this variables values diagram and it sees that value of n is 17 63 00:05:17,864 --> 00:05:22,367 So what that means is that when we print out the value of n, we print out 17. 64 00:05:25,205 --> 00:05:28,997 And same thing for when we print out the value of pi, so 65 00:05:28,997 --> 00:05:34,350 Phyton looks in this reference diagram, sees that pi is 3.14159. 66 00:05:34,350 --> 00:05:40,489 And when we print out pi, then we print out 3.14159. 67 00:05:40,489 --> 00:05:43,739 Let me make this green just to be slightly clearer. 68 00:05:45,756 --> 00:05:48,803 Okay, So 69 00:05:48,803 --> 00:05:54,980 you might ask what happens if we actually overwrite the value of a variable. 70 00:05:54,980 --> 00:06:00,092 So what happens if we actually have a variable that I'll 71 00:06:00,092 --> 00:06:05,199 just call x and we set it to 5 and then I'll say print x and 72 00:06:05,199 --> 00:06:09,366 then I'm going to later on reset x to be 10. 73 00:06:12,422 --> 00:06:14,390 And I'll say print x, okay. 74 00:06:16,340 --> 00:06:19,376 So in order to determine what's going to happen here, 75 00:06:19,376 --> 00:06:22,614 then I'm going to write out my variables values diagram. 76 00:06:32,839 --> 00:06:38,263 So line 1 sets the value of the variable x to the value 5, 77 00:06:38,263 --> 00:06:43,009 and then on line 2 when we print out the value of x, 78 00:06:43,009 --> 00:06:46,290 then we're going to print out 5. 79 00:06:46,290 --> 00:06:51,070 On line 4 we set the value of x to 10, 80 00:06:51,070 --> 00:06:56,330 and so what that does is it says x no longer points to 5, 81 00:06:56,330 --> 00:06:58,830 so I'm going to erase this line. 82 00:07:01,000 --> 00:07:03,690 And I'm going to cross out 5. 83 00:07:03,690 --> 00:07:05,862 X now instead points to 10. 84 00:07:05,862 --> 00:07:10,460 And so X now points to 10. 85 00:07:10,460 --> 00:07:12,452 And here when we print out X, 86 00:07:12,452 --> 00:07:16,615 then we actually are going to print out 10 rather than 5. 87 00:07:16,615 --> 00:07:19,180 And you can see this if we were in our program. 88 00:07:19,180 --> 00:07:24,079 So again here, the first line or the first print statement prints out 5, 89 00:07:24,079 --> 00:07:27,093 the second print statement prints out 10. 90 00:07:29,920 --> 00:07:32,858 So every variable has a name and a value, and 91 00:07:32,858 --> 00:07:39,220 even though the value of a variable can be anything, the name has some restrictions. 92 00:07:39,220 --> 00:07:42,267 So every variable name has to start out with a letter. 93 00:07:55,944 --> 00:07:59,704 And variable names can only contain letters and numbers. 94 00:08:14,821 --> 00:08:19,200 So for example, x is a valid variable name because it starts out with the letter and 95 00:08:19,200 --> 00:08:21,180 only contains letters and numbers. 96 00:08:22,370 --> 00:08:24,242 Same thing with my variable. 97 00:08:28,033 --> 00:08:29,694 I can also have capital letters. 98 00:08:29,694 --> 00:08:35,947 So I can say XYZ, capital XYZ =100. 99 00:08:39,029 --> 00:08:41,370 And these are all valid variable names. 100 00:08:41,370 --> 00:08:46,482 So if I print out the value of XYZ, 101 00:08:46,482 --> 00:08:50,691 then it prints out 100. 102 00:08:50,691 --> 00:08:54,650 One thing to note is that Python is what's called case sensitive. 103 00:08:54,650 --> 00:08:59,437 And what that means is that the variable name XYZ in all capitals is 104 00:08:59,437 --> 00:09:03,278 different from the variable name xyz in lowercase. 105 00:09:06,539 --> 00:09:10,640 So if I print out XYZ in all capitals, then I get 100. 106 00:09:10,640 --> 00:09:18,440 If I print out lower case xyz, then I get this other variable value. 107 00:09:18,440 --> 00:09:24,047 If I print out xYz, then there is no variable that's called xYz and 108 00:09:24,047 --> 00:09:28,967 so I get an error saying that this variable is not defined. 109 00:09:32,979 --> 00:09:36,490 So variable names have to follow these two rules. 110 00:09:36,490 --> 00:09:40,300 Start with a letter and it can only contain letters and numbers. 111 00:09:40,300 --> 00:09:45,145 And what that means is that a variable name that 112 00:09:45,145 --> 00:09:50,248 starts out with a number, so if I say 2x = 50. 113 00:09:50,248 --> 00:09:54,217 This is not a valid variable name because it starts with a number rather than 114 00:09:54,217 --> 00:09:56,510 starting with the letter. 115 00:09:56,510 --> 00:09:58,980 And if I try to save and run my program, 116 00:09:58,980 --> 00:10:04,310 then I'm going to get a syntax error because I have a bad variable name here. 117 00:10:04,310 --> 00:10:08,980 I can have a variable called x2 because that starts with the letter, 118 00:10:10,130 --> 00:10:12,510 but I can't have a variable name 2x. 119 00:10:17,010 --> 00:10:21,385 One thing to not here is that Python also 120 00:10:21,385 --> 00:10:26,028 treats underscores like characters, so 121 00:10:26,028 --> 00:10:30,799 what that means is that I can have a variable 122 00:10:30,799 --> 00:10:35,063 myvariable and I can set it to 5.5. 123 00:10:35,063 --> 00:10:39,820 And if I print out the value of my variable, then I'll get 5.5. 124 00:10:39,820 --> 00:10:46,070 So because Python treats this underscore character as a letter, people 125 00:10:46,070 --> 00:10:52,180 often use underscores as a substitute for spaces in their variable names. 126 00:10:52,180 --> 00:10:56,180 So variables are useful tool that we'll use to one, 127 00:10:56,180 --> 00:10:59,540 remembering in expressions value to use later on. 128 00:10:59,540 --> 00:11:03,070 And two, to give a nice name to the value of an expression so 129 00:11:03,070 --> 00:11:05,280 that it's more human readable. 130 00:11:05,280 --> 00:11:05,940 Until next time.