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,710 points
825 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
• 58,020 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 11, 2019 in Python by Waseem
• 4,540 points
708 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
2,338 views
0 votes
1 answer

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

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

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
851 views
+1 vote
7 answers
0 votes
2 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
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
• 58,020 points
3,760 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
• 58,020 points
2,393 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP