1 00:00:00,000 --> 00:00:02,700 Hello, I think we have learned enough 2 00:00:03,020 --> 00:00:05,960 that it's time for a little bit of a mini project, something 3 00:00:05,970 --> 00:00:09,500 fun. And so we're going to create our first little program. 4 00:00:09,800 --> 00:00:13,000 Now, I'm going to show you exactly how I'm going to create a program, 5 00:00:13,050 --> 00:00:15,590 and if you want to, you can download the source code, and 6 00:00:15,600 --> 00:00:17,240 you can modify it to your hearts content, 7 00:00:17,250 --> 00:00:19,190 or you can try to make one on your own as well. 8 00:00:19,220 --> 00:00:23,180 Now I'm going to show you how you can create a Python program, 9 00:00:23,180 --> 00:00:26,920 and then run it inside of your command line program. 10 00:00:26,930 --> 00:00:30,160 So effectively we will be creating a brand new program. 11 00:00:30,170 --> 00:00:31,780 So let's create a new file here. 12 00:00:31,790 --> 00:00:32,950 I'm just using VS Code. 13 00:00:32,960 --> 00:00:34,210 It's a regular text editor. 14 00:00:34,210 --> 00:00:35,500 There's nothing fancy about it. 15 00:00:35,560 --> 00:00:37,720 I'm going to assign the file type as Python. 16 00:00:37,720 --> 00:00:42,770 And let's go ahead and save this to my Desktop, because I'll 17 00:00:42,770 --> 00:00:45,300 be able to find that. 'program.py', 18 00:00:45,300 --> 00:00:48,000 'program_1.py' 19 00:00:48,100 --> 00:00:50,200 It's a boring name, but you know what, that's fine. 20 00:00:51,900 --> 00:00:54,800 Now, in this program, I want this to do a few things. 21 00:00:55,220 --> 00:00:59,150 I want it to ask for a sentence. 22 00:01:00,400 --> 00:01:08,700 I want it to print the length of the sentence, sentence. 23 00:01:09,430 --> 00:01:15,250 And then I want to write the sentence to a file. 24 00:01:15,700 --> 00:01:20,200 And actually, before that, what I wanted to do is ask for 25 00:01:20,200 --> 00:01:24,200 a file name, and we're just going to make sure that it's always a 'txt'. 26 00:01:24,200 --> 00:01:26,800 So just ask for the file name, but don't add the extension 27 00:01:26,810 --> 00:01:30,950 in there. And then write that sentence to the file, to the 28 00:01:31,130 --> 00:01:37,380 file, and then run the program from your command line. 29 00:01:37,390 --> 00:01:41,940 Now, this sounds like a lot, maybe, but it's actually really 30 00:01:41,950 --> 00:01:44,460 easy. The reason I wrote this out, is because these are actually steps 31 00:01:44,460 --> 00:01:47,550 for me. As a developer, I like to see these steps in front of me. 32 00:01:47,550 --> 00:01:51,590 Okay, so for my first step here, I'm just going to ask for a sentence, 33 00:01:51,600 --> 00:01:54,470 then print out the length of that sentence, and then I'm 34 00:01:54,480 --> 00:01:58,190 going to ask for a file name, write that sentence, the first 35 00:01:58,200 --> 00:02:02,400 sentence to the new file name, and then run the program from my command line. 36 00:02:02,400 --> 00:02:05,000 So this is actually a lot easier than we think it's going to be. So 37 00:02:05,050 --> 00:02:11,229 let's say 'sentence = input("Enter a sentence")', 38 00:02:11,800 --> 00:02:14,830 and then we can print out length. 39 00:02:15,140 --> 00:02:18,860 Oh, I hit something there. I did not want to run. 40 00:02:19,000 --> 00:02:23,700 We can print the length of 'sentence', and then we can 41 00:02:23,700 --> 00:02:24,800 ask for a file name. 42 00:02:24,800 --> 00:02:32,000 So 'file_name', not a 'file_bane', but a 'file_name = 43 00:02:32,000 --> 00:02:36,000 input("Enter a file name")' 44 00:02:36,080 --> 00:02:39,890 Now, I'm automatically going to append '.txt' to this, 45 00:02:39,900 --> 00:02:42,470 so whatever that 'file_name' turns out to be, I'm going to 46 00:02:42,480 --> 00:02:44,570 do 'file_name', because it's a string 47 00:02:44,580 --> 00:02:45,710 we know it's immutable, 48 00:02:45,720 --> 00:02:48,350 so we're just going to replace the first 'file_name' variable 49 00:02:48,380 --> 00:02:50,120 with the second 'file_name' variable, 50 00:02:50,520 --> 00:02:55,770 and I'm going to use an f-string for this. 'f"file_name.txt"', 51 00:02:56,100 --> 00:02:59,500 and for an f-string we put this in curly braces. 52 00:02:59,500 --> 00:03:01,800 [no audio] 53 00:03:01,800 --> 00:03:04,900 So we ask for a 'file_name', and then we need to write the 54 00:03:04,990 --> 00:03:05,880 'sentence' to a file. 55 00:03:05,880 --> 00:03:11,100 So let's go ahead and do that. 'with open(', whatever that 'file_name' is going to be, 56 00:03:11,180 --> 00:03:13,780 So let's go ahead and throw the 'file_name' in here because 57 00:03:13,790 --> 00:03:15,190 it's a variable, it's a reference. 58 00:03:15,200 --> 00:03:17,500 So this 'file_name' is going to point to here, which is going 59 00:03:17,510 --> 00:03:20,640 to point to here, which is going to point to here, which 60 00:03:20,640 --> 00:03:23,300 is going to be whatever you call your 'file_name'. 61 00:03:23,320 --> 00:03:28,950 We want to open it in the write mode 'as f'. 'f' for file. 62 00:03:29,460 --> 00:03:34,200 So 'f.write()', whatever that original 'sentence' is, that's 63 00:03:34,200 --> 00:03:35,400 this one up here. 64 00:03:35,440 --> 00:03:38,470 So whatever that original sentence is, it's going to go into 65 00:03:38,470 --> 00:03:44,500 this file. Then we do 'f.close', and we can print our file is done. 66 00:03:45,300 --> 00:03:52,700 "You've written", let's say the length of the 'sentence', 67 00:03:53,200 --> 00:03:58,700 so '', which we have not assigned yet. 68 00:03:58,700 --> 00:04:00,800 [no audio] 69 00:04:00,800 --> 00:04:02,200 This is going to be an f-string. 70 00:04:02,210 --> 00:04:06,740 "You have written characters to", and then 71 00:04:06,750 --> 00:04:09,380 the 'file_name', '' 72 00:04:10,700 --> 00:04:14,400 Now we just need to run this. Make sure this exists. 73 00:04:14,480 --> 00:04:16,720 So let's throw this into a variable as well. 74 00:04:16,730 --> 00:04:21,190 'sentence_length = len(sentence)', whatever 75 00:04:21,220 --> 00:04:22,510 that user ends up writing. 76 00:04:22,800 --> 00:04:25,800 And we don't need to print this because you can see there's two of them, 77 00:04:25,800 --> 00:04:28,800 [no audio] 78 00:04:28,800 --> 00:04:31,400 so when I highlight it in VS Code, it highlights over here, 79 00:04:31,480 --> 00:04:32,310 it's the exact same. 80 00:04:32,310 --> 00:04:35,000 So we can just use 'sentence_length', 81 00:04:35,000 --> 00:04:36,800 because it's the exact same anyways. 82 00:04:36,800 --> 00:04:38,800 [no audio] 83 00:04:38,800 --> 00:04:41,100 Now what I'm going to do is open up my terminal, 84 00:04:41,120 --> 00:04:42,190 my command line. 85 00:04:42,200 --> 00:04:45,770 If you're on Windows, you might want to use 'cmd', 'cmder', 86 00:04:45,780 --> 00:04:48,470 PowerShell, whatever sort of command line tool you want to use 87 00:04:48,480 --> 00:04:51,020 on Windows, and on Linux you're going to want to use 88 00:04:51,020 --> 00:04:54,300 bash or if you have something custom in there, you'll want to use that. 89 00:04:54,540 --> 00:04:57,540 Now I save this file to my Desktop. 90 00:04:57,780 --> 00:04:59,220 You can see that I'm in 'Users', 91 00:04:59,300 --> 00:05:02,500 'kalebtaulien', 'Desktop', 'program_1.py'. 92 00:05:02,500 --> 00:05:06,670 Now, the reason there's a video in this course, a crash course 93 00:05:06,670 --> 00:05:09,400 lesson on using the command line is because we need to use this. 94 00:05:09,400 --> 00:05:11,400 If I do 'ls -la', 95 00:05:11,400 --> 00:05:13,400 [no audio] 96 00:05:13,400 --> 00:05:16,500 where is my Desktop? Right there. 97 00:05:16,780 --> 00:05:24,070 So I can do 'cd desktop', 'ls -la'. Here's my 'program_1.py'. 98 00:05:24,500 --> 00:05:25,900 And all I have to do, 99 00:05:25,900 --> 00:05:27,400 well, first things first, I'm going to make sure I'm using 100 00:05:27,400 --> 00:05:28,500 the right version of Python, 101 00:05:28,520 --> 00:05:34,250 Python 3.7.2. I use 'python', and then 'program_1 102 00:05:34,260 --> 00:05:35,720 .py', that's the file name. 103 00:05:35,730 --> 00:05:38,720 And I'm going to run this, and it's going to say, "Enter a sentence:" 104 00:05:38,900 --> 00:05:41,300 Hello this is a sentence. 105 00:05:42,400 --> 00:05:44,200 Okay. 24 characters. 106 00:05:44,880 --> 00:05:46,170 "Enter a file name". 107 00:05:46,180 --> 00:05:48,120 'test_file_name'. 108 00:05:49,100 --> 00:05:53,340 I've written 24 characters to 'test_file_name.txt'. 109 00:05:53,580 --> 00:05:56,910 Let's do 'ls -la'. Hey, look at that. 110 00:05:56,920 --> 00:05:58,770 'test_file_name.txt'. 111 00:05:58,890 --> 00:06:00,720 Let's go ahead and open this in VS Code. 112 00:06:00,960 --> 00:06:01,980 There it is. 113 00:06:03,050 --> 00:06:04,770 24 characters. 114 00:06:04,780 --> 00:06:05,610 This is the sentence. 115 00:06:05,800 --> 00:06:09,300 And that is our very first Python program. 116 00:06:09,300 --> 00:06:11,500 And we're actually using a lot of things we've already used. 117 00:06:11,700 --> 00:06:13,200 We're using 'input'. 118 00:06:13,210 --> 00:06:14,760 We know it's a string. 119 00:06:15,100 --> 00:06:19,390 We're using an object property because it has a length. 120 00:06:19,400 --> 00:06:21,040 This one had 24 characters. 121 00:06:21,040 --> 00:06:24,400 We're using the function 'print', which is going to print the length. 122 00:06:24,400 --> 00:06:25,900 We have a 'file_name' in here. 123 00:06:25,900 --> 00:06:30,400 Again, we're using the 'input' function to grab someone's 124 00:06:30,400 --> 00:06:33,300 'file_name'. I need to add that space in there, that will drive me 125 00:06:33,360 --> 00:06:36,530 nuts. And then we're overwriting a string because a string 126 00:06:36,530 --> 00:06:37,900 cannot be changed. 127 00:06:37,920 --> 00:06:39,470 We have to overwrite the string. 128 00:06:39,760 --> 00:06:42,280 And so what we're doing, is we're saying there's going to be 129 00:06:42,290 --> 00:06:47,510 a new 'file_name' variable. But before this whole line is 130 00:06:47,520 --> 00:06:49,700 executed, it's going to reference this one. 131 00:06:49,710 --> 00:06:51,440 Now, this one actually already exists. 132 00:06:51,800 --> 00:06:55,070 So before creating this one, Python says, "Okay, I'm going to use 133 00:06:55,070 --> 00:06:58,900 this one that links to here and whatever that value is going to be. 134 00:06:58,900 --> 00:07:03,450 So that 'file_name' uses an f-string to take that 'file_name' 135 00:07:03,460 --> 00:07:05,160 and append '.txt' to it. 136 00:07:05,170 --> 00:07:07,560 So we're always going to write a '.txt' file. 137 00:07:08,390 --> 00:07:14,880 Then we opened up a new file handler with whatever that file 138 00:07:14,890 --> 00:07:18,200 name is going to be, based on whatever the user wrote with 139 00:07:18,210 --> 00:07:21,440 write mode enabled 'as f' for the file handler. 140 00:07:21,600 --> 00:07:25,900 Then we wrote the original sentence, the first input into that file. 141 00:07:26,000 --> 00:07:28,610 We closed it because that is a very good practice. 142 00:07:28,610 --> 00:07:32,100 And then at the end, we said, with an f-string, "You've written 143 00:07:32,110 --> 00:07:36,210 24 characters to the file name of what was it, 144 00:07:36,220 --> 00:07:38,400 'test_file_name.txt'. 145 00:07:38,760 --> 00:07:42,000 And that is a Python program in a nutshell. 146 00:07:42,240 --> 00:07:45,630 Now, what I would like you to do is create some sort of program 147 00:07:45,640 --> 00:07:47,190 that's similar to this. 148 00:07:47,200 --> 00:07:50,400 You can even write the exact same program if you like. 149 00:07:50,410 --> 00:07:54,370 I would just like you to get some hands on experience with 150 00:07:54,760 --> 00:07:56,650 using multiple different things in here. 151 00:07:56,660 --> 00:08:03,340 So we've got 'input', strings, object properties, files, f-strings. 152 00:08:03,350 --> 00:08:06,160 We've got all sorts of stuff in here, and we're sort of starting to 153 00:08:06,170 --> 00:08:07,210 put it all together. 154 00:08:07,380 --> 00:08:10,800 Don't forget, you can always just download my code, and execute 155 00:08:10,810 --> 00:08:12,870 this as well, and tinker around with it. 156 00:08:12,880 --> 00:08:15,970 If you're not totally confident with being able to write 157 00:08:15,980 --> 00:08:17,230 this right now, that's okay. 158 00:08:17,240 --> 00:08:20,580 A good way of learning is taking code like what I have, and 159 00:08:20,610 --> 00:08:25,020 just modifying it piece by piece, seeing how it changes when 160 00:08:25,030 --> 00:08:26,130 you change something. 161 00:08:26,140 --> 00:08:28,260 So go ahead, give that a shot. 162 00:08:28,520 --> 00:08:31,190 And when you're done, we can move on to that next lesson. 163 00:08:31,200 --> 00:08:35,299 Oh, and one last note, your program does not need to be complicated. 164 00:08:35,299 --> 00:08:38,900 It does not necessarily even need to have a file handler in there. 165 00:08:38,960 --> 00:08:42,650 Your program could just be something super simple. 166 00:08:42,650 --> 00:08:44,700 It asks for a name and says, "Hello, name". 167 00:08:44,740 --> 00:08:48,520 So don't feel like it needs to be super complicated or like, 168 00:08:48,530 --> 00:08:49,870 you need to impress anyone. 169 00:08:49,880 --> 00:08:51,370 This is all just for practice 170 00:08:51,380 --> 00:08:52,809 anyways. Good luck. 171 00:08:52,809 --> 00:08:55,500 And I will meet you in the next video.