1 00:00:00,000 --> 00:00:02,375 [MUSIC PLAYING] 2 00:00:02,375 --> 00:00:04,750 3 00:00:04,750 --> 00:00:06,970 SPEAKER: OK, now in this short, we're going 4 00:00:06,970 --> 00:00:09,970 to take a look at one other building block of our programs. 5 00:00:09,970 --> 00:00:11,830 In this case, variables. 6 00:00:11,830 --> 00:00:17,200 And variables are simply containers for some value that can change over time. 7 00:00:17,200 --> 00:00:20,260 So let's say I want to make a guessing game, where 8 00:00:20,260 --> 00:00:22,060 the user can guess some number. 9 00:00:22,060 --> 00:00:25,810 And I'll tell them if they guessed correctly or incorrectly. 10 00:00:25,810 --> 00:00:30,580 Now, it would be probably worthwhile to have some way of referring 11 00:00:30,580 --> 00:00:33,190 to the user's guess in my program. 12 00:00:33,190 --> 00:00:34,840 What number did they guess? 13 00:00:34,840 --> 00:00:37,900 And that is a great use case for a variable. 14 00:00:37,900 --> 00:00:40,870 So I want to make a variable in Python. 15 00:00:40,870 --> 00:00:43,450 I can do it by typing the variable's name first. 16 00:00:43,450 --> 00:00:46,000 So what's a good name for this variable? 17 00:00:46,000 --> 00:00:47,800 I might say something like just guess. 18 00:00:47,800 --> 00:00:51,580 So guess is the name of our variable, or our container for the value 19 00:00:51,580 --> 00:00:54,370 that you're going to give us so we can store over time. 20 00:00:54,370 --> 00:00:56,710 So I'll call this guess here. 21 00:00:56,710 --> 00:01:02,530 And now I want to assign this container called guess some value, some number 22 00:01:02,530 --> 00:01:04,879 to hold as the user's guess. 23 00:01:04,879 --> 00:01:08,840 So let's say the first guess by the user is 10. 24 00:01:08,840 --> 00:01:13,400 I could store that value, 10, by saying guess equals 10. 25 00:01:13,400 --> 00:01:17,600 And this equals sign doesn't necessarily say that guess is equal to 10 26 00:01:17,600 --> 00:01:21,020 now, but more so that we're going to assign this value 27 00:01:21,020 --> 00:01:26,180 10 to be inside the container that we're going to call guess. 28 00:01:26,180 --> 00:01:29,810 And now that I've done that, if I want to print out the guess, 29 00:01:29,810 --> 00:01:33,710 see what is inside this container, I can do that on line two. 30 00:01:33,710 --> 00:01:35,630 I could say, print guess. 31 00:01:35,630 --> 00:01:41,690 Now, I'll say python of guess.py, and I should see 10 printed back out to me 32 00:01:41,690 --> 00:01:43,430 on the screen. 33 00:01:43,430 --> 00:01:46,490 Now, variables come-- really powerful when we combine them 34 00:01:46,490 --> 00:01:48,080 with things like functions. 35 00:01:48,080 --> 00:01:53,480 So let's try to make our own function that can get a user's guess. 36 00:01:53,480 --> 00:01:57,350 Now, one way to do this is to define some new function. 37 00:01:57,350 --> 00:02:00,620 And I might call it maybe like get_guess. 38 00:02:00,620 --> 00:02:05,320 So I'll say def, define me a function, called get_guess. 39 00:02:05,320 --> 00:02:07,390 And it will take no inputs. 40 00:02:07,390 --> 00:02:13,270 But I'll then, inside this function, define myself a variable called guess. 41 00:02:13,270 --> 00:02:16,750 And I'll set it equal to 10 here, just like I did before. 42 00:02:16,750 --> 00:02:21,520 And now, I'll have that function return to me, as we saw in the return value 43 00:02:21,520 --> 00:02:23,680 short, this guess here. 44 00:02:23,680 --> 00:02:25,930 Return guess itself. 45 00:02:25,930 --> 00:02:31,450 Now, down below, I could say, print get_guess, just 46 00:02:31,450 --> 00:02:34,720 like this, which will run the get_guess function, 47 00:02:34,720 --> 00:02:37,180 and then print the return value, which in this case 48 00:02:37,180 --> 00:02:39,580 is this variable called guess. 49 00:02:39,580 --> 00:02:42,310 So I'll say python of guess.py. 50 00:02:42,310 --> 00:02:45,400 And I should see 10 again. 51 00:02:45,400 --> 00:02:49,360 Now, it would be not a great program if the user 52 00:02:49,360 --> 00:02:53,110 had to open up guess.py and change their guess every time. 53 00:02:53,110 --> 00:02:57,280 So ideally, we would actually let them type in their guess in the terminal. 54 00:02:57,280 --> 00:03:01,090 And there is a function for this, called input in Python. 55 00:03:01,090 --> 00:03:05,570 So I could set this variable, instead of being equal to the number 10, 56 00:03:05,570 --> 00:03:09,320 equal to whatever the user types in at the terminal. 57 00:03:09,320 --> 00:03:15,320 And I could say input, just like this, enter, enter a guess. 58 00:03:15,320 --> 00:03:21,050 So now, before we assign this variable called guess a value, 59 00:03:21,050 --> 00:03:23,390 we'll run the input function. 60 00:03:23,390 --> 00:03:27,500 We'll print this text, enter a guess, and whatever the user types in 61 00:03:27,500 --> 00:03:31,220 will be now stored in this variable called guess. 62 00:03:31,220 --> 00:03:32,360 So let's try it. 63 00:03:32,360 --> 00:03:35,090 I'll say python of guess.py. 64 00:03:35,090 --> 00:03:37,190 And now I'll type in 20. 65 00:03:37,190 --> 00:03:39,440 And I should see I printed out 20. 66 00:03:39,440 --> 00:03:43,640 So that means that the return value of get_guess is 20. 67 00:03:43,640 --> 00:03:48,590 What if I did python of guess.py again and I entered 5? 68 00:03:48,590 --> 00:03:52,700 Well, in that case, it seems like the return value of get_guess changes 69 00:03:52,700 --> 00:03:55,130 as I type in numbers here. 70 00:03:55,130 --> 00:03:57,530 So that's pretty handy for me. 71 00:03:57,530 --> 00:04:03,210 Now, what if I also had a way to compare the guess against some actual number 72 00:04:03,210 --> 00:04:03,710 here. 73 00:04:03,710 --> 00:04:07,130 Well, I could go ahead and define the main part of my program 74 00:04:07,130 --> 00:04:10,910 and get working on the functionality to check the number that the user actually 75 00:04:10,910 --> 00:04:11,690 gave us. 76 00:04:11,690 --> 00:04:15,260 So I'll say define the main function now. 77 00:04:15,260 --> 00:04:21,019 And inside of here, I'll maybe make a new variable called guess. 78 00:04:21,019 --> 00:04:22,520 Guess equals. 79 00:04:22,520 --> 00:04:24,680 And then say get_guess. 80 00:04:24,680 --> 00:04:27,500 So now what's going to happen is get_guess will run, 81 00:04:27,500 --> 00:04:32,270 and its value will be assigned to the guess variable here. 82 00:04:32,270 --> 00:04:34,610 Now, one question you might have is that it 83 00:04:34,610 --> 00:04:39,050 seems like I'm using these variables guess multiple times. 84 00:04:39,050 --> 00:04:41,750 And isn't that something I shouldn't be doing? 85 00:04:41,750 --> 00:04:45,200 Well, it turns out that because these variables are inside 86 00:04:45,200 --> 00:04:49,760 of different functions, they can be used in different contexts, in which case 87 00:04:49,760 --> 00:04:51,920 they will never refer to that same value. 88 00:04:51,920 --> 00:04:55,910 They can be separated, let's say, by these two functions. 89 00:04:55,910 --> 00:05:00,010 So now, I'll go ahead and I will run python of guess.py. 90 00:05:00,010 --> 00:05:02,510 Actually, before I do that, let me go ahead and actually run 91 00:05:02,510 --> 00:05:04,520 the main function down below. 92 00:05:04,520 --> 00:05:07,880 And let me try to run python of guess.py. 93 00:05:07,880 --> 00:05:09,140 And also, you enter a guess. 94 00:05:09,140 --> 00:05:10,490 I'll type 10. 95 00:05:10,490 --> 00:05:11,270 Nothing yet. 96 00:05:11,270 --> 00:05:13,580 But I haven't really printed anything over here. 97 00:05:13,580 --> 00:05:16,940 So why don't I go ahead and now print the guess I got from the user. 98 00:05:16,940 --> 00:05:20,450 And now, I see 10 overall. 99 00:05:20,450 --> 00:05:24,860 And again, this worked, because these two variables are inside 100 00:05:24,860 --> 00:05:26,780 of what we call different scopes. 101 00:05:26,780 --> 00:05:30,260 This variable is inside the scope of this function, get_guess. 102 00:05:30,260 --> 00:05:33,560 Whereas this guess variable is inside the scope of this function, 103 00:05:33,560 --> 00:05:35,630 called main. 104 00:05:35,630 --> 00:05:39,110 So let's go ahead and actually compare the number here. 105 00:05:39,110 --> 00:05:41,840 And the number I'm thinking of is number called 50. 106 00:05:41,840 --> 00:05:48,590 So I'll say, if guess is equal to 50, why don't I go ahead and print correct. 107 00:05:48,590 --> 00:05:50,150 Just like this. 108 00:05:50,150 --> 00:05:53,420 Or let's say maybe the guess isn't 50. 109 00:05:53,420 --> 00:05:55,130 In this case, I'll print-- 110 00:05:55,130 --> 00:05:56,510 I'll print incorrect. 111 00:05:56,510 --> 00:05:57,530 Just like that. 112 00:05:57,530 --> 00:06:01,370 So now, this is our program in its entirety. 113 00:06:01,370 --> 00:06:04,590 And this here simply is a question we're asking. 114 00:06:04,590 --> 00:06:08,310 If guess is equal to the number 50, we'll print out correct. 115 00:06:08,310 --> 00:06:11,010 If it's not, we'll print out incorrect. 116 00:06:11,010 --> 00:06:18,150 So now, I'll go ahead and I will run python of guess.py, hit enter. 117 00:06:18,150 --> 00:06:21,730 And I'll say, maybe I'm going to guess 50. 118 00:06:21,730 --> 00:06:25,070 But I get incorrect. 119 00:06:25,070 --> 00:06:25,690 Hmm. 120 00:06:25,690 --> 00:06:28,810 So it seems like I typed in the number 50, 121 00:06:28,810 --> 00:06:33,460 but when I went to check if guess equals this number 50, 122 00:06:33,460 --> 00:06:38,410 didn't seem to be correct-- didn't seem to see that it is that same number. 123 00:06:38,410 --> 00:06:42,640 Now, this is a bit of a tricky scenario if you're new to programming. 124 00:06:42,640 --> 00:06:46,300 But it's also one that kind of shows us the different kinds of variables 125 00:06:46,300 --> 00:06:47,410 we can have. 126 00:06:47,410 --> 00:06:49,900 Now, it turns out that in Python, there are 127 00:06:49,900 --> 00:06:52,210 many types of variables you can have. 128 00:06:52,210 --> 00:06:56,650 One of them being an integer, as in this number 50 here. 129 00:06:56,650 --> 00:07:00,040 And one of them being a string, where a string is 130 00:07:00,040 --> 00:07:02,230 some collection of characters. 131 00:07:02,230 --> 00:07:05,680 And in this case, when I use the input function, 132 00:07:05,680 --> 00:07:12,580 I'm actually getting not the integer 50, but the string of numbers, like 5-0. 133 00:07:12,580 --> 00:07:18,040 So to illustrate this real quick here, I'll say, this is the number 50, 134 00:07:18,040 --> 00:07:21,170 but this is the string 50. 135 00:07:21,170 --> 00:07:26,300 And if I were to say, does 50 equal the string 50, 136 00:07:26,300 --> 00:07:29,240 I would get back that they actually don't equal each other. 137 00:07:29,240 --> 00:07:32,120 So this is an example of different variable types. 138 00:07:32,120 --> 00:07:35,420 And it's often useful to compare variables of the same type, 139 00:07:35,420 --> 00:07:40,490 because if you were comparing like, let's say, the word cat to the number 140 00:07:40,490 --> 00:07:43,130 50, doesn't quite make sense. 141 00:07:43,130 --> 00:07:47,000 So let's try to actually convert the result of the input 142 00:07:47,000 --> 00:07:49,310 function to a whole number. 143 00:07:49,310 --> 00:07:54,020 And I can do that by using this function called int, which takes some text 144 00:07:54,020 --> 00:07:58,760 and converts it to, or at least tries to convert it to a whole number, 145 00:07:58,760 --> 00:08:01,580 like 50 or 25, for instance. 146 00:08:01,580 --> 00:08:06,620 So now, I will store inside of this guess variable, 147 00:08:06,620 --> 00:08:10,520 inside the get_guess function the number 50. 148 00:08:10,520 --> 00:08:15,470 And then later on, I'll compare that number 50 against the number 50 149 00:08:15,470 --> 00:08:16,460 down below. 150 00:08:16,460 --> 00:08:21,470 So I'll say, python of guess.py, hit Enter, and I'll say 50. 151 00:08:21,470 --> 00:08:23,630 And now I see it's correct. 152 00:08:23,630 --> 00:08:28,070 I'll say python of guess.py, and let me try now 25. 153 00:08:28,070 --> 00:08:30,260 And I guess incorrect. 154 00:08:30,260 --> 00:08:35,120 Let's say I want the user, though, to actually type in the-- 155 00:08:35,120 --> 00:08:37,760 like let's say type in like 50, for instance. 156 00:08:37,760 --> 00:08:40,190 Or 10, just like this. 157 00:08:40,190 --> 00:08:44,509 That would be an example of a string, not some whole number like an integer. 158 00:08:44,509 --> 00:08:46,670 So I could change my program. 159 00:08:46,670 --> 00:08:52,430 I could simply revert the int from this, and now it'll give me just plain text. 160 00:08:52,430 --> 00:08:53,450 And I could compare. 161 00:08:53,450 --> 00:08:58,358 I could say, is guess equal to this string called 50? 162 00:08:58,358 --> 00:08:59,150 I could check that. 163 00:08:59,150 --> 00:09:04,790 I'll say python guess.py, and now I'll say 50, and that seems to be correct. 164 00:09:04,790 --> 00:09:09,980 I could say python, guess.py, and guess 10, and that seems to be incorrect. 165 00:09:09,980 --> 00:09:15,200 So the type of variable you use often depends on the context. 166 00:09:15,200 --> 00:09:17,930 And as you go off and write more programs, 167 00:09:17,930 --> 00:09:20,870 hopefully you get to see how powerful variables can be 168 00:09:20,870 --> 00:09:24,910 and what type might best suit you for each scenario. 169 00:09:24,910 --> 00:09:27,000