1 00:00:00,600 --> 00:00:01,589 Man: Welcome back. 2 00:00:01,589 --> 00:00:03,510 So we've just learned about 3 00:00:03,510 --> 00:00:07,560 the first two, well kind of three because we also mentioned 4 00:00:07,560 --> 00:00:12,390 that there is such a thing as a complex number in Python. 5 00:00:12,390 --> 00:00:15,540 But we talked about the main ones, the int and float. 6 00:00:15,540 --> 00:00:18,423 And there are still a few data types remaining. 7 00:00:19,500 --> 00:00:21,450 But before we get to these, 8 00:00:21,450 --> 00:00:24,210 we are missing an important concept. 9 00:00:24,210 --> 00:00:27,570 And this is going to be our first important term in Python. 10 00:00:27,570 --> 00:00:30,873 And, as a matter of fact, in all programming languages. 11 00:00:31,710 --> 00:00:33,690 It's called, variables. 12 00:00:33,690 --> 00:00:35,493 Yep, that's a term. 13 00:00:36,480 --> 00:00:38,340 Now, if this is your first time 14 00:00:38,340 --> 00:00:39,930 learning a programming language, 15 00:00:39,930 --> 00:00:41,970 you might not know what this means. 16 00:00:41,970 --> 00:00:44,790 If this isn't your first time, well, this is very simple 17 00:00:44,790 --> 00:00:48,150 because all languages have variables. 18 00:00:48,150 --> 00:00:51,360 But what are they exactly? 19 00:00:51,360 --> 00:00:53,940 Well variables store information 20 00:00:53,940 --> 00:00:57,030 that can be used in our programs. 21 00:00:57,030 --> 00:01:01,800 So we can hold perhaps user inputs like values. 22 00:01:01,800 --> 00:01:05,459 Maybe when you log into Facebook, you need to 23 00:01:05,459 --> 00:01:08,760 hold some information such as your profile picture. 24 00:01:08,760 --> 00:01:13,260 Or maybe your date of birth in a variable. 25 00:01:13,260 --> 00:01:16,620 Variables are ways for us to store information 26 00:01:16,620 --> 00:01:17,463 on our computer. 27 00:01:18,390 --> 00:01:20,370 So let's have a look at this. 28 00:01:20,370 --> 00:01:22,200 If we remove this, 29 00:01:22,200 --> 00:01:24,843 and let's say I'm creating a quiz program, 30 00:01:26,010 --> 00:01:30,090 and this quiz program maybe measures your IQ, 31 00:01:30,090 --> 00:01:32,430 and let's say you just took the quiz, 32 00:01:32,430 --> 00:01:35,520 and you found out that your IQ is 190. 33 00:01:35,520 --> 00:01:37,350 Quite high, good job. 34 00:01:37,350 --> 00:01:40,950 But we need to store that information somewhere. 35 00:01:40,950 --> 00:01:43,140 Well, we can do that with variables. 36 00:01:43,140 --> 00:01:46,140 So that in Python all we need to do is 37 00:01:46,140 --> 00:01:47,820 name it whatever we want. 38 00:01:47,820 --> 00:01:50,550 In our case, it'll be "IQ." 39 00:01:50,550 --> 00:01:53,937 And we're gonna say, "IQ equals 190." 40 00:01:55,740 --> 00:01:59,880 And this IQ here is a variable. 41 00:01:59,880 --> 00:02:02,790 It is something that I just completely made up. 42 00:02:02,790 --> 00:02:04,800 I could name it whatever I want. 43 00:02:04,800 --> 00:02:06,930 That's a variable too. 44 00:02:06,930 --> 00:02:11,930 The idea is that once we assign to a variable, that is, 45 00:02:12,060 --> 00:02:15,240 we're saying 190 is going to be assigned to IQ. 46 00:02:15,240 --> 00:02:18,240 I can now use it in my program whenever I want. 47 00:02:18,240 --> 00:02:21,513 For example, I can later on print, 48 00:02:22,380 --> 00:02:23,790 oh, make sure it's not cap. 49 00:02:23,790 --> 00:02:26,160 Let's do print, IQ. 50 00:02:26,160 --> 00:02:28,050 And if I do that and click run, 51 00:02:28,050 --> 00:02:29,973 you see that I can use IQ. 52 00:02:30,990 --> 00:02:34,680 So we can pretend here that a user takes a quiz, 53 00:02:34,680 --> 00:02:37,410 finds out their IQ is 190. 54 00:02:37,410 --> 00:02:40,110 We can store that information in this variable 55 00:02:40,110 --> 00:02:42,300 and later on when they come, 56 00:02:42,300 --> 00:02:45,000 perhaps online or try to use the program again, 57 00:02:45,000 --> 00:02:46,500 they don't have to take the quiz all over 58 00:02:46,500 --> 00:02:49,353 because, well, we store that information in IQ. 59 00:02:51,390 --> 00:02:53,010 And remember what I said at the beginning, 60 00:02:53,010 --> 00:02:57,330 programs are simply data that's being stored, 61 00:02:57,330 --> 00:03:00,420 that's being changed, that's being removed, 62 00:03:00,420 --> 00:03:01,800 and that's all programs are. 63 00:03:01,800 --> 00:03:04,530 And variables are important concepts in Python 64 00:03:04,530 --> 00:03:06,480 and all languages. 65 00:03:06,480 --> 00:03:09,510 Now, variables can also sometimes be called names. 66 00:03:09,510 --> 00:03:12,300 So, this could be a name for example. 67 00:03:12,300 --> 00:03:14,670 And assigning a value, 68 00:03:14,670 --> 00:03:16,650 is also known as binding. 69 00:03:16,650 --> 00:03:21,360 That is we're binding the value 190 to this variable, 70 00:03:21,360 --> 00:03:24,510 so that when we request this variable 71 00:03:24,510 --> 00:03:26,580 later on in our program, 72 00:03:26,580 --> 00:03:29,730 our computer knows how to look for this information. 73 00:03:29,730 --> 00:03:32,310 It's going to say, "Hey, I know what IQ is. 74 00:03:32,310 --> 00:03:34,170 I stored it somewhere in memory." 75 00:03:34,170 --> 00:03:36,780 And it's going to go look for that. 76 00:03:36,780 --> 00:03:40,800 And because it's being bound to a value 77 00:03:40,800 --> 00:03:44,370 it points to this value 190. 78 00:03:44,370 --> 00:03:47,430 And remember, this number in memory gets stored 79 00:03:47,430 --> 00:03:52,430 as a binary representation in zeros and ones, right? 80 00:03:52,830 --> 00:03:54,570 But it doesn't matter to us because 81 00:03:54,570 --> 00:03:57,360 however our machine stores it, we don't care. 82 00:03:57,360 --> 00:03:59,100 We just wanna be able to retrieve it. 83 00:03:59,100 --> 00:04:03,360 And then when we print it to do, well get 190. 84 00:04:03,360 --> 00:04:07,440 Now, we're gonna be using variables all over the course. 85 00:04:07,440 --> 00:04:12,440 But on top of just naming variables however we want, 86 00:04:12,960 --> 00:04:16,560 there is some best practices around variables 87 00:04:16,560 --> 00:04:19,649 of how you should write good variables. 88 00:04:19,649 --> 00:04:20,880 And as a matter of fact, 89 00:04:20,880 --> 00:04:23,550 these are specific rules that the Python community 90 00:04:23,550 --> 00:04:26,820 as a whole has, that you'll just have to remember. 91 00:04:26,820 --> 00:04:28,830 So let's have a look at this. 92 00:04:28,830 --> 00:04:31,170 Variables, and remember, this is the symbol 93 00:04:31,170 --> 00:04:35,223 for best practices, are what we call snake_case. 94 00:04:36,120 --> 00:04:40,620 Snake_case means it's all lower case, and then spaces, 95 00:04:40,620 --> 00:04:43,800 while they don't exist, we use underscores. 96 00:04:43,800 --> 00:04:48,800 Variables must start with a lowercase or an underscore. 97 00:04:48,930 --> 00:04:52,260 Variables can be anything with letters, 98 00:04:52,260 --> 00:04:54,390 numbers, and underscores. 99 00:04:54,390 --> 00:04:56,550 But remember, they have to start 100 00:04:56,550 --> 00:04:58,680 with lowercase and underscore. 101 00:04:58,680 --> 00:05:02,220 That means we can't start a variable with a number. 102 00:05:02,220 --> 00:05:04,560 They're also case sensitive. 103 00:05:04,560 --> 00:05:07,530 That means if I create a variable, 104 00:05:07,530 --> 00:05:09,014 but let's say this snake_case, 105 00:05:09,014 --> 00:05:13,290 this variable, has a capital E instead of a lowercase E, 106 00:05:13,290 --> 00:05:15,093 that'll be a different variable. 107 00:05:16,050 --> 00:05:19,353 And then finally, you can't overwrite keywords. 108 00:05:20,250 --> 00:05:22,350 Let's go through these with some examples. 109 00:05:23,400 --> 00:05:27,420 First, a variable has to be in the form of a snake_case. 110 00:05:27,420 --> 00:05:31,680 That is, if I wanna call this "user IQ," 111 00:05:31,680 --> 00:05:34,350 I should technically have an underscore here. 112 00:05:34,350 --> 00:05:38,670 Instead of a space, just to make sure that a programmer, 113 00:05:38,670 --> 00:05:41,493 maybe I'm working on a team, can read this variable. 114 00:05:42,630 --> 00:05:45,300 So that's snake_case. 115 00:05:45,300 --> 00:05:50,300 You also have to start your variables with either a letter 116 00:05:50,730 --> 00:05:51,720 or an underscore. 117 00:05:51,720 --> 00:05:54,540 So I can technically do this. 118 00:05:54,540 --> 00:05:55,920 And I click run. 119 00:05:55,920 --> 00:05:57,660 Well, that's going to gimme an error 120 00:05:57,660 --> 00:05:59,130 because I've changed the variable. 121 00:05:59,130 --> 00:06:01,920 So now, in order to access that variable, 122 00:06:01,920 --> 00:06:04,140 you have to go like this. 123 00:06:04,140 --> 00:06:08,670 Now, underscore in Python signifies a private variable. 124 00:06:08,670 --> 00:06:11,820 Something that we'll go over later on in the course. 125 00:06:11,820 --> 00:06:16,320 But usually you're starting your variables with a letter. 126 00:06:16,320 --> 00:06:19,320 And afterwards, yeah, you can add numbers 127 00:06:19,320 --> 00:06:21,690 if you want in here, that's no problem. 128 00:06:21,690 --> 00:06:23,160 This is still going to work. 129 00:06:23,160 --> 00:06:24,963 This is a valid variable. 130 00:06:25,830 --> 00:06:29,160 Finally, a variable is case sensitive. 131 00:06:29,160 --> 00:06:32,820 So if I do "user IQ" here, 132 00:06:32,820 --> 00:06:34,683 and I do capital letters, 133 00:06:35,640 --> 00:06:37,353 I can't access this, 134 00:06:41,550 --> 00:06:44,700 like this because, well, it doesn't exist. 135 00:06:44,700 --> 00:06:45,990 Again, it's case sensitive. 136 00:06:45,990 --> 00:06:48,123 We have to make sure that we match. 137 00:06:50,220 --> 00:06:54,180 And finally, we don't wanna overwrite keywords. 138 00:06:54,180 --> 00:06:55,950 What does that mean? 139 00:06:55,950 --> 00:06:58,530 Keywords in Python? 140 00:06:58,530 --> 00:07:00,750 Well, they already mean something in Python. 141 00:07:00,750 --> 00:07:03,510 For example, this print is a keyword. 142 00:07:03,510 --> 00:07:05,520 You can see it highlighted in blue. 143 00:07:05,520 --> 00:07:08,790 So that if I create a variable 144 00:07:08,790 --> 00:07:10,710 saying print equals 190, 145 00:07:10,710 --> 00:07:13,290 and then I do print, print? 146 00:07:13,290 --> 00:07:14,690 Hmm, let's see what happens. 147 00:07:16,380 --> 00:07:19,410 We get an error because I can't really assign 148 00:07:19,410 --> 00:07:22,923 to this variable because print already means something. 149 00:07:23,850 --> 00:07:25,620 Now I know what you're wondering. 150 00:07:25,620 --> 00:07:27,780 What are these keywords in Python? 151 00:07:27,780 --> 00:07:29,850 That's a simple Google search away, 152 00:07:29,850 --> 00:07:32,010 and we'll learn these throughout our course. 153 00:07:32,010 --> 00:07:35,820 If we go to Python keywords by W3Schools, 154 00:07:35,820 --> 00:07:38,280 you'll see that we have these keywords 155 00:07:38,280 --> 00:07:40,983 that each means something in Python. 156 00:07:41,880 --> 00:07:43,920 Again, we'll go through these 157 00:07:43,920 --> 00:07:46,350 and we'll learn them throughout our course. 158 00:07:46,350 --> 00:07:48,660 And if you look, it's not that intimidating. 159 00:07:48,660 --> 00:07:50,040 There's not that many. 160 00:07:50,040 --> 00:07:53,730 So as we practice, you'll start to get familiar with them. 161 00:07:53,730 --> 00:07:56,550 But the easiest way to tell whether it's keyword 162 00:07:56,550 --> 00:07:58,680 or an important word in in Python, 163 00:07:58,680 --> 00:08:01,650 well you see that it's highlighted in blue. 164 00:08:01,650 --> 00:08:05,130 As soon as you create a variable that is unique, 165 00:08:05,130 --> 00:08:06,900 it's highlighted in white. 166 00:08:06,900 --> 00:08:09,660 And this will be the case with whatever 167 00:08:09,660 --> 00:08:12,060 environment that you're typing code into. 168 00:08:12,060 --> 00:08:14,400 As long as it's set up for Python. 169 00:08:14,400 --> 00:08:17,730 Now, beyond the Python keywords, there are different things 170 00:08:17,730 --> 00:08:21,480 like for example, the i.n.t, 171 00:08:21,480 --> 00:08:24,060 for integer that we've learned. 172 00:08:24,060 --> 00:08:26,640 So these we're gonna get familiar with. 173 00:08:26,640 --> 00:08:29,310 So, a good rule of thumb for variables 174 00:08:29,310 --> 00:08:31,080 is to make them really descriptive. 175 00:08:31,080 --> 00:08:33,900 Really say what your intention is. 176 00:08:33,900 --> 00:08:36,390 And a good programmer is somebody that's able to 177 00:08:36,390 --> 00:08:38,909 name things really well with their variables. 178 00:08:38,909 --> 00:08:42,570 So if a new developer comes and looks at your code, 179 00:08:42,570 --> 00:08:44,670 it's easily understood. 180 00:08:44,670 --> 00:08:48,450 Finally, variables can also be reassigned. 181 00:08:48,450 --> 00:08:52,680 For example, let's say we have IQ here of 190, 182 00:08:52,680 --> 00:08:57,600 and then I decide to, perhaps, have another variable, 183 00:08:57,600 --> 00:09:00,807 call it "user age." 184 00:09:01,680 --> 00:09:05,220 And for some reason I wanna assign 185 00:09:05,220 --> 00:09:08,490 user age to perhaps have 186 00:09:08,490 --> 00:09:11,403 IQ divided by four. 187 00:09:12,420 --> 00:09:14,310 Is that going to work? 188 00:09:14,310 --> 00:09:15,720 It should, right? 189 00:09:15,720 --> 00:09:20,720 I'm saying user age is going to equal 47.5. 190 00:09:21,720 --> 00:09:26,280 I'm using IQ, which is 190, 191 00:09:26,280 --> 00:09:27,510 dividing it by four, 192 00:09:27,510 --> 00:09:30,360 and then assigning it to user age. 193 00:09:30,360 --> 00:09:32,670 I can maybe assign this to another variable 194 00:09:32,670 --> 00:09:36,120 called "user age or called a", 195 00:09:36,120 --> 00:09:40,380 and once again, I run this 196 00:09:40,380 --> 00:09:42,810 and it's printing the same thing. 197 00:09:42,810 --> 00:09:45,270 So you can use variables to store that information 198 00:09:45,270 --> 00:09:47,250 and use it whenever you want. 199 00:09:47,250 --> 00:09:51,720 You can use it in operations, you can use it to reassign it, 200 00:09:51,720 --> 00:09:54,090 whatever your program needs. 201 00:09:54,090 --> 00:09:57,630 Now, later on in the course, we're gonna learn about classes 202 00:09:57,630 --> 00:10:01,200 and classes actually have a different convention than this 203 00:10:01,200 --> 00:10:03,750 but we'll get to that later on. 204 00:10:03,750 --> 00:10:07,170 For now though, I wanna mention two small gotchas 205 00:10:07,170 --> 00:10:09,900 with variables that you should be careful with. 206 00:10:09,900 --> 00:10:14,220 For example, there's an idea of constants. 207 00:10:14,220 --> 00:10:16,350 And constants are those things that 208 00:10:16,350 --> 00:10:19,080 never change in a program. 209 00:10:19,080 --> 00:10:22,170 For example, if we wanted to create a constant 210 00:10:22,170 --> 00:10:24,630 such as the value of pi, 211 00:10:24,630 --> 00:10:26,173 let's say for now it's 3.14, 212 00:10:27,330 --> 00:10:30,420 we can have it all in capitals. 213 00:10:30,420 --> 00:10:32,407 And that's going to tell other programmers that, 214 00:10:32,407 --> 00:10:34,410 "Hey, this is a constant." 215 00:10:34,410 --> 00:10:36,720 This number is not meant to change. 216 00:10:36,720 --> 00:10:39,753 I mean, we could change it if we want. 217 00:10:41,130 --> 00:10:41,963 There you go. 218 00:10:41,963 --> 00:10:44,340 I just made pi equal to zero. 219 00:10:44,340 --> 00:10:47,250 So it was stored as 3.14 in memory 220 00:10:47,250 --> 00:10:50,146 but then we overwrote it and reassigned it, 221 00:10:50,146 --> 00:10:52,320 the value of zero. 222 00:10:52,320 --> 00:10:55,410 You can still do that but a good convention 223 00:10:55,410 --> 00:10:58,140 is that if you see this, that means this number 224 00:10:58,140 --> 00:11:01,050 or this value, should never be changed. 225 00:11:01,050 --> 00:11:03,660 Another type of variable that you're gonna see, 226 00:11:03,660 --> 00:11:06,090 and this is something we'll see later on in the course, 227 00:11:06,090 --> 00:11:07,650 are the, it doesn't look like 228 00:11:07,650 --> 00:11:09,750 I just did double underscores here, 229 00:11:09,750 --> 00:11:12,900 but it's two underscores, and we call these dunder. 230 00:11:12,900 --> 00:11:14,940 And as you can see here, we have some 231 00:11:14,940 --> 00:11:18,543 dunder variables that Python has. 232 00:11:19,830 --> 00:11:22,020 Now, we'll learn more about these later on 233 00:11:22,020 --> 00:11:25,920 but, the idea here is that these 234 00:11:25,920 --> 00:11:27,300 are meant to be left alone. 235 00:11:27,300 --> 00:11:28,800 You should not touch them. 236 00:11:28,800 --> 00:11:33,300 You shouldn't create a variable with two underscores, 237 00:11:33,300 --> 00:11:38,300 like this, and call it "hihi" and assign it a value. 238 00:11:38,970 --> 00:11:41,610 I mean, you still can, however, 239 00:11:41,610 --> 00:11:44,040 this is generally not good practice 240 00:11:44,040 --> 00:11:45,753 so you wanna be careful with that. 241 00:11:46,770 --> 00:11:49,710 But the one thing that I really want you to take away 242 00:11:49,710 --> 00:11:52,080 from this, is that variables 243 00:11:52,080 --> 00:11:54,450 are really important concepts in programming. 244 00:11:54,450 --> 00:11:57,600 Naming variables is one of the most important skills 245 00:11:57,600 --> 00:11:59,250 you have as a programmer. 246 00:11:59,250 --> 00:12:02,130 I know it sounds silly, but, there's so many times 247 00:12:02,130 --> 00:12:04,710 that I read code that is so hard to understand, 248 00:12:04,710 --> 00:12:08,640 simply because a programmer is not descriptive enough. 249 00:12:08,640 --> 00:12:11,670 So throughout the course, we're gonna learn how to 250 00:12:11,670 --> 00:12:16,563 name things well, so that our code reads like English. 251 00:12:17,490 --> 00:12:20,340 And that's the whole point of writing good code. 252 00:12:20,340 --> 00:12:22,410 The point of writing good code is that 253 00:12:22,410 --> 00:12:27,030 it's readable and understandable by other programmers. 254 00:12:27,030 --> 00:12:28,470 By the way, to finish off, 255 00:12:28,470 --> 00:12:30,630 I just wanna show you one quick trick. 256 00:12:30,630 --> 00:12:35,400 There's also a way that you might see, in some code bases 257 00:12:35,400 --> 00:12:37,233 that uses something like this. 258 00:12:38,370 --> 00:12:40,353 Equals 1, 2, 3. 259 00:12:41,190 --> 00:12:45,540 And this simply is a way for us to rapidly 260 00:12:45,540 --> 00:12:49,020 assign values to variables multiple times. 261 00:12:49,020 --> 00:12:53,073 So for example, if I do print A, then print B, 262 00:12:54,240 --> 00:12:57,393 then print C, and I run this, 263 00:12:58,410 --> 00:13:00,450 you see that I get 1, 2, 3. 264 00:13:00,450 --> 00:13:04,050 We assign value of one to a, value of two to B, 265 00:13:04,050 --> 00:13:05,763 and value of three to C. 266 00:13:06,660 --> 00:13:09,930 Just a quick shorthand way that you might encounter. 267 00:13:09,930 --> 00:13:12,833 All right, let's take a break and I'll see in the next one.