1 00:00:00,000 --> 00:00:04,140 And let's now write our first Python program, this 2 00:00:04,140 --> 00:00:07,890 time using a text editor and not directly inside 3 00:00:07,920 --> 00:00:11,990 the Python Shell. So you have just installed PyCharm, 4 00:00:11,990 --> 00:00:14,730 and I'm going to just explain to you very 5 00:00:14,730 --> 00:00:17,160 quickly the different sections and what you need 6 00:00:17,160 --> 00:00:20,010 to get started. This is very little, okay, we are 7 00:00:20,010 --> 00:00:23,010 not going to use all the options of PyCharm, the 8 00:00:23,010 --> 00:00:26,490 goal is not here to make you a PyCharm expert. The 9 00:00:26,490 --> 00:00:29,760 goal is to learn Python. So let's keep that in 10 00:00:29,760 --> 00:00:35,793 mind. And let's keep things simple. So, on the PyCharm 11 00:00:35,793 --> 00:00:38,430 IDE here, the main section is of course, 12 00:00:38,430 --> 00:00:41,850 here, the Python text editor. So this is a text 13 00:00:41,880 --> 00:00:44,790 editor, you can write any text you want. We have 14 00:00:44,790 --> 00:00:48,450 an example here, okay. And you can see, you can 15 00:00:48,450 --> 00:00:52,200 zoom in and zoom out, okay, with ctrl, or command, 16 00:00:52,560 --> 00:00:56,100 and the scroll of your mouse, on the left, you 17 00:00:56,100 --> 00:00:59,280 have a navigation tree, where you can see your 18 00:00:59,310 --> 00:01:01,950 different files, or you can navigate between the 19 00:01:01,950 --> 00:01:05,099 different files of your project. So for now, we 20 00:01:05,099 --> 00:01:09,330 just have one file, which is named main.py, and as 21 00:01:09,330 --> 00:01:12,870 you can see, we have the .py extension, that is 22 00:01:12,870 --> 00:01:15,660 going to be the extension for all your Python 23 00:01:15,660 --> 00:01:18,690 files. So here, you can toggle that if you want, 24 00:01:18,720 --> 00:01:23,580 okay, you can, but is shorter. Then on the top here, 25 00:01:23,610 --> 00:01:26,910 you can choose which file you're going to execute. 26 00:01:27,150 --> 00:01:30,480 And with the play button, you can execute that 27 00:01:30,480 --> 00:01:33,900 file, okay, you can execute the code that is in 28 00:01:33,930 --> 00:01:36,450 that program. Okay, we are going to see that just 29 00:01:36,480 --> 00:01:40,350 a bit later. And then one important section you 30 00:01:40,350 --> 00:01:43,710 have on the bottom. You can see here Python 31 00:01:43,710 --> 00:01:47,190 Console. If you open this, you have a Python 32 00:01:47,190 --> 00:01:51,090 Shell. So you have a Python Shell directly inside 33 00:01:51,210 --> 00:01:54,180 your Python IDE. So this is going to be very 34 00:01:54,180 --> 00:01:57,150 convenient. If you just want to test one line of 35 00:01:57,150 --> 00:02:01,080 code, one small instruction, just open the Python 36 00:02:01,170 --> 00:02:04,290 Shell here with Python Console, write the 37 00:02:04,290 --> 00:02:06,870 instruction, test your thing and then come back to 38 00:02:06,870 --> 00:02:07,950 the text editor. 39 00:02:09,330 --> 00:02:13,170 Alright, and now let's write our first real Python 40 00:02:13,170 --> 00:02:16,050 program. I'm going to remove all of that, okay. 41 00:02:16,440 --> 00:02:19,890 This is automatically generated for you, but we're 42 00:02:19,890 --> 00:02:22,800 not going to use it. So back to our file, and now 43 00:02:22,830 --> 00:02:26,160 this file is empty, as you can see here. So you 44 00:02:26,160 --> 00:02:28,470 have the lines also on the left, which is very 45 00:02:28,470 --> 00:02:31,140 convenient. And so you can write many lines of 46 00:02:31,140 --> 00:02:34,350 code if you want. And let's start with the very 47 00:02:34,350 --> 00:02:40,140 basic Hello world. So the same thing I did before 48 00:02:40,140 --> 00:02:43,530 in the Python Shell, I'm just using Hello world 49 00:02:43,530 --> 00:02:47,040 here, the string Hello world. Okay, and to run 50 00:02:47,070 --> 00:02:50,400 that program, so to run the code inside, I'm going 51 00:02:50,400 --> 00:02:53,910 to click here. On the play button, and this is going 52 00:02:53,910 --> 00:02:59,370 to open a new tab here, tab named Run, and you can 53 00:02:59,370 --> 00:03:02,280 see, so the first line will be here you can see 54 00:03:02,280 --> 00:03:05,970 this is the Python executable. And this is the 55 00:03:05,970 --> 00:03:09,420 main.py. So this is basically the command that is 56 00:03:09,450 --> 00:03:14,130 executed to run that file. And then you have the 57 00:03:14,160 --> 00:03:17,580 output of your file, then you will have also at 58 00:03:17,580 --> 00:03:21,270 the end, Process finished with exit code, and a 59 00:03:21,270 --> 00:03:25,290 number. So zero means success. If you have 60 00:03:25,320 --> 00:03:28,530 anything other than zero, then you have an error 61 00:03:28,590 --> 00:03:30,780 in your code, and you will also see the error 62 00:03:30,810 --> 00:03:36,000 here. So now, well, we have Hello world. But why 63 00:03:36,030 --> 00:03:39,150 don't we see Hello world here. I'm going to open 64 00:03:39,150 --> 00:03:43,560 the Python Shell again do hello world, that is, and 65 00:03:43,560 --> 00:03:46,920 you can see hello world is printed here. So that's 66 00:03:46,950 --> 00:03:49,470 the big difference between the Python Shell and 67 00:03:49,470 --> 00:03:54,120 when you execute a Python program here from Python 68 00:03:54,120 --> 00:03:57,480 file. In the Python Shell, everything you write will 69 00:03:57,480 --> 00:04:01,410 be evaluated and printed. In a Python program, well, 70 00:04:01,500 --> 00:04:04,140 this is not the case. If you want to print 71 00:04:04,140 --> 00:04:07,620 something, you actually have to explicitly say 72 00:04:07,620 --> 00:04:11,490 that you want to print that thing. And to do that 73 00:04:11,490 --> 00:04:15,630 it is very simple, you use the print function. So 74 00:04:15,630 --> 00:04:19,320 print parenthesis and then the string you want. 75 00:04:19,410 --> 00:04:23,400 Okay, I'm going to make it a bit bigger. So print 76 00:04:23,430 --> 00:04:26,130 Hello world, and now I'm going to run it again and 77 00:04:26,130 --> 00:04:30,480 you can see Hello world. So I can go to a newline 78 00:04:30,480 --> 00:04:34,050 so we're going to write each, so let's write Test, 79 00:04:34,530 --> 00:04:38,070 we are going to write each instruction in a new 80 00:04:38,070 --> 00:04:41,130 line okay. So now if I run this, you can see Hello 81 00:04:41,130 --> 00:04:44,910 world, Test. So as you can see, and this is very 82 00:04:44,970 --> 00:04:48,960 basic, each line is going to be executed one by 83 00:04:48,960 --> 00:04:52,740 one. So first the line one and then the line two, 84 00:04:52,800 --> 00:04:56,520 etc. So the order is very important. Okay. This 85 00:04:56,520 --> 00:04:59,970 line is going to be executed before that one. Now 86 00:05:00,000 --> 00:05:04,200 one nice functionality you get with the IDE is the 87 00:05:04,260 --> 00:05:06,930 auto completion. So what is the auto completion? 88 00:05:07,350 --> 00:05:10,620 So when you want to write print, for example, 89 00:05:10,620 --> 00:05:13,980 you can see here, on the bottom, we have some 90 00:05:13,980 --> 00:05:17,700 help, okay. You have some suggestions of what you 91 00:05:17,700 --> 00:05:20,910 could want to do, okay. And you can see here we 92 00:05:20,910 --> 00:05:24,300 have print. So the first suggestion, if you wants 93 00:05:24,300 --> 00:05:27,810 to write this, you can simply here, press on tab, 94 00:05:28,170 --> 00:05:32,070 and this is going to auto complete the function, 95 00:05:32,430 --> 00:05:35,520 whatever you need to write, okay. So this is the 96 00:05:35,580 --> 00:05:38,130 auto completion, and you're going to use that a 97 00:05:38,130 --> 00:05:40,800 lot. Alright, and now what I'm going to do, I'm 98 00:05:40,800 --> 00:05:44,310 going to simply remove that, and I'm going to 99 00:05:44,310 --> 00:05:48,480 rewrite one of the exercise we had to do in the 100 00:05:48,480 --> 00:05:52,170 last section. So we simply asked the user for the 101 00:05:52,170 --> 00:05:56,430 name, for the age, and then we'll print a nice 102 00:05:56,460 --> 00:05:59,790 string to say, for example, Hello, Bob, you are 103 00:05:59,880 --> 00:06:03,330 25. So now instead of doing that in the Python 104 00:06:03,330 --> 00:06:06,660 Shell and write each instruction one by one, I'm 105 00:06:06,660 --> 00:06:10,920 going to write all the instructions in that file, 106 00:06:10,950 --> 00:06:14,370 okay with the text editor, and then run the Python 107 00:06:14,370 --> 00:06:19,620 program. Okay, so user_name is equal to input, 108 00:06:19,680 --> 00:06:27,000 let's say, What is your name? And then the user_age is 109 00:06:27,000 --> 00:06:34,200 equal to input("How old are you?"). Okay, so I put 110 00:06:34,230 --> 00:06:37,710 each instruction in a separate line, and then I 111 00:06:37,710 --> 00:06:42,570 can do print. So let's say I want to say Hello, 112 00:06:43,710 --> 00:06:46,620 and then I'm going to put plus user_name, 113 00:06:48,060 --> 00:06:55,320 plus comma you are, and then plus user_age. As you 114 00:06:55,320 --> 00:06:58,170 can see, I also have the auto completion for the 115 00:06:58,170 --> 00:07:00,960 variables that I have created before. So I can 116 00:07:00,960 --> 00:07:04,350 just press tab here, and I have user_age. 117 00:07:04,650 --> 00:07:07,170 Alright, one thing to note here, if you don't have 118 00:07:07,170 --> 00:07:11,730 to write on every line, okay. Here, I just kind of 119 00:07:12,090 --> 00:07:15,660 group those two instructions together again, then 120 00:07:15,660 --> 00:07:19,020 I put a new space, and that instruction. This is 121 00:07:19,020 --> 00:07:23,280 just for clarity. Okay, so it is easier to read. 122 00:07:23,940 --> 00:07:28,920 So now, I'm going to press here on Run. And you 123 00:07:28,920 --> 00:07:31,320 can see What is your name? So now I need to say, 124 00:07:31,380 --> 00:07:36,270 say, Bob. How old are you? 25. Hello Bob, you are 125 00:07:36,300 --> 00:07:39,810 25. Okay, so now as you can see, this is better 126 00:07:39,810 --> 00:07:42,930 than the Python Shell, okay, because here, we will 127 00:07:42,930 --> 00:07:45,660 be able to easily modify the program, okay. We can 128 00:07:45,690 --> 00:07:49,050 add new line, modify existing line, etc. And we 129 00:07:49,050 --> 00:07:52,560 can just save that and run this. Okay, if I want, 130 00:07:52,560 --> 00:07:56,160 I can run it again. What is the name? John. How 131 00:07:56,160 --> 00:08:01,530 old are you? 29. Hello John, you are 29. Okay. So 132 00:08:01,530 --> 00:08:03,630 you don't need to write the program again to 133 00:08:04,020 --> 00:08:07,680 execute it again. Okay, and what you can do to 134 00:08:07,710 --> 00:08:10,500 save the files. Normally, this is going to save 135 00:08:10,740 --> 00:08:14,430 automatically, you can just click here, Save All 136 00:08:14,430 --> 00:08:18,360 with or press Ctrl S. And if you want to reopen 137 00:08:18,360 --> 00:08:21,060 that project, so let's say you go on File, Closed 138 00:08:21,060 --> 00:08:24,720 Project, you can just open PyCharm, and it's 139 00:08:24,720 --> 00:08:27,030 going to open the project for you, or you can go 140 00:08:27,030 --> 00:08:30,000 on the project and just click on the project you 141 00:08:30,000 --> 00:08:33,419 have, and this is going to come back to your 142 00:08:33,450 --> 00:08:36,120 current project. Okay, if you click here, you can 143 00:08:36,150 --> 00:08:41,039 also see the exact path where your project is 144 00:08:41,039 --> 00:08:45,180 saved. Okay, so in Users, so you username, and 145 00:08:45,180 --> 00:08:48,000 then PyCharm Projects, my_first_project and then 146 00:08:48,000 --> 00:08:52,470 you have another folder here and the main.py. So 147 00:08:52,470 --> 00:08:55,800 that is where you can find your file. Alright, and 148 00:08:55,830 --> 00:08:58,800 one last thing I want to show you is, let's say you 149 00:08:58,800 --> 00:09:01,800 have an error. So let's say forget this, you can 150 00:09:01,800 --> 00:09:04,350 see if we have an error, then you're going to see 151 00:09:04,350 --> 00:09:08,547 something in red. And we did it there, we have an SyntaxError, 152 00:09:08,547 --> 00:09:13,500 EOL means an offline and well this is 153 00:09:13,530 --> 00:09:15,630 just it's going to show you where you have the 154 00:09:15,660 --> 00:09:18,960 error and an explanation that can be explicit or 155 00:09:18,960 --> 00:09:21,990 not depends. And you will see here exit code 156 00:09:21,990 --> 00:09:25,560 number one. Also you are going to see a red 157 00:09:25,620 --> 00:09:29,580 underline here, so you can easily spot the errors 158 00:09:29,580 --> 00:09:32,550 and once you correct the error, that's going to work 159 00:09:32,940 --> 00:09:35,700 as expected. Okay, great. Now you know how to 160 00:09:35,700 --> 00:09:39,330 write a Python program in a text editor, how to 161 00:09:39,330 --> 00:09:42,390 save it, how to run it, and also how to use the 162 00:09:42,420 --> 00:09:47,434 auto completion and a few features of a Python IDE.