How to open Terminal
To launch a terminal, follow these steps.
-
Windows
There are many different ways to open the command prompt in windows. One of the ways is to press the Windows key, then type cmd and click enter.
-
Linux
In Linux, the command line can be accessed by various apps, including xterm, Gnome Terminal, and console. Gnome terminal can be accessed by clicking [ctrl+alt+T].
-
Mac
In the Dock, click the Launchpad icon, type Terminal into the search field, and then click Terminal.
How to run Python script using the terminal
First of all, open launch a terminal by following the above instructions. Then navigate to the folder where the python script is stored on your pc. Then type python followed by the name of your file.
Suppose the name of your python file is main.py and the contents of the file are
print("Hello from the other side")

You can also try this code with Online Python Compiler
Run Code
To execute the python file, type
D:\Course\Codeforces\New Codeforces>python main.py
Hello from the other side

You can also try this code with Online Python Compiler
Run Code
You will see output similar to above.
The real potential of using the command line to run scripts comes when you can call the same script with multiple arguments. This enables you to experiment with alternative parameters or evaluate a different data set without modifying the underlying code.
To obtain parameters, import the sys module. The desired values are typed after the script name to supply arguments to it. If multiple arguments are provided, they must be space-separated. The following line of code, for example, would be used to pass two numbers 5 and 6 as an argument to main.py.
python main.py 5 6

You can also try this code with Online Python Compiler
Run Code
Let’s create a python script that takes two arguments as input and prints their sum.
To access the arguments, use sys.argv, which returns all the arguments in the form of a list in string form.
Consider the following python script main_args.py.
import sys
# sys.argv is a list of strings
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
print("The sum of the numbers is :",num1+num2)

You can also try this code with Online Python Compiler
Run Code
Below is the output when we execute the python file with arguments 1 and 2.
D:\Course\Codeforces\New Codeforces>python main.py 1 2
The sum of the numbers is : 3

You can also try this code with Online Python Compiler
Run Code
You can try it on online python compiler.
Running Python in Interactive Mode
Normal and interactive are the two basic modes of Python. Interactive mode is a command-line shell that provides instant response for each statement while simultaneously running previously provided statements in active memory.
In simpler words, when we input a command and press enter in interactive mode, the output is displayed immediately. The last command we give impacts the output of the code in interactive mode. For writing very small lines of code, the interactive mode is incredibly useful.
To use the interactive mode, you first need to open a terminal.
Then type python and press enter.
If you see >>>, then you have successfully entered into Python interactive mode.
D:\Course\Codeforces\New Codeforces>python
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can also try this code with Online Python Compiler
Run Code
Consider the below example, in which we add two numbers using interactive mode.
D:\Course\Codeforces\New Codeforces>python
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 3
>>> b = 5
>>> c = a*b
>>> c
15

You can also try this code with Online Python Compiler
Run Code
Consider another example, which takes a number as an argument and then prints its square.
C:\Users\apurv>python
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> n = int(input())
9
>>> print("The square of the number is ",n**2)
The square of the number is 81
>>>

You can also try this code with Online Python Compiler
Run Code
You can also read about the Multilevel Inheritance in Python.
FAQs
-
When is the interactive mode in python not preferred?
Large programs should not be executed in interactive mode. The interactive mode does not save the statements. Once we create a program, it is only valid for that period of time; it cannot be used again in the future.
-
What is a shell?
The command-line interpreter is referred to as a shell. A shell is a program that executes commands and displays the output. It is a layer that rests on top of the kernel. The user's commands are interpreted and processed by it.
-
Why is python called a scripting language?
The term "scripting language" refers to a language that is interpreted. Python is a scripting language. Python's code is translated and executed using an interpreter. Python is, therefore, a scripting language.
Key Takeaways
Congratulations on making it this far.
We got introduced to executing Python scripts through the terminal in this blog. We also learned how to run python in interactive mode.
Check out this article - Quicksort Python
If you want to become proficient with Python programming, I suggest you take the Coding Ninjas Python Course, which will teach Python basics with Data Structures and Algorithms.
Happy Learning!