Use Python in Script Mode



Python is a high-level programming language that helps users to run a program and integrate systems more efficiently, as it executes a program in comparatively fewer lines of code. In Python programming language, a code can be executed in two ways -

  • Interactive Mode
  • Script Mode

In this article, we will learn how to execute a Python program in script mode on Windows. However, the steps are almost the same for other operating systems.

Python Script Mode

Script mode in Python allows us to write Python code in files with the .py extension and run the entire program at once. This mode is suitable for writing larger programs and saving them for future use.

When compared to interactive mode, where commands are executed line by line and script mode enables us to execute multiple lines of code stored in a Python file. This approach is more efficient and maintainable for developing full-fledged applications.

Note: To run Python in script mode, we typically use a text editor or an IDE like VS Code, PyCharm, etc., to write the script and a terminal or command prompt to execute it.

Steps to Run Python in Script Mode

The following are the steps that we need to follow to use Python in Script mode -

  • Open any text editor or IDE and write the Python code.
  • Save the file with a .py extension, for example, sample.py.
  • Open the terminal or command prompt in the directory where the file is saved.
  • Type the command python filename.py and press Enter.

After following the above steps, the output of the script will be displayed in the terminal.

Example

Let us consider an example, in which the sample.py script contains the following Python code -

print("Welcome to Tutorialspoint")
name = input("Enter your name: ")
print("Hello, " + name)

Now, open the terminal or command prompt and navigate to the location where the Python file is located. Then run the script using the following command -

python sample.py

Following is the output after executing the sample.py file -

Welcome to Tutorialspoint
Enter your name: Riyaansh
Hello, Riyaansh

Note: Script mode is the default mode, which is used when running Python files through an IDE or terminal. It's the most common method used by developers in real-world applications.

Updated on: 2025-04-24T15:44:55+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements