1
00:00:00,710 --> 00:00:07,080
In this video, we are going to ride and around our first Python program in Visual Studio Code, so previously

2
00:00:07,100 --> 00:00:09,330
I installed the Python extension.

3
00:00:09,480 --> 00:00:14,790
If you didn't install that, please go to the previous video and follow the instructions on how to install

4
00:00:15,090 --> 00:00:17,180
the Python extension for Visual Studio Code.

5
00:00:17,820 --> 00:00:20,340
Now, I want to minimize this pan here.

6
00:00:20,370 --> 00:00:25,680
To do that, I can click this Extensions button again and that will disappear.

7
00:00:25,950 --> 00:00:32,430
And what I want to show here instead is what is known as the Explorer in Visual Studio Code.

8
00:00:32,730 --> 00:00:37,230
So this pan here, which will display the project directory tree.

9
00:00:37,380 --> 00:00:41,780
So the folders and the files that we are going to have for this particular project.

10
00:00:42,030 --> 00:00:48,840
And here we can create new files, which could be Python files, where we write the codes for the program

11
00:00:49,110 --> 00:00:54,690
or other associated files for the programs such as text files, Excel files and so on.

12
00:00:54,870 --> 00:01:00,840
In the course, we will work with a lot of files so files that our Python programs will process and

13
00:01:00,840 --> 00:01:01,710
will create.

14
00:01:01,900 --> 00:01:07,650
But for now, for this particular video, we are going to write a very simple Python program, which

15
00:01:07,650 --> 00:01:12,510
is enough to get you started with creating and running programs in Visual Studio Code.

16
00:01:13,350 --> 00:01:16,110
What We're Going to create is very, very simple.

17
00:01:17,070 --> 00:01:22,990
We are going to create a program that calculates the amount of hours in a full week.

18
00:01:23,310 --> 00:01:24,500
Have you ever thought of that?

19
00:01:24,540 --> 00:01:25,800
How many hours are in a week?

20
00:01:26,050 --> 00:01:27,460
Let's find it out with Python.

21
00:01:27,900 --> 00:01:34,140
So when you have a program in mind to what you want to create in Visual Studio Code is a Visual Studio Code

22
00:01:34,140 --> 00:01:34,950
Project.

23
00:01:35,160 --> 00:01:42,990
To do that, the first thing you do is you open a folder, an empty folder, where you are going to

24
00:01:42,990 --> 00:01:46,060
save all the program files. To do that

25
00:01:46,060 --> 00:01:53,460
you can either go to this button or the standard way is to go to File and then go to Open Folder.

26
00:01:54,150 --> 00:01:55,150
If you are on Mac

27
00:01:55,740 --> 00:01:59,900
this should be named just Open instead of Open Folder.

28
00:01:59,910 --> 00:02:01,260
On Windows it would be Open Folder.

29
00:02:04,580 --> 00:02:11,100
And here, what we want to do is we want to create an empty folder. In that empty folder

30
00:02:11,120 --> 00:02:17,840
we are going to save all the Python files and maybe other associated files related to our project.

31
00:02:18,350 --> 00:02:22,280
So what do you want to do is you want to create a new empty folder.

32
00:02:23,840 --> 00:02:31,700
I'm going to name it python_basics. Note that I used an underscore, I didn't use a space. It is recommended

33
00:02:31,700 --> 00:02:36,170
not to use spaces in directory paths or file paths.

34
00:02:36,320 --> 00:02:39,170
So press, enter and select folder.

35
00:02:41,330 --> 00:02:48,410
What that will do is Visual Studio Code will open in that particular folder, you can see here Python

36
00:02:48,410 --> 00:02:51,770
basics and then we get this welcome window again.

37
00:02:51,770 --> 00:02:52,570
I can close that.

38
00:02:52,580 --> 00:02:53,730
We don't need that anymore.

39
00:02:54,260 --> 00:03:00,350
The next thing we want to do is we want to create an empty Python file where we will write our Python program.

