Transcript for:
Getting Started with Python Programming

Hello there, I'm Gary Sims and this is Gary Explains. Now, have you ever thought about learning to program? Well, one of the easiest ways to learn to program is using the Python programming language.

So what I want to do today is show you how to download and install Python for Windows and then write a very simple program that will just give you a flavor, a taste of what you can achieve using a language like Python. So if you want to take your first steps in writing a Python program on a Windows desktop, please let me explain. So the first thing we do is download Python for our Windows machine.

So you go over to python.org and then you click here in the download on the menu bar there and you'll be offered two downloads. One is Python 3.6.5 and one is Python 2.7.14. Historically, Python 2 is the older one and really development has moved over to Python 3. But if you have systems that are already using Python 2, they still offer it.

So you can use it. We want to download Python 3.6.5. So click on the download and then that will download the XE, run the XE and that will install Python on your Windows machine.

Now once you've done that you're looking for this program called Idle. I've got it here on my desktop. If you don't find it on your desktop then look for it in your start menu and you just double click on Idle and that will bring up this window here which is called the shell and it's got these three chevron prompts on it. Now that in itself is not very interesting. What we want to do is start writing a program and to do that we go to file new file and that will bring up an editor okay and what we're going to do now is in here we write our python program so the first thing we do just to get something going is we're going to write a guessing game and the idea is the computer will guess a number between 1 and 10 and then the user has to guess it so we're going to say print printout on the screen i am thinking of a number Between 1 and 10. Now, obviously, the program is going to be more complicated than that.

But let's just show you now what we can do at this point. You go to File, and then you want to save it. And you want to save it here. It's giving you the default Python directory. You might want to save it somewhere else where you're keeping all your source code.

I'm going to quickly put it in here and call it guess.py. And now what you do is you go to Run and then Run Module. and then over here on the left in this uh shell area you'll see it says it's printed out i am thinking of a number between one and ten so here is where the program runs and here is where you write it now we're gonna go ahead quickly and just change this a bit so we can write the whole program first we need to import uh the ability to pick random numbers so you do that by calling import random and then we're gonna say n that's a variable where we're gonna store this number n for number we're gonna say n random dot random Int at 1 10 and if you think about it that says I want a random number between 1 and 10 and that's good because that's what we've told the computer what we want.

Now we're going to define here a variable called a boolean which basically tells you whether the game is still being played so we're saying running is equal to true yes it's true and then we're going to say while running so while the game is running then do the following thing so a while loop goes round and round and round while the condition is true. And in this particular case, running is actually true at the moment. And we say guess string is equal to input.

So that means get something from the user. And there needs to be a prompt. I'm going to say take a guess. Leave a space there so it looks nice on the thing.

And that will then ask the user to type in a number. It comes in as a string. So we're now going to convert that into an integer, which means a whole number.

And we pass in the guess string into that. Okay, and then we say this, if guess, the number they've guessed, is equal to, double equal to, is equal to n, the number that we've said, then what do we say? We say print.

Well done, that is right, exclamation mark, and now we want to say running is equal to false, because the game is over, but if they didn't guess it right, then we want to give them a clue, so we say else if, el if, guess. Is less than n, colon, then we say print, try a bigger number. Give them a hint.

Now, I just want to say at this point that all these tabs that you see here, you can see how this while here is at the beginning, and then here we've got indented once, and then here we've got indented. That's very important with Python. That's how Python knows what code to execute. Under each condition so these two lines here are indented because they get done if the user guessed right this line will get done Here if the guess is less than and now we're going to the next level we're going to say else Okay, and we say and again indented in print try a smaller number So if they get it right they get the game ends if they get it wrong It tells them either to try or a bigger or a smaller number and that's it.

That's the whole program written So let's just save it and now let's just run it and let's see it in action Okay, I'm thinking of a number between 1 and 10. Okay, let's try 5. Try a bigger number. Okay, 7. A bigger number. Okay, 9. Well done, that's right. There we go.

We guessed it. Let's just check to see what happens if we try a smaller number. So run the module again.

I don't know, 1. Try a bigger number, 10. Try a smaller number. Good, so we know that works. Okay, 6. Okay, 7, 8, 9. Well done, you got it right.

Now, of course, this program does no error checking so you know if we ran this again here it says i'm thinking of and i typed in the word you know fred then it just crashes always error stuff coming out here and read we won't get into that now but the program is very simple it doesn't check for stupid answers it doesn't check for anything but if you do type in the right thing there you go a very simple python program so well then you've written your first pro python program on windows do go ahead and explore python because there's lots to learn And as you can see, it's very easy to get into actually writing a program. Just a few minutes, you can have something up and running. Well, there you go.

I hope you enjoyed that. All the source code will be posted down here in the description in case you didn't catch everything I was typing in there. Obviously we could go much, much deeper into Python.

Please tell me if that's something that interests you. Let me know in the comments below. Please give this video a thumbs up if you think it deserved it.

Please do subscribe. It'd be really great if you could hit that bell notification icon to become part of the notification squad here at Gary Explains. Okay, that's about it.

I'll see you in the next one.