How to Run Python Program?
This tutorial provides a comprehensive guide on running Python programs, focusing on various environments, platforms, and needs. Key steps after coding are executing and testing, essential for validating code functionality. Python, an interpreted language, doesn't require compilation, allowing direct script execution.
A Python script, a plain text file with .py or .pyw extension, is run by the Python interpreter. The interpreter, necessary for running Python code, operates in two modes: executing scripts/modules or running code in interactive sessions.
Different Ways to Run Python Program
There are various ways to run a Python Program, let us see them -
1. The Python Interactive Mode
This is the most frequently used way to run a Python code. Here basically we do the following:
- Open command prompt / terminal in your system
- Enter the command - python or python3 (based on python version installed in your system)
- If we see these : >>> then, we are into our python prompt. It will look something like this:
Now we can start writing our code here.
Example 1:
Example 2:
Example 3:
Pros:
- Best for testing every single line of code written.
- In this approach, each line of code is evaluated and executed immediately as soon as we hit ↵ Enter.
- Great deveopment tool for experimentation of Python code on the way.
Cons:
- The code is gone as soon as we close the terminal(or the interactive session)
- Not user friendly way to write long code.
To close the interactive session we can:
- Type quit() or exit()
- Press ctrl + z and hit ↵ Enter for windows. For linux, we can press ctrl+d.
2. Using the Python Command Line
Follow the below steps to run a Python Code through command line -
- Write your Python code into any text editor.
- Save it with the extension .py into a suitable directory
- Open the terminal or command prompt.
- Type python/python3 (based on installation) followed by the file name.py --
Example 1: Saved with hello.py
OUTPUT:
Example 2: Saved with factorial.py
OUTPUT:
Note: If it doesn't runs, then re-check your Python installation path & the place you saved your Python file.
3. Run Python on Text Editor
If you want to run your Python code into a text editor -- suppose VS Code, then follow the below steps:
- From extensions section, find and install 'Python' extension.
- Restart your VS code to make sure the extension is installed properly
- Create a new file, type your Python code and save it.
- Run it using the VS code's terminal by typing the command "py hello.py"
- Or, Right Click -> Run Python file in terminal
OUTPUT:
Example 1: Factorial of a number:
OUTPUT:
4. Run Python on IDLE
Python installation comes with an Integrated Development and Learning Environment, which is popularly known as IDLE. Though Python IDLE are by-default included with Python installations on Windows and Mac, if you’re a Linux user, then you can easily download Python IDLE using your package manager.
Steps to run a python program on IDLE:
- Open the Python IDLE(navigate to the path where Python is installed, open IDLE(python version))
- The shell is the default mode of operation for Python IDLE. So, the first thing you will see is the Python Shell --
- You can write any code here and press enter to execute
- To run a Python script you can go to File -> New File
- Now, write your code and save the file --
6. Once saved, you can run your code from Run -> Run Module Or simply by pressing F5 --
OUTPUT:
5. Run Python On IDE (PyCharm)
- In the Project tool window, select the project root, right-click it, and select File -> New -> Python File
- Then you will see a prompt, select the option Python File from the menu, and then type the new filename.
- Write your code in the Python file you have just created.
- To run this file, Right click on the file name -> Run filename
- PyCharm executes your code in the Run tool window. Check your output there:
Example 1:
OUTPUT:
Conclusion
Congratulations on running your first script in Python! Let's get an overview of what you learn throughout this article :
- Script & Interpreter
- Different ways to run Python Script through :
- The Python interactive mode
- Using the Python command line
- Run on Text Editor
- Run on IDLE
- Run On IDE (PyCharm)
- We also saw detailed examples for each of them.
Happy learning!