*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
36 views

1 answer to this question.

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,900 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
• 4,480 points
50 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,720 points
81 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,080 points
44 views
+1 vote
3 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 278 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,900 points
871 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,900 points
239 views