sys.argv Python
To pass arguments to a Python script from the command line you need to use the library sys
import sys
Then every argument has to be received with the help of sys.argv
a = sys.argv[1]
Example: the script add.py
import sys
first = sys.argv[1]
second = sys.argv[2]
print(f"{first} + {second} = {first + second}")
python3 add.py top bicycle
top + bicycle = topbicycle
To do the same with integers - we use the int function
import sys
first = int(sys.argv[1])
second = int(sys.argv[2])
print(f"{first} + {second} = {first + second}")
python3 add.py 3 4
3 + 4 = 7
In other languages
In
C
a similar function is performed by
int argc, char* argv[]
In
Bash
it is
built in by default
.
Article author: Andrei Olegovich