Python is an interpreted programming language.
This means you write Python codes and send them to the python interpreter to be executed.
There are different methods of executing python programs. We can use the interactive method and the script-file method.
Interactive Method of Running Python Programs
Python programs can be executed by writing the codes and syntax directly in the Command-Line or Terminal. This is done by directly calling up the interpreter without passing a script file as a parameter.
This is used to test a small block of code in python when you need to quickly and easily test a code without writing it in a file.
To get started, open your command-line on windows or terminal on macOS and Linux, and type the following:
If that doesn't work, you can use:
And in some cases where you have more than a version of python installed such as having python 2 and python 3, you may need to specify the version. Like below:
From here on out, you can write and run any python program in the command-line/terminal. Test the sample program below:
>>> print("Hello Archangel!")
When you are done, simply type the following to quit the python command-line interface:
Script-File Method of Running Python Programs
Python programs can also be executed by creating a python file containing syntax and codes on the server, using the
.py file extension, and running it in the Command Line or Terminal.
This is used for writing complete programs and apps in python and can include having several files.
In the script-file method, the interpreter starts executing the script and continues until the end of the script. Thereafter, the interpreter becomes inactive.
For example, create a file with the name
tutorials.py and enter
Save your file. Open your command line, navigate to the directory where you saved your file, and run the script like below:
C:\Users\Your Name\folder>python tutorials.py
Note that the
python command in the code above can also be
python3 as discussed earlier.
If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.