After installing and setting up the environment variable, the next thing is to learn about how to run Python scripts. There are different ways of executing a python script and each has its own advantages. In this tutorial, we will guide you through different ways in which you can run a python script on your computer. First, let's learn about Python Interpreter.
Python is an interpreted language just like PHP. As we have told you earlier, Python needs to be interpreted, which means,
it will get executed line by line.
A computer doesn't understand Python language, it just runs on machine language and system calls. Hence, a python interpreter transforms the python code line by line into Machine Language, so that the computer understands it and performs the given instruction.
In simple terms, Python interpreter is a software, that is used for communication between the Python script and the computer to seamlessly run the program.
There are different Python Interpreter for different implementation of Python:
An interactive mode is nothing but executing python commands directly by using the Command prompt. It is a fast way to see the result of any Python code. It is useful when the amount is code is small, around 1-2 lines only.
Just open the command Prompt and type
python and hit enter, Now you have entered in the Python terminal and can execute small Python commands.
Let's print something, we will use python's
print() function to print "Hello Python!" on the terminal. This print method is used to print something on the screen. Look at the image below, after we hit enter,
"Hello Python!" gets printed on the terminal.
To exit this Python Terminal, just enter
exit() and press enter.
The script mode means creating a script of Python and then executing it. The terminal mode is useful for single statement execution but if we want to execute a large amount of code then we would have to save the code first and then execute it.
The Scripts are created with
.py extension. These scripts contain multiple lines of Python code. You can create these scripts in any editor like notepad but always save it with
Let's save the command
print("Hello Python!") in a file and name it Hello.py.
Now to execute, we simply have to open the folder in which the file resides and then has to type
python Hello.py and press enter.
It will print
Hello Python! as the output of the script. You can enter multiple statements and execute them all at once using this approach.
IDE is Integrated Development Environment software that provides
a GUI based execution environment for Python Scripts. In an IDE, you can use both Interactive and Script mode with a lot of other functionalities.
Some popular Python IDE's are IDLE, Visual Studio Code, PyCharm, etc. Install these IDE's and start creating Python programs.