How to Run Python Program?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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 -

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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!

Additional Resources

  1. Applications for Python
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more