1
00:00:00,610 --> 00:00:01,400
Hi, welcome back!

2
00:00:01,540 --> 00:00:07,000
In the previous video we wrote, this simple program which returns the number of hours in a week.

3
00:00:07,180 --> 00:00:16,180
Now let's get a bit more real and improve this program by making it more readable, because the problem

4
00:00:16,180 --> 00:00:22,360
now is that let's say you wrote this program, but then you come a week after and you open up, you

5
00:00:22,360 --> 00:00:25,650
see the code and you don't remember what this is about.

6
00:00:25,660 --> 00:00:27,550
It just says 24 times 7.

7
00:00:27,580 --> 00:00:30,280
So, I mean, what does it calculate?

8
00:00:30,640 --> 00:00:33,290
Well, variables will save your day.

9
00:00:33,700 --> 00:00:36,970
So let us improve this program by using variables.

10
00:00:38,150 --> 00:00:43,880
I'm going to rename this using rename to basics_old.

11
00:00:46,800 --> 00:00:50,730
And create a new file and say basics.py.

12
00:00:52,980 --> 00:00:59,820
And what you can do is you can right-click over this tab, basics_old.py, and you can split

13
00:00:59,850 --> 00:01:00,490
it right.

14
00:01:00,840 --> 00:01:06,600
So now you can see both the new file and the old file just for reference.

15
00:01:07,350 --> 00:01:11,010
So we want to make a program that calculates the number of hours in a week.

16
00:01:11,610 --> 00:01:13,430
How many hours in a day?

17
00:01:14,610 --> 00:01:15,340
24.

18
00:01:15,600 --> 00:01:20,640
So how about saying day_hours equal to 24?

19
00:01:22,400 --> 00:01:33,110
And week_days equal to seven, so what I just did is I created two variables day_hours and week_days,

20
00:01:33,830 --> 00:01:42,490
so that allows me to understand what these numbers are and variables will provide that sort of feature.

21
00:01:42,770 --> 00:01:48,960
And so a variable basically is a name you give to an object.

22
00:01:49,130 --> 00:01:51,860
So these are that are two objects.

23
00:01:52,820 --> 00:01:58,390
In this case, they both happen to be number objects or more specifically integers.

24
00:01:58,400 --> 00:02:01,790
But we are going to cover all types of objects later on.

25
00:02:01,910 --> 00:02:06,260
And there's also other types of objects such as strings and lists and tuples.

26
00:02:06,410 --> 00:02:07,990
You're going to learn everything.

27
00:02:08,150 --> 00:02:15,470
But for now, it's important to know that variables are names attached to an object and to write variables

28
00:02:15,470 --> 00:02:16,160
you can write,

29
00:02:16,160 --> 00:02:17,710
you can choose whatever name you like.

30
00:02:17,720 --> 00:02:24,540
So I like to call it day_hours with an underscore.
Note that you cannot have spaces in variables.

31
00:02:24,540 --> 00:02:25,700
So that is not allowed.

32
00:02:25,700 --> 00:02:27,520
You will get an error.

33
00:02:28,460 --> 00:02:33,560
So if you want to have variables with two names, you want to use an underscore between the names.

34
00:02:33,760 --> 00:02:39,860
Day underscore hours. This is optional, so you can have a space here or not.

35
00:02:41,600 --> 00:02:50,120
But by having spaces around the assignment operator make the code more readable. Either way it will work.

36
00:02:50,300 --> 00:02:52,940
But this is more readable. In variable names

37
00:02:52,940 --> 00:02:59,780
you can use numbers so you could say day_hours1.
That would be fine, but it's not fine to start the

38
00:02:59,780 --> 00:03:02,120
variable name with a number such as that.

39
00:03:02,390 --> 00:03:07,200
You see, it becomes red, the python extension is highlighting it to red.

40
00:03:07,700 --> 00:03:10,220
Actually if we run it you'll get an error.

41
00:03:11,500 --> 00:03:13,350
You see that green button here?

42
00:03:15,410 --> 00:03:21,640
It says that syntax error, invalid syntax, so you cannot start the variable name with a number.

43
00:03:21,650 --> 00:03:25,320
Delete that, run again using that green button.

44
00:03:26,570 --> 00:03:29,720
And in this case, we didn't get an error.

45
00:03:30,050 --> 00:03:32,630
However, we didn't get an output either.

46
00:03:32,990 --> 00:03:33,510
Why?

47
00:03:33,800 --> 00:03:36,950
Well, because we are not printing out anything.

48
00:03:37,130 --> 00:03:44,330
So if you want to see output in the terminal, you want to use a print function.

