A Python script is a file containing Python code saved with the .py extension. It can be executed using the Python interpreter through the command line, interactive mode, text editors, or IDEs.
Example: Run a Python Script
Input: print("Hello World!")
Output: Hello World!
Explanation: The print() function displays the message when the Python script is executed.
Ways to Run a Python Script
Python scripts can be executed using the following methods:
- Interactive Mode
- Command Line
- VS Code
- PyCharm
Using Interactive Mode
Python Interactive Mode allows you to execute Python statements directly in the interpreter. Open Command Prompt or Terminal, type python, and press Enter.
Example 1: Using Print Function
Run the following line in the interactive mode:
print('Hello World !')
Output:

Explanation:
- python starts the Python interpreter.
- The print() statement is executed immediately.
- The result is displayed in the terminal.
Example 2: Using Interactive Execution
Run the following lines one by one in the interactive mode.
name = "Aakash"
print("My name is " + name)
Output:

Explanation:
- The if statement checks whether a is greater than b.
- Since 1 is not greater than 3, the else block executes.
- The result is displayed using print().
Example 3: Interactive Mode Comparison
Run the following lines one by one in the interactive mode.
a = 1
b = 3
if a > b:
print("a is Greater")
else:
print("b is Greater")
Output:

Explanation:
- The python command starts the Python interpreter.
- hello.py specifies the Python script to execute.
- The script output is displayed in the terminal.
Note: To exit from this mode, press ‘Ctrl+Z’ and then press ‘Enter’ or type ‘exit()’ and then press Enter.
2. Run Python Script by the Command Line
Running Python scripts on Windows via the command line provides a direct and efficient way to execute code. It allows for easy navigation to the script's directory and initiation, facilitating quick testing and automation.
Example 1: Using Script Filename
Save the Python code in a file named hello.py:
python hello.pyOutput:

Explanation:
- The python command starts the Python interpreter.
- hello.py specifies the Python script to execute.
- The script output is displayed in the terminal.
Example 2: Redirecting output
The > operator can be used to save the output of a Python script to a file.
Output :

Explanation:
- The > operator redirects the script output.
- The output is written to output.txt.
3. Run a Script in Python using a Text Editor
A Python script can be created and executed using a text editor such as VS Code. Follow these steps:
- Go to the Extensions section or press Ctrl+Shift+X on Windows.
- Search for and install the Python extension.
- Create a new file named hello.py.
- Add the following code:
print('Hello World!')
- Then, right-click anywhere in the text area and select the option that says ‘Run Code’ or press ‘Ctrl+Alt+N’ to run the code.
Output:

Explanation:
- The Python extension provides Python support in VS Code.
- The Run Python File option executes the selected .py file.
- The output is displayed in the terminal.
4. Run Python Scripts using an IDE
A Python script can also be executed using an IDE (Integrated Development Environment) such as PyCharm. Follow these steps:
- Create a new project.
- Give the project a name such as GfG and click Create.
- Right-click the project directory, select New, and choose Python File.
- Enter hello as the file name. PyCharm automatically adds the .py extension.


Output:

Explanation:
- PyCharm uses the Python interpreter configured for the project.
- The Run option executes the selected Python file.
- The output is displayed in the PyCharm console.
Note: You don’t have to specify the extension as it will take it automatically.