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.
Eg: D:\Python_classes py test.py 10(command) 20(line) 30(arguments)
Within the python program this command line arguments are available in argv. Which is present in SYS module.
Test.py
|
10
|
20
|
30
|
Note:
argv[0] represents Name of program. But not first command line argument.
argv[1] represents first command line argument
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
from sys import argv
print(“The Number of Command Line Arguments:”, len(argv))
print(“The List of Command Line Arguments:”,argv)
print(“Command Line Arguments one by one:”)
for x in argv
print(x)
D:\Python_classes>py test.py 10 20 30
The Number of Command Line Arguments: 4
The List of Command Line Arguments: [’test.py’, ’10’,’20’,’30’]
Command Line Arguments one by one
test.py
10
20
30
from sys import argv
sum=0
args=argv[1:]
for x in args :
n=int(x)
sum=sum+n
print(”The Sum:”,sum)
D:\python_classes>py test.py 10 20 30 40
The Sum: 100
Note1: Usually space is separator b/w command line arguments. If our command line arguments. If our command line argument itself contains space then we should enclose within double quotes(but not single quotes )
from sys import argv
print(argv[1])
D:\Python_classes>py test.py Karthik Bear
Karthik
D:\Python_classes>py test.py 'Karthik Bear’
‘Karthik
D:\Python_classes>py test.py "Karthik Bear"
Karthik Bear
Note2: within the python program command line arguments are available in the string form. Based on our requirement, we can convert into corresponding type by using type casting methods.
from sys import argv
print(argv[1]+argv[2])
print(int(argv[1])+int(argv[2]))
D:\Python_classes>py test.py 10 20
1020
30
Note3: If we are trying to access command line arguments with out of range index then we will get error.
- from sys import argv
- print(argv[100])
D:\Python_classes>py test.py 10 20
IndexError: list index out of range.
Note : In python there is argparse module to parse command line arguments and display some help messages whenever end user enters wrong inputs.
Input
raw_input
Command line arguments
Output statements
We can print() function to display output
Form1 : print() without any argument
Just in prints new line character
Form2:
print(String):
print(“Hello World”)
We can use escape characters also
print(“Hello \n World”)
print(“Hello\tWorld”)
We can use repetition operator (*) in the string
print(10*”Hello”)
print(“Hello”*10)
We can use + operator also
print(“Hello”+”World”)
Note:
If both arguments are string type then + operator acts as concatenation operator.
If one argument is string type and second is any other type like int then we will get error
If both arguments are number type then + operator acts as arithmetic addition operator
Note:
print(“Hello”+”World”)
print(“Hello”,”World”)
HelloWorld
Hello World
Form3: print() with variable number of arguments
> a,b,c=10,20,30
> print(a,b,c,sep=‘,’)
> print(a,b,c,sep=‘:’)
D:\Python_classes>py test.py
10,20,30
10:20:30
Form4: print() with end attribute
print(“Hello”)
print(“Good”)
print(“Morning”)
Output
Hello
Good
Morning
If we want output in the same line with space.
print(“Hello”,end=‘’)
print(“karthik”,end=‘’)
print(“world”)
output: Hello Karthik World
Note: The default value for end attribute is \n, which is nothing but new line character.
Form5: print(object) statement
We can pass any object ( like list, tuple, set etc ) as argument to the print() statement
I=[10,20,30,40]
t=(10,20,30,40)
print(I)
Print(t)
Form6: print(String, variable list)
We can use print() statement with string and any number of arguments
s=“karthik”
a=48
s1=“Java”
s2=“Python”
print(“Hello”,s,”Your Age is”,a)
print(“You are teaching”,s1,”and”,s2)
Output
Hello Karthik Your Age is 48
You are teaching java and python
Form-7: print (formatted string)
- %I —> int
- %d —> int
- %f —> float
- %s —> String type
Syntax: print(“formatted string” %(variable list))
a=10
b=20
c=30
print(“a value is %I” %a)
print(“b value is %d and c value is %d” %(b,c))
Output
a value is 10
b value is 20 and c value is 30
Eg2:
s=“Karthik”
list=[10,20,30,40]
print(“Hello %s….. The list of items are %s” %(s,list))
Output :
Hello karthik…. The list of items are [10,20,30,40]
Form-8: print() with replacement operator {}
Eg:
name = “Karthik”
salary = 10000
gf = “Sunny”
print(“Hello {0} your salary is {1} and Your Friend {2} is waiting”.format(name, salary,gf))
print(“Hello {x} your salary is {y} and Your Friend {z} is waiting”.format(x=name, y=salary,z=gf))
Output
Hello Karthik your salary is 10000 and Your Friend Sunny is waiting
Hello Karthik your salary is 10000 and Your Friend Sunny is waiting
No comments:
Post a Comment