You can convert the arguments to integers using int()

import sys

a = int(sys.argv[1])  b = int(sys.argv[2])

print a, b

print a+b

input: python mySum.py 100 200

output:

100 200
300
Answer from Wesley on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ argparse.html
argparse โ€” Parser for command-line options, arguments and subcommands
The nargs keyword argument associates a different number of command-line arguments with a single action. See also Specifying ambiguous arguments. The supported values are: N (an integer).
Discussions

python - How do I access command line arguments? - Stack Overflow
I use python to create my project settings setup, but I need help getting the command line arguments. I tried this on the terminal: $python myfile.py var1 var2 var3 In my Python file, I want to u... More on stackoverflow.com
๐ŸŒ stackoverflow.com
string - How to read integer command line arguments in Python? - Stack Overflow
I have been trying the following, as suggested by many post, but I am still getting ValueError: invalid literal for int() with base 10: 'gap_interval.py'. What am I missing? import sys if __n... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2017
python - Run script with integer arguments - Stack Overflow
This of course assumes that your are expecting exactly two arguments which can be converted to integers. If you ever want to pass an arbitrary number of integers, you can get a list of them with ... Also, if you ever plan to write a serious command line interface, consider using argparse. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Accept command line arguments as numbers - Stack Overflow
I need to accept all arguments as array. #!/usr/bin/env python import sys value = sys.argv[1:] print value ... How to do that? Thanks. ... Convert each input into an int - value = [int(x) for x in sys.argv[1:]] (or just call int(value[1]) to get the integer of a specific value. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Python
docs.python.org โ€บ 3.3 โ€บ library โ€บ argparse.html
16.4. argparse โ€” Parser for command-line options, arguments and sub-commands โ€” Python 3.3.7 documentation
ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The nargs keyword argument associates a different number of command-line arguments with a single action. The supported values are: N (an integer). N arguments from the command line will ...
๐ŸŒ
Real Python
realpython.com โ€บ python-command-line-arguments
Python Command-Line Arguments โ€“ Real Python
August 27, 2023 - To illustrate the usage of a regular expression to parse Python command-line arguments, youโ€™ll implement a Python version of seq, which is a program that prints a sequence of numbers. Following the docopt conventions, a specification for seq.py could be this: ... Print integers from <first> to <last>, in steps of <increment>. Usage: python seq.py --help python seq.py [-s SEPARATOR] <last> python seq.py [-s SEPARATOR] <first> <last> python seq.py [-s SEPARATOR] <first> <increment> <last> Mandatory arguments to long options are mandatory for short options too.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-check-if-a-command-line-argument-is-an-integer-in-Python
How to check if a command line argument is an integer in Python - Quora
int(s, base=0) lets Python detect 0x, 0o, 0b prefixes: n = int(s, 0) # accepts "0x10", "0b101", "012" (legacy octal in Py <3), or decimal ... Use try/except for int conversion; int("3.0") raises ValueError.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ command-line-arguments-in-python
Command Line Arguments in Python - GeeksforGeeks
A loop converts arguments to integers and sums them. getopt module is used when you want to handle flags and options (both short -h and long --help styles). It is helpful when your script needs command-line options similar to Linux commands.
Published ย  December 17, 2025
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_command_line_arguments.htm
Python - Command-Line Arguments
argparse โˆ’ Parser for command-line options, arguments and sub-commands. Python provides a getopt module that helps you parse command-line options and arguments.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-set-6-arguments
Python | Set 6 (Command Line and Variable Arguments) - GeeksforGeeks
July 23, 2025 - We need to fire the below command ... element in the list is the name of the file. The arguments always come in the form of a string even if we type an integer in the argument list....
Find elsewhere
๐ŸŒ
MachineLearningMastery
machinelearningmastery.com โ€บ home โ€บ blog โ€บ command line arguments for your python script
Command Line Arguments for Your Python Script - MachineLearningMastery.com
June 21, 2022 - We can further require an argument to be a specific type. For example, in the -B option above, we can make it expect integer data by adding type like the following: And if we provided the wrong type, argparse will help terminate our program with an informative error message: Empowering your Python script with command line arguments can bring it to a new level of reusability.
๐ŸŒ
PythonForBeginners
pythonforbeginners.com โ€บ home โ€บ how to use sys.argv in python?
How to Use sys.argv in Python? - PythonForBeginners.com
June 11, 2023 - In this article, we will discuss the sys.argv list in Python and its uses with an example. ... The sys.argv variable represents the argument vectors of for a program. The sys.argv list is a list that contains command line arguments in a Python program. Here, sys represents the system and argv stands for argument vector.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ command-line-option-and-argument-parsing-using-argparse-in-python
Command-Line Option and Argument Parsing using argparse in Python - GeeksforGeeks
July 12, 2025 - It defines two command-line arguments: 'integers' to accept multiple integer values, and 'accumulate' to perform a sum operation on those integers. When the script is executed with integer values ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ command-line-arguments-in-python
Command Line Arguments in Python
June 19, 2023 - In this tutorial, we're diving into Command Line Arguments in Python. Using the sys module, getopt module and argparse module, we'll parse and read arguments.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-args
Python Args | Mastering Command-Line Arguments
February 7, 2024 - The argparse module is part of the standard Python library and provides a more sophisticated mechanism to handle command-line arguments. With argparse, you can specify the type of each argument, provide help messages, and handle errors in a user-friendly way. ... import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() print(args.accumulate(args.integers)) # Output when running: python3 your_script.py 1 2 3 4 --sum # 10
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ 3 easy ways to handle command line arguments in python
3 Easy Ways to Handle Command Line Arguments in Python
January 15, 2025 - Elevate your Python skills. Explore command line arguments with real-world examples. Join our Free Python Course for hands-on learning! If we have a python script which generates 5 Random numbers between two numbers 1 to 100, which accepts these 3 integers as Command Line Arguments.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ python tutorial โ€บ python command-line arguments
Python Command-line Arguments | Options in Command-line Argument
May 3, 2023 - Then for loop is being used to traverse the argv list to calculate the sum of command-line arguments. Since arguments are of string type they are the first typecast to integer type before sum operation. The result is then printed as shown in the screenshot above. The second approach in python to implement command-line arguments is by using the getopt module.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
GitHub
hplgit.github.io โ€บ primer.html โ€บ doc โ€บ pub โ€บ input โ€บ ._input-readable002.html
Reading from the command line
A command-line argument is treated as a text, so sys.argv[1] refers to a string object, in this case '21'. Since we interpret the command-line argument as a number and want to compute with it, it is necessary to explicitly convert the string to a float object.