40
00:03:00,350 --> 00:03:02,300
To create files

41
00:03:02,330 --> 00:03:04,420
you can go to this icon here.

42
00:03:05,240 --> 00:03:08,930
So that is the button for new files, to create new files.

43
00:03:09,150 --> 00:03:11,270
That is a button to create new folders.

44
00:03:11,270 --> 00:03:15,000
And that refreshes the Explorer to show new files

45
00:03:15,020 --> 00:03:17,930
that our Python program may be generating.

46
00:03:17,930 --> 00:03:19,170
For now we don't need that button.

47
00:03:19,370 --> 00:03:21,050
We need the new file button.

48
00:03:21,170 --> 00:03:22,940
So there you want to give a name.

49
00:03:23,980 --> 00:03:31,640
Such as basics.py. So Python files need to come with a dot py extension.

50
00:03:31,780 --> 00:03:38,200
You see that when I add a py extension, Visual Studio Code recognizes that file as Python.

51
00:03:38,200 --> 00:03:43,060
You see the logo of Python there and that is made possible by the Python extension that we installed

52
00:03:43,060 --> 00:03:43,630
previously.

53
00:03:43,960 --> 00:03:47,080
So press enter and that will create a new python file.

54
00:03:47,440 --> 00:03:54,910
Of course, that file can also be seen in the Python basics directory that I created.

55
00:03:57,450 --> 00:04:03,390
So you see, when you create something in Visual Studio Code, that is reflected to your Windows Explorer

56
00:04:03,390 --> 00:04:06,010
or Mac finder, so that is an empty file.

57
00:04:06,300 --> 00:04:11,460
We want to make a program that calculates the number of hours in a week.

58
00:04:12,030 --> 00:04:18,000
Now, when we say we want to make a program that calculates that and we are not specifying that this

59
00:04:18,000 --> 00:04:25,560
is a Web app or a desktop app with a graphical user interface, you know, with buttons where the user

60
00:04:25,560 --> 00:04:28,870
can press a button, can enter some data in a text box and so on,

61
00:04:29,280 --> 00:04:33,110
so we are not talking about a graphical user interface or web app.

62
00:04:33,900 --> 00:04:39,510
What we are talking about is a simple program with a command-line interface, which you are going to

63
00:04:39,510 --> 00:04:41,180
see in a bit how it looks like.

64
00:04:41,760 --> 00:04:47,400
So it's a text-based interface and to display the output in that text-base interface,

65
00:04:47,550 --> 00:04:50,670
we want to use the print function.

66
00:04:51,740 --> 00:04:58,310
So the print function is a very important function in Python, and the print function expects some input

67
00:04:58,310 --> 00:05:01,680
to print out in the terminal, in the text based interface.

68
00:05:02,000 --> 00:05:05,670
In this case, we want to calculate how many hours are in a week.

69
00:05:05,690 --> 00:05:09,470
So in a day, we know that there's 24 hours.

70
00:05:10,160 --> 00:05:13,000
In a week, it means 24 times 7.

71
00:05:14,150 --> 00:05:15,110
As simple as that.

72
00:05:15,990 --> 00:05:19,020
OK, I know this is not the best way to write this program.

73
00:05:19,190 --> 00:05:21,880
We are going to improve this program in the next video.

74
00:05:22,520 --> 00:05:25,670
But for now, let's keep it this simple, right?

75
00:05:25,700 --> 00:05:26,570
This is a program.

76
00:05:26,570 --> 00:05:28,140
We are ready to run the program now.

77
00:05:28,170 --> 00:05:29,500
How do we run it?

78
00:05:29,930 --> 00:05:36,370
Well, there are two ways to run a Python program in Visual Studio Code. The easiest way to write

79
00:05:36,620 --> 00:05:40,580
a program in Visual Studio Code is to use this green triangle here.

80
00:05:40,880 --> 00:05:42,830
This is a button. If you don't have that,