49
00:03:44,360 --> 00:03:49,690
In this case it would be (day_hours * week_days).

50
00:03:50,210 --> 00:03:56,780
Now, just something I want to tell you is that if you execute this using the other way, such as

51
00:03:56,790 --> 00:04:00,740
Python3.9 basics.py...

52
00:04:03,440 --> 00:04:10,770
Sorry, that is the command for Mac, I'm on Windows, so I'll do py -3.9 

53
00:04:10,790 --> 00:04:11,590
basics.py.

54
00:04:12,830 --> 00:04:14,800
So the program was executed.

55
00:04:14,810 --> 00:04:18,080
We didn't get an error, but we didn't get an output either.

56
00:04:18,110 --> 00:04:25,650
That is because when you use this command, the script, the file is not saved automatically.

57
00:04:25,910 --> 00:04:33,380
So basically what I just did here is I executed the previous version of the script, which was without

58
00:04:33,380 --> 00:04:34,310
this line.

59
00:04:36,590 --> 00:04:44,160
So I didn't save the scripts and this command is executing the previous version of the program.

60
00:04:44,330 --> 00:04:51,000
So if you are using the command line commands like that and not that button, you want to first save

61
00:04:51,000 --> 00:04:59,950
the file with File, Save or Control S, the shortcut, whatever you like, then I can use the upper

62
00:04:59,960 --> 00:05:05,870
arrow key to call the previously executed command and then presenter to execute.

63
00:05:05,870 --> 00:05:08,670
And this time we get 168.

64
00:05:09,260 --> 00:05:14,810
So the output that we were expecting is the same output that we get with this green icon.

65
00:05:15,440 --> 00:05:21,070
So you can see that just like we did multiply the numbers so we can also multiply variables.

66
00:05:21,260 --> 00:05:25,340
So a variable is the representation of an object.

67
00:05:25,850 --> 00:05:32,780
When you are doing operations with variables, you are doing operations with the objects referred by

68
00:05:32,780 --> 00:05:34,280
those variables.

69
00:05:34,740 --> 00:05:42,070
Of course, we can improve this even more by saying week_hours.

70
00:05:42,560 --> 00:05:53,300
So a new variable which should be equal to day_hours times week_days, and then in here

71
00:05:55,760 --> 00:06:06,740
we say week_hours to print the week_hours, execute and we get again 168.

72
00:06:08,450 --> 00:06:16,340
See that here I used an empty line, Python will ignore this line so it doesn't matter how many blank

73
00:06:16,340 --> 00:06:17,840
lines you have in your code.

74
00:06:18,290 --> 00:06:22,380
These are just for readability, for your own convenience

75
00:06:22,400 --> 00:06:26,230
as a programmer. A blank line would make also sense in here.

76
00:06:26,960 --> 00:06:30,530
They allow you to group certain parts of your code.

77
00:06:30,560 --> 00:06:33,680
So here we are creating the input variables.

78
00:06:34,040 --> 00:06:36,420
And this here is a formula

79
00:06:37,590 --> 00:06:41,550
that calculates the output, and then here we return that output.

80
00:06:42,180 --> 00:06:50,690
So what happened here is that a new object was created, so 168, and that object was stored in that variable.

81
00:06:50,700 --> 00:06:52,980
So it's being referred by that variable.

82
00:06:53,130 --> 00:07:00,540
The variable now represents 168 and then we print out the value of that variable.

83
00:07:00,570 --> 00:07:05,220
So the object of the variable, that prints out 168.

84
00:07:05,760 --> 00:07:09,150
So even if this is longer than this,

85
00:07:09,840 --> 00:07:13,850
it is much better because first it is more readable.

86
00:07:13,860 --> 00:07:19,350
And second, it also allows us to expand the program.

87
00:07:20,270 --> 00:07:25,350
And that was the lecture about the variables, so two key points to take from this lecture.

88
00:07:25,520 --> 00:07:28,940
First, variables will make your code readable.

89
00:07:29,090 --> 00:07:33,980
And second, variables are used as transporters.

90
00:07:34,160 --> 00:07:38,700
So they transport objects from one place to the other.

91
00:07:38,930 --> 00:07:45,730
For example, in our example, we stored an object in this variable through that formula.

92
00:07:45,740 --> 00:07:56,990
So 168 is being referred by week_hours, and week_hours transports for that 168 number to the print function,

93
00:07:56,990 --> 00:07:58,640
so the print function prints it out.

94
00:07:58,850 --> 00:08:01,430
And that is about variables.

95
00:08:01,730 --> 00:08:02,770
Thanks a lot for following!

96
00:08:02,780 --> 00:08:03,580
I'll talk to you later.

