Table of contents
1.
Introduction
2.
What is a Terminal
3.
How to open Terminal
4.
How to run Python script using the terminal
5.
Running Python in Interactive Mode
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Using Python through Shell & Terminal

Author APURV RATHORE
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In today’s generation, programming has become an integral part of the world. We interact with things that are programmed on a daily basis. Everything is programmed, whether it is a computer, mobile, washing machine, AC, or even television. 
Hence, learning the principles of programming can set you apart from your peers, giving you a competitive advantage in today's technology-driven environment.

Hence if you plan to learn Python, you must also know how to execute the Python scripts that you write. It is very important for a programmer to know different ways of running a code depending on the requirements. 
Knowing this will take your Pythonic skills to a whole new level. 
Also see, Merge Sort Python, and  Convert String to List Python
Also check,   reverse a string in python

What is a Terminal

Graphical user interfaces (GUIs) are helpful for various jobs since they allow you to complete daily tasks by interacting with windows and icons. For many activities, though, we are better suited to directly entering text commands into the computer for greater efficiency and flexibility. This is usually done through terminals.


Terminals, usually called command lines or consoles, allow us to complete and automate activities on a computer without using a graphical user interface. Using a terminal enables us to send simple text commands to our computer, such as navigating through a directory or copying a file. It serves as the foundation for many more advanced automation and programming skills.

Also see, Swapcase in Python

How to open Terminal

To launch a terminal, follow these steps.

  1. 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.
     
  2. 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]. 
     
  3. 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

  1. 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.
     
  2. 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.
     
  3. 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!

 

Live masterclass