Python | Set 6 (Command Line and Variable Arguments)

Previous Python articles (

# Python code to demonstrate using the & # 39; sys & # 39; module
# for command line arguments

 

import sys

 
# command line arguments are in the form
# list in sys.argv

argumentList = sys.argv

print argumentList

 
# Print file name

print sys.argv [ 0 ]

 
# Print the first argument after the file name

print sys.argv [ 1 ]

OUTPUT:

 ['program1.py',' test', '1'] program1.py test 

NOTE The above code only works on the command line. We need to run the below command, given that the program is saved as program1.py
python program1.py test 123

Please note the following points regarding the above program :

  • Sys.argv accepts command line arguments as a list.
  • The first item in the list — this is the filename.
  • Arguments always come as a string, even if we enter an integer in the argument list. We need to use the int () function to convert a string to an integer.
  • We can use command line arguments to write programs that do commonly used tasks. For example, we need to find the factorial many times. We can save the following program to a file called factorial.py on our computer and get the output by simply writing a command to get the factorial of a number, say 5.
    python factorial.py 5

    import sys

    from math import factorial

     

    print factorial ( int (sys.argv [ 1 ]))




    Variable Arguments

    args (*) and kwargs (**)

    Both args and kwargs are used to get an arbitrary number of arguments in fu functions.

    args will give us all the parameters of the function as a list, and kwargs will give us all the key arguments, except for those that correspond to the formal parameter as a dictionary.

    # Python program to illustrate the use of arguments that
    # multiplies all values ​​given to the function as a parameter

     

    def multiplyAll ( * values):

      mul = 1  

     

    # values ​​(arguments) will be in the form tuple

    print values ​​

    print "Type =" , type (values)

      

      

      # Multiply all parameters and return the result

    for i in values:

    mul = mul * i

     

      return mul

      

     
    # Driver program to test the above function

     
    # Multiply two numbers using the function above

    ans = multiplyAll ( 1 , 2 )

    print "The multiplication of 1 and 2 is" , ans

     
    # Multiply 5 numbers using the above function

    ans = multiplyAll ( 3 , 4 , 5 , 6 , 7 )

    print "The multiplication of 3 to 7 is" , ans

    OUTPUT:

 (1, 2) Type = The multiplication of 1 and 2 is 2 ( 3, 4, 5, 6, 7) Type = The multiplication of 3 to 7 is 2520 

Note that arguments are denoted with one star, and quarks are denoted by two stars before formal parameters in the function.

# Program to illustrate the use of kwargs in Python

 
# Function that prints student data
# Parts per student may vary

def printDetails ( * * details):

  

  # Variable & # 39; details & # 39; contains details in

# vocabulary form

print "Parameter details contains"

print details 

  print " Type = " , type (details)

 

# Print name

print "First Name =" , details [ 'firstName' ]

  

  # Print student department

print "Department =" , details [ 'department' ]

  print "" # Optional line break

 

 
# Driver program to test the above function

 
# Call a function with three arguments

printDetails (firstName = "Nikhil"

  rollNumber = " 007 " ,

department = " CSE " )

  
# Calling a function with two arguments

printDetails (firstName = "Abhay" ,

department = "ECE" )

Exit :

 Parameter details contains {'department':' CSE', 'rollNumber':' 007', 'firstName':' Nikhil'} Type = First Name = Nikhil Department = CSE Parameter details c ontains {'department':' ECE', 'firstName':' Abhay'} Type = First Name = Abhay Department = ECE 

Please note that if you use in a function for both args and kwargs, the args parameter must precede the kwarg parameters. 
Example:

# A function containing both args and kwargs

def cheeseshop (kind, * arguments, * * keywords):

print "- Do you have any" , kind, "? "

  print " - I'm sorry, we're all out of " , kind

for arg in arguments:  

print arg

print "-" * 40

  keys = sorted (keywords.keys ())

for kw in keys:

  print kw, ":" , keywords [kw]

 
# Driver program to test the above function

cheeseshop ( " Burger " , " It's very funny, sir. " ,

  "It's really very, VERY funny, sir." ,

shopkeeper = 'Michael Palin' ,

client = "John Cleese" ,

sketch = "Cheese Shop Sketch" )

This article is courtesy of  Kumar Singh

Please post comments if you find anything wrong or if you would like to share more information on the topic under discussion





Get Solution for free from DataCamp guru