Command line arguments are those values that are passed during calling of program along with the calling statement. Thus, the first element of the array
sys.argv() is the name of the program itself.
sys.argv() is an array for command line arguments in Python. To employ this module named “
sys” is used.
sys.argv is similar to an array and the values are also retrieved like Python array.
The sys module
The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
The above program has been saved by the name “com.py” and thus has to be called in the following in command prompt
Functions that can be used with sys.argv
- len()- function is used to count the number of arguments passed to the command line. Since the iteration starts with 0, it also counts the name of the program as one argument. If one just wants to deal with other inputs they can use (len(sys.argv)-1).
- str()- this function is used to present the array as a string array. Makes displaying the command line array easier and better.
The following program performs addition using inputs given during runtime:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Use of nonlocal vs use of global keyword in Python
- When to use yield instead of return in Python?
- Python | Use of __slots__
- How to use a List as a key of a Dictionary in Python 3?
- Use Pandas to Calculate Statistics in Python
- Use of min() and max() in Python
- Python | How to use Multiple kv files in kivy
- How to use multiple UX Widgets in kivy | Python
- Use of slice() in Python
- How to use Glob() function to find files recursively in Python?
- Python program to find GSoC organisations that use a Particular Programming Language
- How to use close() and quit() method in Selenium Python ?
- Python theHarvester - How to use it?
- Use get() method to create a dictionary in Python from a list of elements
- Use of na_values parameter in read_csv() function of Pandas in Python
- How to use Color Palettes in Python-Bokeh?
- How to use NamedTuple and Dataclass in Python?
- How to use datasets.fetch_mldata() in sklearn - Python?
- How to use string formatters in Python ?
- Functors and their use in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.