Python Programing Lab EXERCISE - 2(b)

Aim: 

             Write a program add.py that takes 2 numbers as command line arguments and prints its sum.
Description: 

                        Command line arguments are the arguments that are passed to the program when the program is invoked for execution. Python provides a getopt module that helps us to pass command line arguments and options. The Python sys module provides access to any command line arguments via sys.argv. This serves two purposes: 1. sys.argv is the list of command line arguments. 2.len(sys.argv) is the number of command line arguments. The first argument is always script name and it is also being counted in number of arguments. As the command line arguments are strings, here we need to type-cast those arguments to the suitable type.

Algorithm:


Input: Two numbers from command line


Output: Sum of two numbers 


Step1: Start 

Step2: Import sys module 

Step3: Read the arguments using commandline and store the values in a and b 

Step4: Type cast a to integer and b to integer then calculate the addition of a and b and store the result in sum

Step5: Print sum 

Step6: Stop

Program:

step 1: open notepad and write the below program
import sys;
n1=int(sys.argv[1]);
n2=int(sys.argv[2]);
print (n1+n2)
step 2: SAVE
step 3: Open DOS SHELL and go to file saved location (D:\)
step 4: python filename.py argument1 argument2

Output:

Conclusion:
                  gets the knowledge on math functions and how the values are passed from the command line.

1 comment

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...