How to Run Python Programs in Windows: In the last post, we learned to install Python interpreter on our computer. In this tutorial we will learn to create, edit and run our first Python program.
In this tutorial we will run (execute) many Python commands and programs but you do not understand the syntax and logic of those programs because you will learn in the next tutorials.
In this tutorial you just have to understand and learn how to run Python commands, programs (scripts).
In Python interpreter you get 2 options to run Python programs. First Python Shell and second Python IDLE .
We will first talk about Python Shell and learn to use it and then we will talk about Python IDLE and learn to use it.
You can also read: How To Install Python Programming Language
Table of Contents:
What is Python Shell and how to use it?
Python Shell is a command line tool which we also call Python Interactive Shell. We mostly use Python Interactive Shell when we want to execute a single Python command.
As soon as we type our Python command and press the enter key, the Python interpreter executes our commands and shows the output and then waits for the next command.
To open Python Interactive Shell in Windows, search the Python folder in the start menu and click on the Python option in it as you can see in the image below.
Apart from this, if you open Command Prompt (CMD) in your windows computer and type Python in it, then Python Shell will open in CMD itself.
When Python Interactive Shell is open then you will get >>> show which means Python prompt is ready and you can type your Python commands.
I have entered some Python commands in the Python shell in the example below and have also shown its output.
>>> 25+5 30 >>> print("Hello World") Hello World >>> name = "Karan" >>> print("Hello "+name) Hello Karan
The type after Python prompt (>>>) are Python commands and the lines you see without Python prompt (>>>) are the output of those Python commands.
To close Python Interactive Shell, you can either press the enter key by pressing Ctrl+Z or by typing the quit() or exit() built-in function.
I personally rarely use Python Shell because Python Shell is only good for testing small code.
Apart from this, the biggest problem when running Python programs or commands on the Python shell is that as soon as you close the Python shell, your Python programs also get lost because Python shell does not save your programs anywhere.
So if you want to save and use your Python programs (scripts) again, then for that you have to use Python interpreter in script mode instead of interactive shell mode.
For what is Python script mode and how to use it, we will now talk about Python IDLE and learn how to use it.
What is Python IDLE and how to use it?
As I told you above that in Python interpreter you get 2 options to run Python programs. First Python Shell and second Python IDLE.
The full form of IDLE is Integrated Development and Learning Environment.
In Python IDLE, you get very useful features like professional IDE’s like Python syntax highlighting, auto-indentation etc.
In IDLE you get Python Shell mode as well as Python Script mode so that you can save, edit and run Python programs in text files.
Just like we had opened the Python shell by going to the start menu above, you can open IDLE in the same way or you can also open it by typing IDLE in windows search.
When you open Python IDLE, then you get Shell mode default open in it and here also you can run Python programs and commands in the same way as we did above.
Now if you want to work in IDLE’s script mode, for that you have to open New File by clicking on the file menu.
As soon as you click on the New file option, a file will open in front of you, where you can create any Python program.
I have just written a statement in my program and then saved it in my computer with the name first.py .
You can save your Python programs, which we also call Python scripts, with any name anywhere on your computer, but forgetting to put the .py extension after the file name.
To run your saved program, press the F5 key from the keyboard or open the run menu and click on the run module option.
As soon as your Python program is run, you will be shown its output on the Python shell, as you can see in the image below.
To make changes to the Python program, save it back in script mode (text editor) by making changes to the program file and then press F5 again to see the output.
How to Run Python Programs in CMD Command Line
After installing Python interpreter, you can use Python shell mode and script mode from Command Prompt (CMD) of your windows computer i.e. from command line as well.
I have told you above to open, close and use Python shell mode in Command Prompt. Now here I will tell you how to run Python programs files in CMD.
First of all, you create your program in any text editor such as Notepad, Notepad++, Sublime etc. and then save it in any folder on your computer.
Like I have saved my Python program drive (C:\MyPython) in the MyPython folder of C.
While saving Python programs, just keep in mind that you must put the .py extension after the file name.
Now to run your Python programs from the command line, you have to open CMD and for this you first open the folder where your Python programs are saved.
After this, enter by typing CMD in the address bar of the folder and this will open the CMD command line with the same location path.
Now to run your Python program, you must first type python on the command line and then type Python program file name with .py extension.
As you can see above that as soon as we run our program, immediately below it you are showing its output.
I hope that you have liked this post of mine. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at firstname.lastname@example.org.
1 thought on “How to Run Python Programs in Windows?”
Itís hard to find well-informed people for this subject, but you seem like you know what youíre talking about! Thanks