1 00:00:00,000 --> 00:00:02,100 Hi. Welcome back. 2 00:00:02,101 --> 00:00:04,260 In this video, I'll show you how to build 3 00:00:04,261 --> 00:00:07,818 a Python program and how to execute that Python 4 00:00:07,819 --> 00:00:09,946 program so that you can get the output. 5 00:00:09,947 --> 00:00:13,028 Obviously, this program will be very simple because 6 00:00:13,029 --> 00:00:15,476 the focus here is to show you the 7 00:00:15,477 --> 00:00:20,000 process of creating a program and executing it. 8 00:00:20,690 --> 00:00:23,972 To build a program in Python, you need two things. 9 00:00:23,973 --> 00:00:31,330 One is Python and the other is an IDE or a text editor. 10 00:00:31,331 --> 00:00:34,950 An IDE is just an advanced text editor. 11 00:00:35,770 --> 00:00:38,928 So I'll use Visual Studio Code as the 12 00:00:38,929 --> 00:00:43,072 IDE, which is this one in here. 13 00:00:43,073 --> 00:00:46,288 And then I will create a project here. 14 00:00:46,289 --> 00:00:48,624 A project is nothing more than 15 00:00:48,625 --> 00:00:52,110 just creating an empty directory. 16 00:00:52,111 --> 00:00:54,516 So first you want to go to your 17 00:00:54,517 --> 00:00:57,972 file system, Windows Explorer or Mac Finder and 18 00:00:57,973 --> 00:00:59,402 make sure you have an empty folder. 19 00:00:59,403 --> 00:01:01,556 So First Project is the name of 20 00:01:01,557 --> 00:01:06,622 this empty directory on Windows Explorer. 21 00:01:06,623 --> 00:01:09,752 And then you want to go back to Visual Studio Code. 22 00:01:09,753 --> 00:01:11,352 If you are on another IDE, 23 00:01:11,353 --> 00:01:14,260 the process is basically the same. 24 00:01:14,890 --> 00:01:19,858 You go to File and then you go to Open Folder. On Mac, 25 00:01:19,859 --> 00:01:22,440 it could be written just Open. 26 00:01:23,850 --> 00:01:27,052 So click on that, and then you want to 27 00:01:27,053 --> 00:01:31,088 browse to that empty folder, double click it, and 28 00:01:31,089 --> 00:01:35,280 then select folder, and Visual Studio code, 29 00:01:35,281 --> 00:01:38,272 what it will do is, it will open that 30 00:01:38,273 --> 00:01:41,840 folder as the root directory of the project. 31 00:01:43,090 --> 00:01:44,740 You have this warning here. 32 00:01:44,741 --> 00:01:47,550 It's fine to say, yes, I trust the authors. 33 00:01:49,250 --> 00:01:52,868 You can see now that FIRST PROJECT, the name of 34 00:01:52,869 --> 00:01:56,870 the folder is the name of the project as well. 35 00:01:56,871 --> 00:01:58,840 So what I'll do now is 36 00:01:58,841 --> 00:02:01,256 write a program, a Python program, 37 00:02:01,257 --> 00:02:04,360 and this consists of creating a text 38 00:02:04,361 --> 00:02:08,120 file where you write that actual program. 39 00:02:09,130 --> 00:02:12,028 To create that text file, you can go 40 00:02:12,029 --> 00:02:15,292 to your folder again and create it here. 41 00:02:15,293 --> 00:02:18,432 Or normally what we do is we 42 00:02:18,433 --> 00:02:21,580 create that file from within the IDE. 43 00:02:22,190 --> 00:02:26,064 So if you click here, then write a 44 00:02:26,065 --> 00:02:31,356 name for the file, such as my_program. 45 00:02:32,850 --> 00:02:36,052 It's not recommended to use spaces in 46 00:02:36,053 --> 00:02:38,234 the names all over the files. 47 00:02:38,235 --> 00:02:41,988 So please always use underscores instead of a space. 48 00:02:41,989 --> 00:02:47,860 And then let's dot, try txt, and see if this will work. 49 00:02:48,710 --> 00:02:51,896 The actual extension is .py, but 50 00:02:51,897 --> 00:02:53,570 that is not a requirement. 51 00:02:54,390 --> 00:02:57,752 Of course we will use .py always, always. 52 00:02:57,753 --> 00:03:02,108 But for now, let's keep it at .txt so 53 00:03:02,109 --> 00:03:05,298 that you understand a very crucial concept about Python. 54 00:03:05,299 --> 00:03:08,512 So I'll press Enter, and here I 55 00:03:08,513 --> 00:03:12,510 will do print(3+4). 56 00:03:12,511 --> 00:03:14,300 And that's our program. 57 00:03:14,910 --> 00:03:17,808 So this program simply outputs the 58 00:03:17,809 --> 00:03:20,880 sum of 3 and 4. 59 00:03:22,530 --> 00:03:25,790 How can I execute this program now to get the results? 60 00:03:26,450 --> 00:03:30,756 Well, you see this Terminal menu item here? 61 00:03:30,757 --> 00:03:33,934 You press that and go to New Terminal. 62 00:03:33,935 --> 00:03:37,592 And that should open a new terminal in here. 63 00:03:37,593 --> 00:03:39,544 So that's like the command line 64 00:03:39,545 --> 00:03:42,488 where you interact with your computer. 65 00:03:42,489 --> 00:03:49,154 And here, now we can write py -3 if you are on Windows. 66 00:03:49,155 --> 00:03:53,400 If you are on Mac, you should write python3. 67 00:03:54,410 --> 00:03:56,668 So I'm on Windows here, so I'll use 68 00:03:56,669 --> 00:04:05,718 py -3, and then my_program, actually I can use the tab. 69 00:04:05,719 --> 00:04:07,894 Now, if I press the tab, 70 00:04:07,895 --> 00:04:11,200 that will autocomplete the file name. 71 00:04:12,130 --> 00:04:14,554 This part here is not necessary. 72 00:04:14,555 --> 00:04:17,971 The dot and the backslash, you can leave 73 00:04:17,972 --> 00:04:20,298 it, it's fine, but even if you remove 74 00:04:20,299 --> 00:04:23,410 it like that, it will still work. 75 00:04:23,411 --> 00:04:28,552 So now if you press Enter here, what we 76 00:04:28,553 --> 00:04:30,900 get is we go to the next line. 77 00:04:31,510 --> 00:04:33,288 Perhaps you were waiting for the 78 00:04:33,289 --> 00:04:34,840 output to show in here. 79 00:04:34,841 --> 00:04:39,634 It didn't show up because the file is still empty. 80 00:04:39,635 --> 00:04:42,444 We haven't saved the changes. 81 00:04:42,445 --> 00:04:44,012 So after I wrote this line 82 00:04:44,013 --> 00:04:45,660 of code, I didn't save anything. 83 00:04:45,661 --> 00:04:47,888 So what you want to do is you want 84 00:04:47,889 --> 00:04:51,952 to go to File, Save or Control s and 85 00:04:51,953 --> 00:04:56,928 then press the upper arrow key in here to 86 00:04:56,929 --> 00:05:01,490 get that previously executed command and press Enter. 87 00:05:01,491 --> 00:05:04,000 And then we get the output 7. 88 00:05:04,690 --> 00:05:08,180 So as you can see, a Python program is nothing 89 00:05:08,181 --> 00:05:12,114 more, but just a text file that you write, 90 00:05:12,115 --> 00:05:15,433 [No audio] 91 00:05:15,434 --> 00:05:18,326 and then you execute it using the Python command. 92 00:05:18,327 --> 00:05:21,302 But there is a nicer way to execute 93 00:05:21,303 --> 00:05:25,952 Python programs, at least on Visual Studio code, 94 00:05:25,953 --> 00:05:28,740 and that is by using a run button. 95 00:05:28,741 --> 00:05:30,852 Currently there is no run button here because 96 00:05:30,853 --> 00:05:33,890 we need to install a Python extension. 97 00:05:33,891 --> 00:05:39,176 So if you go over to this icon here, extensions, and 98 00:05:39,177 --> 00:05:42,966 here you want to search for python, press Enter, 99 00:05:42,967 --> 00:05:44,966 [No audio] 100 00:05:44,967 --> 00:05:48,424 and this first result is the extension we want to install. 101 00:05:48,425 --> 00:05:50,766 So press on the Install button, 102 00:05:50,767 --> 00:05:56,866 [No audio] 103 00:05:56,867 --> 00:05:59,210 and that will install the Python extension. 104 00:05:59,211 --> 00:06:02,026 But what is the Python extension? 105 00:06:02,027 --> 00:06:04,628 Well, I told you that this will 106 00:06:04,629 --> 00:06:06,804 show the Run button somewhere in here. 107 00:06:06,805 --> 00:06:10,696 It didn't show it because my 108 00:06:10,697 --> 00:06:14,530 file doesn't have a .py extension. 109 00:06:15,350 --> 00:06:20,962 So the Python extension searches for .py files. 110 00:06:20,963 --> 00:06:23,356 So what you want to do is right click here, 111 00:06:23,357 --> 00:06:30,040 rename to .py and now we see that button. 112 00:06:30,041 --> 00:06:32,066 [No audio] 113 00:06:32,090 --> 00:06:34,012 We also get this message that we 114 00:06:34,013 --> 00:06:36,082 need to select a Python interpreter. 115 00:06:36,083 --> 00:06:39,760 So to select a Python interpreter, you either press that button 116 00:06:39,761 --> 00:06:43,168 there or if you didn't get that window, what you want 117 00:06:43,169 --> 00:06:45,744 to do is, you want to go to this icon here, 118 00:06:45,745 --> 00:06:50,110 this curly bracket, and then go to Select Interpreter. 119 00:06:50,770 --> 00:06:54,596 And then you want to select the python 3.10.3 120 00:06:54,597 --> 00:06:58,132 interpreter, which we installed in the previous video. 121 00:06:58,133 --> 00:07:01,054 Perhaps in your case it's another version. 122 00:07:01,055 --> 00:07:04,040 So whatever Python interpreter you're using, select 123 00:07:04,041 --> 00:07:07,460 it, and then you're ready to run 124 00:07:07,990 --> 00:07:10,200 this program using that button. 125 00:07:10,201 --> 00:07:11,300 So press that button 126 00:07:11,301 --> 00:07:13,466 [No audio] 127 00:07:13,467 --> 00:07:17,000 and that's the output 7 down here. 128 00:07:18,090 --> 00:07:19,772 So as you can see, there are 129 00:07:19,773 --> 00:07:22,258 two ways to run your Python programs. 130 00:07:22,259 --> 00:07:25,088 Either you use the python command, as we did 131 00:07:25,089 --> 00:07:28,512 with py -3, and then you mention the 132 00:07:28,513 --> 00:07:32,182 name of the file, or you use the easier 133 00:07:32,183 --> 00:07:34,768 way, which is by using this button. 134 00:07:34,769 --> 00:07:38,100 But as you so to be able to use that button, 135 00:07:38,101 --> 00:07:43,460 you need to name your files with .py at the end. 136 00:07:43,461 --> 00:07:45,450 And that's actually the best practice. 137 00:07:45,451 --> 00:07:47,818 Everyone names their files .py. 138 00:07:47,819 --> 00:07:49,006 It's like a convention. 139 00:07:49,007 --> 00:07:52,510 It's not a requirement, but it is a hard convention. 140 00:07:52,511 --> 00:07:55,454 And as you saw, things like extensions, 141 00:07:55,455 --> 00:07:58,696 IDE extensions, also will work better if 142 00:07:58,697 --> 00:08:01,590 you have your files named with .py. 143 00:08:02,330 --> 00:08:06,044 The other advantage of using this button is that 144 00:08:06,045 --> 00:08:10,140 if you had more code, such as 4 plus 145 00:08:10,141 --> 00:08:12,892 9, you don't have to save the code, you 146 00:08:12,893 --> 00:08:15,760 just run it and you'll get the result. 147 00:08:15,761 --> 00:08:17,790 7 and 13. 148 00:08:17,791 --> 00:08:19,728 So the sum of that line, and 149 00:08:19,729 --> 00:08:21,500 the sum of that line as well. 150 00:08:22,030 --> 00:08:24,944 If you want to create another program, what you want 151 00:08:24,945 --> 00:08:26,980 to do is you want to go to File again, 152 00:08:26,981 --> 00:08:30,618 Open Folder and make sure you have an empty folder, 153 00:08:30,619 --> 00:08:33,476 or just create one on the fly here. 154 00:08:33,477 --> 00:08:36,943 So Folder, Second Project, 155 00:08:36,944 --> 00:08:38,866 [No audio] 156 00:08:38,900 --> 00:08:40,966 double click it, select folder, 157 00:08:40,991 --> 00:08:45,496 and that will close that current window, current project, 158 00:08:45,497 --> 00:08:47,768 and it will open the SECOND PROJECT. 159 00:08:47,769 --> 00:08:48,872 So this one in here. 160 00:08:48,873 --> 00:08:50,770 Again, you create a file. 161 00:08:52,330 --> 00:08:58,748 my_program.py, using .py always, select an 162 00:08:58,749 --> 00:09:03,210 interpreter using this curly brackets and go there. 163 00:09:03,211 --> 00:09:06,316 Choose always the same interpreter that you used 164 00:09:06,317 --> 00:09:10,402 previously, unless you want to select another interpreter, 165 00:09:10,403 --> 00:09:14,082 if you have many interpreters and 166 00:09:14,083 --> 00:09:19,133 [No audio] 167 00:09:19,134 --> 00:09:22,794 you write some code, run it and you should see the output 168 00:09:22,795 --> 00:09:26,052 here on the command line. With that, 169 00:09:26,053 --> 00:09:27,972 I thank you for watching this video, and 170 00:09:27,973 --> 00:09:29,200 I'll talk to you in the next one.