From the course: Programming Foundations: Fundamentals

Running Python on the command line on Windows - Python Tutorial

From the course: Programming Foundations: Fundamentals

Running Python on the command line on Windows

- [Instructor] One way to execute or run your Python code is by using the command line. I'll be referring to it as the terminal or command line throughout this course. You can access it on your Windows machine by going to the Start menu and then navigating to the Python folder. When you get to the dropdown, choose Python 64 bit. This is going to open up the Python shell. The Python shell will display the prompt, which lets us know that the interpreter is ready for our Python instructions. Unlike the Mac, you don't need to type anything else to start sending Python commands. We'll start out by trying to get the sum of two plus two. Let's see what happens when we type what is two plus two? And hit Enter. Well, we get a syntax error from the Python interpreter. We call that syntax refers to the rules of a programming language. Syntax errors happen when you write code that breaks the expected rules. In this case, we wrote our code in plain English but the interpreter doesn't understand English, only Python. Now, let's try that again. But this time, we'll type two space plus two and then hit Enter. And look, we get back the number four. That's because the expression two plus two is valid in the Python programming language. But what if we were to just type two plus and then hit Enter? Well, we get another syntax error because our expression was not complete. For example, in English, I could say you're doing a great job with the course so far. That's a grammatically correct sentence but if I were to say you're doing a great job with, that would be an incomplete sentence. I didn't finish it up. Programming languages work much the same way. They need you to follow the syntax or rules in order to understand your commands. For now, let's exit the Python prompt. We can do this by typing exit, open parentheses, close parentheses and then hitting Enter. Remember our 01_03.py file? Let's see if we can run the code contained in it. First, we'll need to open up the command prompt. To do that, we'll come down to search and we'll type Command and we'll choose Prompt. Next, we need to make sure we're in the same directory as our file. My file is located on my desktop inside of the exercise files folder. Yours might be in your downloads or another location. The first thing we're going to do is to type cd or the change directory command and then we need to find the path of our file. I'm going to open up the File Explorer and then I'll navigate to where our file is located. First, I said it's on the Desktop. I'll choose the Programming Foundations folder. Then Exercise Files, and I want the file that's inside of chapter 01. So I'm going to drag this folder over to my command prompt. Now, when we close down the File Explorer, notice that the entire file path has been copied for us. It's pretty sweet. Next, we're going to type enter. We're now inside of the chapter 01 folder. I'll then type the dir command, which stands for directory to get a list of all the files that are contained in this directory and I see my 01_03.py file. In order to execute it, I'm going to type python 01_03.py and then hit Enter and we get our Hello world! Throughout this course, I'll be using a Mac computer, so I'll often refer to Python 3 instead but for Windows users, typing strictly Python will work just fine. Although most of the programs that you write in your IDE, you can run directly from there, you may have occasions where you want to run some Python code from the command line and now you know how.

Contents