How to Run Python Program?

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Abstract

In this step by step tutorial we are going to get an in-depth view of how to run any Python Program. We will cover every possible way to run our code seamlessly depending on various factors such as our coding environment, platform and needs.

Scope

Here in this article we are going to run a Python program using the following ways:

  1. The Python Interactive mode
  2. Python Command Line
  3. Text Editor (VS Code)
  4. Python IDLE (also Python Shell)
  5. Python IDE (Pycharm)

So, let's start without any further delay.

Introduction

Do you know what is the most important step after writing any code? Yes, obviously to compile, execute and test the code! It is the only way to be sure if our code works at all, and whether we get the desired output.But since Python is an interpreted language, we can skip the compilation part and run the python script/file directly.

To run a Python Script,first let's understand what is a Python Script --

Script:

  • It is a plain text file containing Python code.
  • It has the extension .py or .pyw
  • It can be executed directly.
  • Python interpreter is responsible for executing the Python scripts.

So, what is an Interpreter? Let's see --

Interpreter:

  • It is a program which is needed to run Python Script or code.
  • It is mandatory for the Python Interpreter to be installed into one's system to run the code.
  • It can run a Python code in two ways:
    1. As a Script or Module
    2. As a piece of code written in an interactive session

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:

  1. Open command prompt / terminal in your system
  2. Enter the command - python or python3 (based on python version installed in your system)
  3. 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:

  1. Best for testing every single line of code written.
  2. In this approach, each line of code is evaluated and executed immediately as soon as we hit ↵ Enter.
  3. Great deveopment tool for experimentation of Python code on the way.

Cons:

  1. The code is gone as soon as we close the terminal(or the interactive session)
  2. Not user friendly way to write long code.

To close the interactive session we can:

  1. Type quit() or exit()
  2. 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 -

  1. Write your Python code into any text editor.
  2. Save it with the extension .py into a suitable directory
  1. Open the terminal or command prompt.
  2. 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:

  1. From extensions section, find and install 'Python' extension. Install Python Extension
  2. Restart your VS code to make sure the extension is installed properly
  3. Create a new file, type your Python code and save it. Create Python File
  4. Run it using the VS code's terminal by typing the command "py hello.py" PY Hello in Python
  5. Or, Right Click -> Run Python file in terminal Run Python File in Terminal

OUTPUT:

Python File Output

Example 1: Factorial of a number:

Python Facorial File

OUTPUT: Python Facorial File 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:

  1. Open the Python IDLE(navigate to the path where Python is installed, open IDLE(python version))
  2. The shell is the default mode of operation for Python IDLE. So, the first thing you will see is the Python Shell -- Python IDLE
  3. You can write any code here and press enter to execute Execute python IDLE code
  4. To run a Python script you can go to File -> New File Run Python Script
  5. Now, write your code and save the file -- Write Python Code

Save Python Code 6. Once saved, you can run your code from Run -> Run Module Or simply by pressing F5 -- Run Python Module

OUTPUT: Python IDLE Output

5. Run Python On IDE (PyCharm)

  1. In the Project tool window, select the project root, right-click it, and select File -> New -> Python File Python IDE
  2. Then you will see a prompt, select the option Python File from the menu, and then type the new filename. Python IDE File Name
  3. Write your code in the Python file you have just created. Python IDE Code
  4. To run this file, Right click on the file name -> Run filename Run Python IDE File
  5. PyCharm executes your code in the Run tool window. Check your output there: Output of code

Example 1: example code

OUTPUT: output of code

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 :
    1. The Python interactive mode
    2. Using the Python command line
    3. Run on Text Editor
    4. Run on IDLE
    5. Run On IDE (PyCharm)
  • We also saw detailed examples for each of them.

Happy learning!