Execute Functions with Multiple Arguments at a Terminal



In Python, you can execute functions with multiple arguments directly from the terminal using different approaches depending on how your function is defined and how you want to provide the arguments.

Executing Python functions from the terminal allows you to quickly test or run code without writing a full script. When a function requires multiple arguments, you can pass them manually, use input prompts, or use command-line arguments with the help of modules like sys or argparse.

Using the Python Interactive Shell

If you are working in the Python interactive shell, you can define and call functions directly by entering commands one at a time.

Example

Here is a simple function that adds two numbers. You can define and call it in the terminal like this -

# Define and call the function
>>> def add(a, b):
...             return a + b
...
>>> result = add(4, 7)
>>> print(result)

The output will be -

11

The above code needs to be executed in your python terminal, not inside a Python script or interpreter.

Running a Script with sys.argv

If your function is defined in a script file, you can use the sys module to capture command-line arguments and pass them to the function.

Example

Suppose you have a file named sum.py with the following content -

import sys

def add(a, b):
   return a + b

# Read arguments from terminal
x = int(sys.argv[1])
y = int(sys.argv[2])

print("Sum:", add(x, y))

To run this script, open the command prompt and provide the arguments like this -

C:\Users\Tutorialspoint\Desktop>sum.py 5 10

The output will be -

Sum: 15

Using argparse for Better Argument Handling

The argparse module provides a better way to handle multiple arguments from the terminal and it is suitable for scripts with multiple options.

Example

Create a script named multiply.py with the following code -

import argparse

def multiply(a, b):
   return a * b

parser = argparse.ArgumentParser()
parser.add_argument("a", type=int, help="First number")
parser.add_argument("b", type=int, help="Second number")

args = parser.parse_args()

result = multiply(args.a, args.b)
print("Product:", result)

Now run this from the command prompt with the following arguments -

C:\Users\Tutorialspoint\Desktop>python multiply.py 3 4

You will get the output as shown below -

Product: 12

Using Input Function in Terminal

You can also use the input() function inside a script to take multiple values from the user when the script is run.

Example

Here is an example in a file named greet.py -

def greet(name, city):
   print(f"Hello {name} from {city}!")

name = input("Enter your name: ")
city = input("Enter your city: ")

greet(name, city)

Run the script and enter values when prompted:

C:\Users\Tutorialspoint\Desktop>greet.py
Enter your name: Ravi
Enter your city: Mumbai

You get the output as shown below -

Hello Ravi from Mumbai!

When to Use Each Method

You can use the following methods depending on your use case ?

  • Interactive Shell: For quick testing and exploration.
  • sys.argv: For simple command-line argument parsing in small scripts.
  • argparse: For scripts that need multiple, named, or optional arguments.
  • input(): When you want to prompt users for values interactively.
Updated on: 2025-05-07T13:05:12+05:30

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements