In this tutorial, we will learn how to run a Python program on our local computer. We have two different ways to run a Python program. First, we can install any Python IDE, so we don’t need to explicitly install Python. The second is to install Python on our local computer itself. Let’s discuss each method one by one.
1. Installing any Python IDE
Using a Python IDE(Integrated Development Environment) is the easiest way to run a Python program. There are a lot of free Python IDE available. In this tutorial, we will use Thonny IDE, which is basically dedicated to beginners. This IDE comes up with the latest version of Python bundled in it, so you don’t need to install Python separately.
Follow the below steps:
- Download Thonny from its official website.
- Once it’s downloaded, right-click on the executable file and click on Run as administrator.
- Move forward by selecting each step and at last click on the Install button.
- Once it’s installed, launch the application.
- Go to File> New File and then save this file as .py extension. You can name it whatever you want but don’t forget to add .py at the last.
- Now you are ready to write your first program.
7. After writing your program, click on the Run icon(play button) or press F5 and you can see the output in the bottom Python shell.
2. Installing Python on Our Local Computer Separately
In the first method, we have discussed how we can run a Python program without explicitly installing Python. If you are not willing to use any separate IDE, you can install Python and run your Python program. Here is how:
- Download Python from its official website.
- Launch the installer, move forward step by step and install it.
- Once it’s installed completely, add PATH to Python, so that you can run Python commands from anywhere. You can set the PATH by adding Python to the environment variables.
- Now open the command prompt and type python –version. If you see the Python version after hitting the python –version command, everything is fine now. If not, you need to set the PATH properly. Refer below image.
5. Now as everything looks good. you can type the command Python in your command prompt. Once you hit the Python command it will immediately run in interpreter mode and you can run any Python code here. See the below example:
Note: Use command exit() to come out of the Python interpreter mode.
Now that we have seen how we can run our program using the command prompt. But wait, wouldn’t it be very difficult to write multiple lines of program in command prompt? To avoid such a difficulty, we can use any text editor to write our programs and we can use command prompt to run it.
Follow below steps:
- Write your program in any text editor you want.
- Save the file with .py extension.
- Open the command prompt and move to the saved file path.
- Run the command python file_name.py on that path.
- After hitting above mentioned command you can see the output.
First Python Program
Now that we have learned how to run a Python program. It’s time to create our first Python program. Let’s create a very simple hello world program. Actually, the hello world program is used to introduce a new programming language to beginners. Let’s get started.
Type the below code in any text editor or IDE you are using and save it as hello_world.py
Run the file.