1 00:00:00,000 --> 00:00:02,450 [No audio] 2 00:00:02,451 --> 00:00:03,684 Hi, welcome back. 3 00:00:03,685 --> 00:00:06,596 Please take a look at this program in here. 4 00:00:06,597 --> 00:00:09,583 Do you see any issue with this program? 5 00:00:09,584 --> 00:00:11,490 [No audio] 6 00:00:11,491 --> 00:00:13,380 The program works well. 7 00:00:13,381 --> 00:00:15,300 If we run it, we get 7 8 00:00:15,301 --> 00:00:17,786 as output, which is the expected output. 9 00:00:17,787 --> 00:00:20,276 So 3 plus 4 gives you 7. 10 00:00:20,277 --> 00:00:22,484 But there is an issue with the program, 11 00:00:22,485 --> 00:00:25,820 and the issue is that we have no 12 00:00:25,821 --> 00:00:30,028 idea what 3 means and what 4 means. 13 00:00:30,029 --> 00:00:33,692 So as programmers, we don't know what's going on 14 00:00:33,693 --> 00:00:38,070 in here and that makes this program not readable. 15 00:00:38,750 --> 00:00:40,736 Now let me show you how I will 16 00:00:40,737 --> 00:00:44,630 fix that issue right away by using variables. 17 00:00:44,631 --> 00:00:49,902 So I'll delete this entirely and say spent = 3, 18 00:00:49,903 --> 00:00:52,166 [No audio] 19 00:00:52,167 --> 00:00:54,599 donated = 4, 20 00:00:54,600 --> 00:00:57,900 [No audio] 21 00:00:57,901 --> 00:01:07,724 total_amount = spent + donated, and then print total_amount. 22 00:01:07,725 --> 00:01:09,452 If I execute this program now, 23 00:01:09,453 --> 00:01:10,898 I will get the same output. 24 00:01:10,899 --> 00:01:12,200 So it's 7 again. 25 00:01:13,210 --> 00:01:15,420 But this time we as 26 00:01:15,421 --> 00:01:18,016 programmers can understand this program. 27 00:01:18,017 --> 00:01:20,336 So we can give this program to some other 28 00:01:20,337 --> 00:01:23,152 persons, some other programmers, or we can go back 29 00:01:23,153 --> 00:01:27,584 to this program later and we can still understand 30 00:01:27,585 --> 00:01:31,236 this program because now we have names for the 31 00:01:31,237 --> 00:01:35,410 values and these are referred to as variables. 32 00:01:35,411 --> 00:01:40,270 So spent, donated, and total_amount are all variables. 33 00:01:40,850 --> 00:01:42,772 The syntax is pretty simple. 34 00:01:42,773 --> 00:01:45,336 So all you have to do is just pick 35 00:01:45,337 --> 00:01:48,070 a name from your mind for a variable. 36 00:01:48,071 --> 00:01:51,598 So I chose to have spend as the first variable. 37 00:01:51,599 --> 00:01:55,816 So this is the amount of money I spent for 38 00:01:55,817 --> 00:01:59,064 purchasing things, for example, and then this is the amount 39 00:01:59,065 --> 00:02:03,304 I donated and then I calculated the total_amount. 40 00:02:03,305 --> 00:02:05,910 So now this program makes more sense. 41 00:02:05,911 --> 00:02:09,500 And of course there are other examples such as 42 00:02:11,150 --> 00:02:16,283 items = 10 and price = 2. 43 00:02:17,150 --> 00:02:22,964 total_price = items * price. 44 00:02:22,965 --> 00:02:26,532 So now there is this asterisk here, which 45 00:02:26,533 --> 00:02:28,897 means multiplication operator. 46 00:02:28,898 --> 00:02:32,500 [No audio] 47 00:02:32,501 --> 00:02:34,472 print(total_price) and we 48 00:02:34,473 --> 00:02:37,700 get 20 as output in here. 49 00:02:39,510 --> 00:02:43,060 So again, we have one variable here, two and three. 50 00:02:43,061 --> 00:02:45,066 [No audio] 51 00:02:45,066 --> 00:02:48,082 Now, there are some rules regarding 52 00:02:48,083 --> 00:02:50,306 to the syntax of variables. 53 00:02:50,307 --> 00:02:53,532 The first rule is that you shouldn't have numbers 54 00:02:53,533 --> 00:02:57,456 in front of your variables, but you can have 55 00:02:57,457 --> 00:03:00,464 numbers in the middle somewhere or at the end, 56 00:03:00,465 --> 00:03:02,780 but not in front of them. 57 00:03:03,550 --> 00:03:07,072 You can use underscores, such as I did 58 00:03:07,073 --> 00:03:09,814 in here, but you cannot use spaces. 59 00:03:09,815 --> 00:03:13,092 So this will not work in Python, which 60 00:03:13,093 --> 00:03:15,252 means that if you have variables with more 61 00:03:15,253 --> 00:03:18,586 than one word, you should use an underscore. 62 00:03:18,587 --> 00:03:21,470 It's the best practice to use an underscore. 63 00:03:22,390 --> 00:03:25,742 And lastly, you can have as many spaces 64 00:03:25,743 --> 00:03:30,222 as you want around the assignment operator. 65 00:03:30,223 --> 00:03:35,442 So that will work, but it is recommended, according 66 00:03:35,443 --> 00:03:38,658 to the official Python style guide, it is recommended 67 00:03:38,659 --> 00:03:42,236 to have one space on both sides of the 68 00:03:42,237 --> 00:03:45,762 assignment operator to make the code more readable. 69 00:03:45,763 --> 00:03:50,032 So spaces serve only the purpose of readability here. 70 00:03:50,033 --> 00:03:52,982 The same goes for these operators. 71 00:03:52,983 --> 00:03:55,104 It's good to have spaces around 72 00:03:55,105 --> 00:03:58,930 the operator for readability again. 73 00:03:58,931 --> 00:04:01,780 So now our program makes more sense. 74 00:04:01,781 --> 00:04:04,130 We know what's going on in here. 75 00:04:04,131 --> 00:04:06,004 We understand the code. 76 00:04:06,005 --> 00:04:09,470 And that is one benefit of variables. 77 00:04:10,050 --> 00:04:12,424 There is a second benefit as well. 78 00:04:12,425 --> 00:04:20,029 Variables serve as buckets to carry data or objects. 79 00:04:20,030 --> 00:04:23,528 So 10 is a data value, or you 80 00:04:23,529 --> 00:04:25,868 can also refer to it as an object. 81 00:04:25,869 --> 00:04:29,050 So 10 is an object, 2 is an object 82 00:04:29,051 --> 00:04:32,380 and items is the name of this object, 83 00:04:32,381 --> 00:04:34,920 or you can say the variable of this object. 84 00:04:35,690 --> 00:04:40,750 And so these names, these variables are like buckets. 85 00:04:40,751 --> 00:04:43,980 They are used to carry objects around. 86 00:04:44,510 --> 00:04:47,526 For example, let's say we placed 87 00:04:47,527 --> 00:04:50,778 10 inside the items bucket, 88 00:04:50,779 --> 00:04:54,532 and then we get that bucket and multiply that 89 00:04:54,533 --> 00:04:58,770 bucket with a price bucket, which has 2 inside. 90 00:04:58,771 --> 00:05:05,656 So instead of writing 10 in here and 2, we 91 00:05:05,657 --> 00:05:10,070 actually work on a bucket basis, on a variable basis. 92 00:05:10,071 --> 00:05:14,508 There is a benefit for this because, for example, let's say 93 00:05:14,509 --> 00:05:18,716 we decide to change the value of this to 3, and 94 00:05:18,717 --> 00:05:22,060 that means items now here will also be 3. 95 00:05:22,061 --> 00:05:26,440 So 3 times 2 we're going to get 6. 96 00:05:27,630 --> 00:05:31,910 That's why we don't want to have objects hard coded. 97 00:05:31,911 --> 00:05:35,392 For example, if this was 10, we run 98 00:05:35,393 --> 00:05:40,270 this, we would get the old output 20. 99 00:05:40,271 --> 00:05:42,380 So I hope that is clear. 100 00:05:43,310 --> 00:05:45,030 Always use variables. 101 00:05:45,031 --> 00:05:51,672 And that concludes this lecture about variables. I did go 102 00:05:51,673 --> 00:05:55,048 into a lot of details because variables are used 103 00:05:55,049 --> 00:05:58,104 in every single program that you will write. 104 00:05:58,105 --> 00:06:00,798 So make sure you understand them and I'll 105 00:06:00,799 --> 00:06:03,000 talk to you in the next video. Thank you.