*args and **kwargs in python

0 votes

I know as what is *args used for but i am not sure about **kwargs

Jul 20, 2018 in Python by eatcodesleeprepeat
• 4,670 points
19 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In cases when we don’t know how many arguments will be passed to a function, like when we want to pass a list or a tuple of values, we use *args.

  1. >>> def func(*args):

  2.   for i in args:

  3.       print(i)

  4. >>> func(3,2,1,4,7)

3

2

1

4

7

**kwargs takes keyword arguments when we don’t know how many there will be.

  1. >>> def func(**kwargs):

  2.   for i in kwargs:

  3.       print(i,kwargs[i])

  4. >>> func(a=1,b=2,c=7)

a.1

b.2

c.7

The words args and kwargs are convention, and we can use anything in their place.

answered Jul 20, 2018 by Priyaj
• 56,140 points

Related Questions In Python

0 votes
0 answers

what is the use of args and kwargs in python?

can you an example for each? READ MORE

May 10 in Python by Waseem
• 2,870 points
15 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,710 points
58 views
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,070 points
26 views
+1 vote
3 answers
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
69 views
0 votes
1 answer

Behaviour of increment and decrement operators in Python

When you want to increment or decrement, ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 56,140 points
440 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 56,140 points
104 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.