unlimited arguments python
def add(*args): # *args takes multiple inputs
return sum(args)
print(add(1,2,3,4,5)) # prints 15
print(add(10, 20, 30)) # prints 60
unlimited arguments python
def add(*args): # *args takes multiple inputs
return sum(args)
print(add(1,2,3,4,5)) # prints 15
print(add(10, 20, 30)) # prints 60
unlimited keyword arguments python
def calculate(n, **kwargs):
print(n + kwargs['add'])
print(n * kwargs['multiply'])
calculate(3, add=4, multiply=5)
args kwargs python
>>> def argsKwargs(*args, **kwargs):
... print(args)
... print(kwargs)
...
>>> argsKwargs('1', 1, 'slgotting.com', upvote='yes', is_true=True, test=1, sufficient_example=True)
('1', 1, 'slgotting.com')
{'upvote': 'yes', 'is_true': True, 'test': 1, 'sufficient_example': True}
variable number of arguments to python class
def multiply(*args):
z = 1
for num in args:
z *= num
print(z)
multiply(4, 5)
multiply(10, 9)
multiply(2, 3, 4)
multiply(3, 5, 10, 6)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us