Answers for "get args in python"

9

pass argument to a py file

import sys

def hello(a,b):
    print "hello and that's your sum:", a + b

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    hello(a, b)
# If you type : py main.py 1 5
# It should give you "hello and that's your sum:6"
Posted by: Guest on May-28-2020
11

read argument from terminal

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

#Terminal
# $ python test.py arg1 arg2 arg3

#print
#Number of arguments: 4 arguments.
#Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
Posted by: Guest on May-22-2020
0

python get args

import sys

print(sys.argv)
Posted by: Guest on February-23-2021
0

args in python

# if args is not passed it return message
# "Hey you didn't pass the arguements"

def ech(nums,*args):
    if args:
        return [i ** nums for i in args]  # list comprehension
    else:
        return "Hey you didn't pass args"
    
print(ech(3,4,3,2,1))
Posted by: Guest on June-24-2021
0

args in python

def func2(*args):
    for i in args:
        print(i * 2)
        
func2(2,3,4,5)
Posted by: Guest on June-25-2021
0

args in python

# if option is not passed it still returns 20
# if option = True or option = 1 is passed it returns 20 which is sum of numbers
# if option = False or option = 0 is passed it returns 0 

def addition(a, b, *args, option=True):
   result = 0
   if option:
      for i in args:
      result += i
      return a + b + result
   else:
      return result
Posted by: Guest on June-24-2021

Python Answers by Framework

Browse Popular Code Answers by Language