- argv is not Array it is a List. It is available sys Module.
- The Argument which are passing at the time of execution are called Command Line Arguments.
Within the Python Program this Command Line Arguments are available in argv. Which is present in SYS Module.
Program: To check type of argv from sys
import argv
print(type(argv))
D:\Python_classes\py test.py
Write a Program to display Command Line Arguments
1) from sys import argv 2) print(“The Number of Command Line Arguments:”, len(argv)) 3) print(“The List of Command Line Arguments:”, argv) 4) print(“Command Line Arguments one by one:”) 5) for x in argv: 6) print(x) 7) 8) D:\Python_classes>py test.py 10 20 30 9) The Number of Command Line Arguments: 4 10) The List of Command Line Arguments: [‘test.py’, ‘10’,’20’,’30’] 11) Command Line Arguments one by one: 12) test.py 13) 10 14) 20 15) 30
1) from sys import argv 2) sum=0 3) args=argv[1:] 4) for x in args : 5) n=int(x) 6) sum=sum+n 7) print("The Sum:",sum) 8) 9) D:\Python_classes>py test.py 10 20 30 40 10) The Sum: 100
Eg:
1) from sys import argv 2) print(argv[1]) 3) 4) D:\Python_classes>py test.py Sunny Leone 5) Sunny 6) 7) D:\Python_classes>py test.py 'Sunny Leone' 8) 'Sunny 9) 10) D:\Python_classes>py test.py "Sunny Leone" 11) Sunny Leone
Eg:
1) from sys import argv 2) print(argv[1]+argv[2]) 3) print(int(argv[1])+int(argv[2])) 4) 5) D:\Python_classes>py test.py 10 20 6) 1020 7) 30
Eg:
1) from sys import argv 2) print(argv[100]) 3) 4) D:\Python_classes>py test.py 10 20 5) IndexError: list index out of range
output statements:
We can use print() function to display output.
Form-1: print() without any argument
Just it prints new line character
Form-2:
1) print(String): 2) print("Hello World") 3) We can use escape characters also 4) print("Hello \n World") 5) print("Hello\tWorld") 6) We can use repetetion operator (*) in the string 7) print(10*"Hello") 8) print("Hello"*10) 9) We can use + operator also 10) print("Hello"+"World")
Form-3: print() with variable number of arguments:
1. a,b,c=10,20,30 2. print("The Values are :",a,b,c) 3. 4. OutputThe Values are : 10 20 30
By default output values are seperated by space.If we want we can specify seperator by using "sep" attribute
1. a,b,c=10,20,30 2. print(a,b,c,sep=',') 3. print(a,b,c,sep=':') 4. 5. D:\Python_classes>py test.py 6. 10,20,30 7. 10:20:30
Form-4:print() with end attribute:
1. print("Hello") 2. print("Durga") 3. print("Soft")
Output:
1. Hello 2. Durga 3. Soft
If we want output in the same line with space
1. print("Hello",end=' ') 2. print("Durga",end=' ') 3. print("Soft")
Output: Hello Durga Soft
Form-7: print(formatted string):
%i => int %d => int %f => float %s => String type
"Python Argparse"
"Python Command Line Arguments
Parser"
"Python Command Line Arguments
List"
"Python Command Line Arguments
Example"
"Python Command Line Arguments
Library"
"Python Command Line Options"