Saturday, September 12, 2020

386 - write a program in python to display the sum of two numbers using command line arguments and not by using input function

 386 - write a program in python to display the sum of two numbers using command line arguments and not by using input function


 

write a program in python to display the sum of two

          numbers using command line arguments

                  and not by using input function

==========================================

 

Code:

import sys

sum = int(sys.argv[1]) + int(sys.argv[2])

print("Sum of numbers:",sum)

 

Analysis:

       Here , the arguments passed to the command line

is usually in the datatype of a string which needs

to be converted to integer type before any type of

mathematical operation could occur over the object .

So the arguments are converted to integer datatype by

using the 'int' function over the provided input . In the

program we have mentioned values as argv[1]

and argv[2] as the command line usually accepts any

type of argument first by the name of the program

which is stored in the list and can be accessed from

the list by the list's first element that is argv[0] in each

case , the subsequent arguments are stored in [1] , [2]

and so on . In our case , as we are doing an addition of

two numbers , so we will use the arguments stored at

list's [1] and [2] index , so we have used argv[1] and

argv[2] to access the elements and do the addition

operation .

 

 


No comments:

Post a Comment

Durga Puja in Odisha

 Durga Puja in Odisha