How to Execute python code

There are 3 main ways to execute a python script.

1) Interactive Prompt

2) IDE

3) Command Line

h3>Interactive Prompt> A:link { COLOR: DodgerBlue /*The color of the link*/ } A:visited { COLOR: Lime /*The color of the visited link*/ } A:hover { COLOR: Snow /*The color of the mouseover or 'hover' link*/ } body{ color: white; background-image:url(image/dark_spots.jpg); background-attachment:fixed; } /h3>

This is mostly used for experimenting and testing. Although you can write out long codes here, you should switch over to a file if you plan to write more than 3-5 lines. An IDE or file would be more appropriate and easier. The >>> is the prompt for interactive mode. What you type here is python code. It will be executed right after you hit Enter. The only time it does not execute after Enter is when you are defining a function or class or a loop, for example. The ... will indicate that you are inside the block of what you are defining. Once you finish writing the code inside the block, you must hit Enter twice, where it will execute and take you back to the >>> prompt depending on what you are defining.

The reason the interactive prompt is for testing is because:

1) it will execute upon each line or after the definition of a loop, class, function, etc.

2) Once you exit the interactive prompt, what code you wrote in it, is gone. There is no saving the code.

The other two methods of executing python code will save a file with your code in it.


Click on Start Menu -> (on search) type: "command prompt". A Black icon "cmd" or Command prompt" will show up. Or open c:\Windows\system32\cmd.exe which will do the same. This is a DOS prompt. You can do everything you can do normally on the PC in this DOS prompt with commands. The text before your cursor is the directory you are currently in. The command "cd" will change your current directory to the string after. The command 'dir' will list the directories in that directory that you are in. PythonXX should be in the directory C:\PythonXX (where XX represent the version you have downloaded). Once in the directory: type "python" to execute the interactive prompt.

If you see ">>>", then you are in in the python interactive prompt.


Open a Terminal. It does not matter what directory you are in. If you have both python2.x and python3.x installed, you can use either interactive prompt by a number. "python" is the default for your OS. The default may be python3.x in some distros (currently now is gentoo and arch)




There are numerous IDE's. The only difference is preference. Some are free and some are not, but give out a free version that will work.

Each IDE is different in how you configure it, execute code, etc. Some have a run command, and F# key, etc. to execute the python code. Some will open a terminal/console with for the output, some will have embedded terminals, and some will have their own area for output. Some will have all of these options, some will have only a few of these options.

What is IDLE? IDLE is just only one of the numerous IDE's. Also by most programmers, it is also considered one of the worst IDE's too. It shall be fine for beginner's though. You can research why with a google search later, or run across the reasons yourself as you progess in programming. For those that stick with IDLE, IDLE starts you off with the python interpreter. This is not a DOS prompt! It is just a GUI form of the python interpreter that you can also get in the DOS prompt/terminal. It confuses a lot of beginner's because they think they click new window, write their code, and run their code in the python interpreter, thinking it is a DOS prompt or terminal.

Command Line / Terminal

More often than you think, people will not use an IDE. In this case you open any text editor you want, write code, and when you save it you save it with the file extension of ".py". An IDE is just a fancy text editor, that's it! You can use anything, Gedit, notepad, IDLE, whatever to write the code. All IDE's have a way of quick run, but you can also run them from the terminal/DOS as shown below.

For Windows simplicity: put this test.py file in your directory that you started the interactive prompt: for example of the path:

and example of the same path executed:

Then you open a Terminal/DOS prompt and change directory to your test.py and write this:

The same as before like with the interactive prompt, but this time we add a argument and the argument is your .py file.

Does your program when executed just flicker and then go away? Most Windows users have got accustomed to double clicking an icon and have it execute. To adjust the program for double clicking, the basic method is to add the builtin input to have it ask the user for "nothing" to assure that the user must hit ENTER before the program closes out. raw_input() for Python 2.x and input() for Python 3.x. Just put this after your program at the end of the file and it will restrict the program to prematurely exiting. This is however only effective if you plan to execute the program by double clicking it. If you use a command line/Terminal to execute the program, you do not need the input function as the command line will still allow you to view the output of your program even after exiting, as you just get the users prompt when it exits.


This example shows the creation and execution of a one line python script. 'touch tester.py' creates the file, although vim will create it anyways. 'vim tester.py' opens one of the numerous text editors. Remember, you can use anything. 'cat tester.py' shows the content of the .py file that i wrote in it, in vim. 'python3 tester.py' executes the script I made with python3.x. 'this is a test' is the output of that program executed.