81
00:05:43,210 --> 00:05:45,080
that might be because of two reasons.

82
00:05:45,080 --> 00:05:45,740
First,

83
00:05:45,770 --> 00:05:49,910
the first reason is that you didn't installe the python extension.

84
00:05:49,930 --> 00:05:53,680
So if you didn't do that search for Python and install that extension.

85
00:05:53,690 --> 00:05:55,120
In my case, I have installed it.

86
00:05:55,130 --> 00:06:01,490
If you have installed the Python extension and the button still doesn't show, then that might be because

87
00:06:01,490 --> 00:06:09,140
in here, down here, there's no python interpreter specified for your Visual Studio Code editor.

88
00:06:09,380 --> 00:06:11,110
So in my case, this is fine.

89
00:06:11,120 --> 00:06:11,900
It's a Python 3.9.2 

90
00:06:11,900 --> 00:06:13,700
is the interpreter interpreter.

91
00:06:14,060 --> 00:06:16,750
In your case, maybe it says 'select interpreter'.

92
00:06:16,940 --> 00:06:21,980
In that case, you want to click over 'select interpreter' and then select one of the interpreters

93
00:06:21,980 --> 00:06:23,840
that you see in the list here.

94
00:06:24,020 --> 00:06:25,820
So what are these interpreters?

95
00:06:25,850 --> 00:06:29,140
Well, in one of the previous videos, we installed Python.

96
00:06:29,330 --> 00:06:35,120
So basically when you installed Python, what you're doing is you're installing an interpreter, the

97
00:06:35,120 --> 00:06:36,140
Python interpreter.

98
00:06:36,410 --> 00:06:40,100
So it's a program that interprets Python language.

99
00:06:40,340 --> 00:06:42,590
So this is the language, right?

100
00:06:42,590 --> 00:06:49,280
Now we want a Python interpreter that understands that language and is able to return some output for that

101
00:06:49,280 --> 00:06:49,740
language.

102
00:06:49,970 --> 00:06:53,990
So we want to tell Visual Studio Code which interpreter to use.

103
00:06:53,990 --> 00:07:00,200
So select one interpreter and then you should see this button here.

104
00:07:01,320 --> 00:07:06,690
If that doesn't work, then there is another way to run Python programs, which I'll show in just a

105
00:07:06,690 --> 00:07:06,930
bit.

106
00:07:07,530 --> 00:07:16,530
So let's first try this easy way, press that button and this is the terminal, so the text-based interface.

107
00:07:16,800 --> 00:07:20,460
You see, it's only text here and this is the output.

108
00:07:21,360 --> 00:07:24,450
So there's 168 hours in a week.

109
00:07:24,780 --> 00:07:31,440
Now, let me explain to you how Visual Studio Code executed this program so that it's not all magic

110
00:07:31,440 --> 00:07:31,990
for you.

111
00:07:32,340 --> 00:07:36,720
This also relates to the second way of executing a Python program.

112
00:07:37,260 --> 00:07:45,690
So when you press, when I pressed this button, what happened is that Visual Studio Code executed these

113
00:07:46,320 --> 00:07:47,250
commands.

114
00:07:47,700 --> 00:07:49,020
So let me try that again.

115
00:07:50,610 --> 00:07:56,760
You see, when I pressed the button, Visual Studio Code executed these two commands and it returns the

116
00:07:56,760 --> 00:07:57,240
output.

117
00:07:57,540 --> 00:07:59,820
Now, these are not Python code.

118
00:07:59,820 --> 00:08:01,880
Don't confuse them with Python code.

119
00:08:02,100 --> 00:08:07,770
So this is a terminal and this terminal here expects Windows commands.

120
00:08:07,770 --> 00:08:15,030
Or if you are on Mac, it expects Unix commands, so Mac commands, commands that the Mac operating system

121
00:08:15,180 --> 00:08:17,880
understands and commands that Windows understands.

