Hello, I think we have learned enough
that it's time for a little bit of a mini project, something
fun. And so we're going to create our first little program.
Now, I'm going to show you exactly how I'm going to create a program,
and if you want to, you can download the source code, and
you can modify it to your hearts content,
or you can try to make one on your own as well.
Now I'm going to show you how you can create a Python program,
and then run it inside of your command line program.
So effectively we will be creating a brand new program.
So let's create a new file here.
I'm just using VS Code.
It's a regular text editor.
There's nothing fancy about it.
I'm going to assign the file type as Python.
And let's go ahead and save this to my Desktop, because I'll
be able to find that. 'program.py',
'program_1.py'
It's a boring name, but you know what, that's fine.
Now, in this program, I want this to do a few things.
I want it to ask for a sentence.
I want it to print the length of the sentence, sentence.
And then I want to write the sentence to a file.
And actually, before that, what I wanted to do is ask for
a file name, and we're just going to make sure that it's always a 'txt'.
So just ask for the file name, but don't add the extension
in there. And then write that sentence to the file, to the
file, and then run the program from your command line.
Now, this sounds like a lot, maybe, but it's actually really
easy. The reason I wrote this out, is because these are actually steps
for me. As a developer, I like to see these steps in front of me.
Okay, so for my first step here, I'm just going to ask for a sentence,
then print out the length of that sentence, and then I'm
going to ask for a file name, write that sentence, the first
sentence to the new file name, and then run the program from my command line.
So this is actually a lot easier than we think it's going to be. So
let's say 'sentence = input("Enter a sentence")',
and then we can print out length.
Oh, I hit something there. I did not want to run.
We can print the length of 'sentence', and then we can
ask for a file name.
So 'file_name', not a 'file_bane', but a 'file_name =
input("Enter a file name")'
Now, I'm automatically going to append '.txt' to this,
so whatever that 'file_name' turns out to be, I'm going to
do 'file_name', because it's a string
we know it's immutable,
so we're just going to replace the first 'file_name' variable
with the second 'file_name' variable,
and I'm going to use an f-string for this. 'f"file_name.txt"',
and for an f-string we put this in curly braces.
[no audio]
So we ask for a 'file_name', and then we need to write the
'sentence' to a file.
So let's go ahead and do that. 'with open(', whatever that 'file_name' is going to be,
So let's go ahead and throw the 'file_name' in here because
it's a variable, it's a reference.
So this 'file_name' is going to point to here, which is going
to point to here, which is going to point to here, which
is going to be whatever you call your 'file_name'.
We want to open it in the write mode 'as f'. 'f' for file.
So 'f.write()', whatever that original 'sentence' is, that's
this one up here.
So whatever that original sentence is, it's going to go into
this file. Then we do 'f.close', and we can print our file is done.
"You've written", let's say the length of the 'sentence',
so '', which we have not assigned yet.
[no audio]
This is going to be an f-string.
"You have written characters to", and then
the 'file_name', ''
Now we just need to run this. Make sure this exists.
So let's throw this into a variable as well.
'sentence_length = len(sentence)', whatever
that user ends up writing.
And we don't need to print this because you can see there's two of them,
[no audio]
so when I highlight it in VS Code, it highlights over here,
it's the exact same.
So we can just use 'sentence_length',
because it's the exact same anyways.
[no audio]
Now what I'm going to do is open up my terminal,
my command line.
If you're on Windows, you might want to use 'cmd', 'cmder',
PowerShell, whatever sort of command line tool you want to use
on Windows, and on Linux you're going to want to use
bash or if you have something custom in there, you'll want to use that.
Now I save this file to my Desktop.
You can see that I'm in 'Users',
'kalebtaulien', 'Desktop', 'program_1.py'.
Now, the reason there's a video in this course, a crash course
lesson on using the command line is because we need to use this.
If I do 'ls -la',
[no audio]
where is my Desktop? Right there.
So I can do 'cd desktop', 'ls -la'. Here's my 'program_1.py'.
And all I have to do,
well, first things first, I'm going to make sure I'm using
the right version of Python,
Python 3.7.2. I use 'python', and then 'program_1
.py', that's the file name.
And I'm going to run this, and it's going to say, "Enter a sentence:"
Hello this is a sentence.
Okay. 24 characters.
"Enter a file name".
'test_file_name'.
I've written 24 characters to 'test_file_name.txt'.
Let's do 'ls -la'. Hey, look at that.
'test_file_name.txt'.
Let's go ahead and open this in VS Code.
There it is.
24 characters.
This is the sentence.
And that is our very first Python program.
And we're actually using a lot of things we've already used.
We're using 'input'.
We know it's a string.
We're using an object property because it has a length.
This one had 24 characters.
We're using the function 'print', which is going to print the length.
We have a 'file_name' in here.
Again, we're using the 'input' function to grab someone's
'file_name'. I need to add that space in there, that will drive me
nuts. And then we're overwriting a string because a string
cannot be changed.
We have to overwrite the string.
And so what we're doing, is we're saying there's going to be
a new 'file_name' variable. But before this whole line is
executed, it's going to reference this one.
Now, this one actually already exists.
So before creating this one, Python says, "Okay, I'm going to use
this one that links to here and whatever that value is going to be.
So that 'file_name' uses an f-string to take that 'file_name'
and append '.txt' to it.
So we're always going to write a '.txt' file.
Then we opened up a new file handler with whatever that file
name is going to be, based on whatever the user wrote with
write mode enabled 'as f' for the file handler.
Then we wrote the original sentence, the first input into that file.
We closed it because that is a very good practice.
And then at the end, we said, with an f-string, "You've written
24 characters to the file name of what was it,
'test_file_name.txt'.
And that is a Python program in a nutshell.
Now, what I would like you to do is create some sort of program
that's similar to this.
You can even write the exact same program if you like.
I would just like you to get some hands on experience with
using multiple different things in here.
So we've got 'input', strings, object properties, files, f-strings.
We've got all sorts of stuff in here, and we're sort of starting to
put it all together.
Don't forget, you can always just download my code, and execute
this as well, and tinker around with it.
If you're not totally confident with being able to write
this right now, that's okay.
A good way of learning is taking code like what I have, and
just modifying it piece by piece, seeing how it changes when
you change something.
So go ahead, give that a shot.
And when you're done, we can move on to that next lesson.
Oh, and one last note, your program does not need to be complicated.
It does not necessarily even need to have a file handler in there.
Your program could just be something super simple.
It asks for a name and says, "Hello, name".
So don't feel like it needs to be super complicated or like,
you need to impress anyone.
This is all just for practice
anyways. Good luck.
And I will meet you in the next video.