122
00:08:18,630 --> 00:08:20,960
So this command here contains two parts.

123
00:08:21,900 --> 00:08:24,600
The first part is this one in here.

124
00:08:25,170 --> 00:08:29,700
That is the path to the Python 3.9 interpreter.

125
00:08:29,970 --> 00:08:36,090
You can see here, 'python.exe', this is interpreted, that we installed a few videos before.

126
00:08:36,210 --> 00:08:42,960
So that button knows where this interpreter is because we have told Visual Studio Code that this is

127
00:08:42,960 --> 00:08:44,370
the interpreter I'm using.

128
00:08:44,640 --> 00:08:49,890
So visual to the code knows the path of that interpreter and gives that to Windows.

129
00:08:51,240 --> 00:08:57,420
And so this is a program, the interpreter, and this is the code, the file that contains the codes,

130
00:08:58,050 --> 00:09:05,950
so 'C:\Users: arditsulce\python_basics/basics.py.
So this file here.

131
00:09:06,300 --> 00:09:14,340
That means if you don't have that green button, you can also execute programs by typing that manually,

132
00:09:14,340 --> 00:09:17,100
that commands and you'll get the output.

133
00:09:17,340 --> 00:09:21,570
But there is an easier way and that is to write py.

134
00:09:21,570 --> 00:09:31,320
If you are on Windows you do this py-3.9 and then basics.py, execute that

135
00:09:31,320 --> 00:09:32,630
and you'll get the output again.

136
00:09:33,420 --> 00:09:36,090
So it's just another way to execute this program.

137
00:09:36,210 --> 00:09:42,600
If you are on Mac or Linux, you do python3.9

138
00:09:42,780 --> 00:09:43,770
basics.py.

139
00:09:45,440 --> 00:09:51,320
So that is for Mac and Linux. If you have another Python version, you'd have to replace the 3.9

140
00:09:51,320 --> 00:09:54,680
for example with 3.10 and so on.

141
00:09:54,860 --> 00:09:56,910
The same you'd do for Windows.

142
00:09:56,910 --> 00:10:02,630
So instead of  3.9, you do 3.10, 3.11 or whatever Python version

143
00:10:02,630 --> 00:10:05,400
you installed and you are using.

144
00:10:05,690 --> 00:10:07,740
So that's about running a Python program.

145
00:10:08,000 --> 00:10:10,640
You either use that button or use these commands.

146
00:10:11,030 --> 00:10:16,910
Now what about if you want to close this project and open a new one and then you want to find this project?

147
00:10:17,170 --> 00:10:18,280
Let me show you how to do that.

148
00:10:18,770 --> 00:10:25,070
So let's suppose that you closed Visual Studio Code and then tomorrow you open up Visual Studio Code

149
00:10:25,070 --> 00:10:28,730
again and it shows the same project.

150
00:10:29,390 --> 00:10:33,590
If you want to create a new project, you go to File, Open Folder,

151
00:10:35,790 --> 00:10:37,820
creates a new empty folder.

152
00:10:42,160 --> 00:10:51,010
Let's name this python_basics2, select folder, and that will open a completely new project, and then

153
00:10:51,010 --> 00:10:53,380
here you are supposed to write a new program.

154
00:10:53,560 --> 00:10:57,760
If you close that, you open up Visual Studio Code again.

155
00:10:59,080 --> 00:11:06,580
It shows the previous project, so python_basics2.
If you want to open the previous project, you want

156
00:11:06,580 --> 00:11:07,630
to go to Open Recents.

157
00:11:07,840 --> 00:11:10,330
And then here you'll find python_basics.

158
00:11:10,720 --> 00:11:15,880
This one here, click that and it should open the previous project that we were working on.

159
00:11:16,120 --> 00:11:19,330
And that is how to write and run Python programs.

160
00:11:19,930 --> 00:11:20,700
Thanks for following!

161
00:11:20,710 --> 00:11:22,390
I'll talk to you in the next videos. See you.

