Skip to playerSkip to main contentSkip to footer
  • 5/21/2025

In Python, *args and **kwargs are used in function definitions to allow a function to accept a variable number of arguments.

*args (Non-Keyword Arguments): It is used to pass a variable number of non-keyworded arguments to a function. These arguments are collected into a tuple within the function.

**kwargs (Keyword Arguments): It is used to pass a variable number of keyword arguments to a function. These arguments are collected into a dictionary within the function, where the keys are the argument names and the values are the argument values.

Python

def example_function(*args, **kwargs):
print("Args:", args)
print("Kwargs:", kwargs)

example_function(1, 2, 3, name="Alice", age=30)
# Output:
# Args: (1, 2, 3)
# Kwargs: {'name': 'Alice', 'age': 30}

*args and **kwargs are useful when you don't know in advance how many arguments will be passed to a function, or when you want to create functions that are flexible and can handle different numbers of arguments.

Category

📚
Learning
Transcript
00:00If you have a simple sum function that you can use variable number of arguments and sum.
00:09We can use adder function to adder function and say that we can return value of some of those arguments.
00:25If you have a simple adder function, you can use the number of input values.
00:31Let's say 1 or 2.
00:34We can print values.
00:37However, when I execute it, I will give an error.
00:40If you have a little bit of error, I will give an error.
00:46If you have a simple adder function, it takes one positional argument.
00:55If you have an argument defined, then you can define one argument.
01:01However, two were given.
01:03If you have an error function called, this is a function.
01:20So, we have got to know that, when we sum that we have a number of argument is variable.
01:26So, this is a variable.
01:29So, if we use the number of argument is variable, that's one, two, four, five, six, eight, nine, 10, 11, million,
01:36so we have got to do it.
01:37Basically, we have number of argument is variable.
01:41So, we have got to go with number of argument.
01:45We have got to go with simple.
01:48Now this is the number of argument function call
01:51That will be variable
01:532 can be 6 can be 6 can be 4 can be
01:55So now I have this
01:57Why do you do this
01:58When you define it
01:59Now this is the star arg
02:02So now I have to
02:04Clearly code
02:06This is the number of argument
02:08This is the variable
02:09So now I have to complain
02:10I have to show you
02:11And it's working fine
02:16However
02:16Now you have bound not to
02:20How much argument you can give it
02:22Right
02:23Okay
02:24Now this is the important thing
02:28It's the name args
02:30It's the name of basically
02:33You can do anything
02:35You can do it
02:37It's the name of chao
02:38Whatever you like
02:39But standard practice
02:40It's the name of args
02:44So when you have to do it
02:46You can do it
02:47You can do it
02:48Okay
02:49Excellent
02:50This is the second concept
02:52That is the keyword argument
02:53You can do it
02:54You can do it
02:55You can do it
02:56Values
02:57It's the name of args
02:57It's the name of args
02:58I can do it
03:00You can do it
03:01todos
03:02You can do it
03:03We can do it
03:04Ai-pour- ее
03:05い- hab- failed to
03:05Ti-
03:07And the meaning of args
03:08Is being equal to 10
03:10To
03:11And the answer is
03:12A fix
03:13Now the answer
03:13It is same
03:14There, it says
03:14Now I can
03:15It
03:15تو یہ ہے کہ یہاں پہ میں دو
03:17keyword argument define کر دوں
03:19ایسے price and
03:20tax لیکن
03:23پھر وہی problem ہے کہ وہ مجھے
03:24positional argument کا error دے گا کہے گا
03:26دو define کیا آپ تو پھر price tax کے بعد
03:28تیسرا جب آپ add کریں گے
03:30let's say میں کہتا ہوں
03:31recovery
03:33recovery is equal to 3
03:35let's say
03:36وہ کہے گا یار وہ تو تم نے دو define کی ہے
03:38keyword argument اب تم تین دے رہے ہو
03:40وہی positional argument
03:41تو یہ بھی میں کی جو named argument ہے
03:44یا جس کو keyword argument کہتے ہیں
03:46اس کو میں بھی variable رکھنا چاہتا ہوں
03:48تو اس کو variable رکھنے کے لیے
03:50again we use star quarks
03:52double star sorry
03:53double star
03:54keyword
03:56a
03:56quarks
03:58quarks
03:59اب basically میں
04:02variable number of input دے سکتا ہوں
04:04اور اس کو execute کروں گا کیونکہ میں quarks جو use نہیں کر رہا
04:06تو یہ کچھ بھی نہیں show کرے گا
04:08however چلیے ہم اس quarks کو
04:09دیکھتے ہیں پہلے تو
04:10argument اور keyword argument کا format دیکھتے ہیں
04:13کہ یہ کس format میں basically transfer ہو جاتے ہیں
04:15جب آپ ان کو values دیتے ہیں
04:17تو میں simple یہاں پہ print کا function use کرتا ہوں
04:19print
04:20اور میں کہتا ہوں
04:21یار args کے بارے میں مجھے بتاؤ
04:23یہ کون سا format ہے
04:24اور print کو args کے بارے میں مجھے بتاؤ
04:27کہ
04:27args کے بارے میں بتاؤ
04:29کہ یہ کون سا format ہے
04:30میں اس کو execute کرتا ہوں
04:31تو اب ذرا دیکھئے کہ یہ جو argument ہے
04:33یہ inform of tuple
04:35show ہو رہا ہے
04:36کہ یہ tuple تھا
04:37however keyword argument
04:38ایک dictionary میں convert ہو جاتا ہے
04:39ٹھیک ہے
04:40تو اب اگر میں keyword argument کی values کو access کرنا چاہتا ہوں
04:43میں تو simple یہ کام کر سکتا ہوں
04:44for i in
04:45i in quarks
04:47یہی quarks کی value پکڑ لیتے ہیں
04:50quarks
04:50dot values
04:51ٹھیک ہے
04:52اور dot values کو close کہاں پہ
04:54اور simple میں اس کو
04:55let's say
04:56total کہہ دیتا ہوں
05:00total
05:01is equal to
05:05plus i
05:08ٹھیک ہے
05:09تو اب یہ total ہو جائے گا
05:11total کے اندر میں اس کو
05:12total کو add کر دیتا ہوں
05:13total
05:15ٹھیک ہے
05:18تو اب basically یہ کیا کرے گا
05:20کہ یہ
05:21values اٹھا لے گا
05:22quarks میں سے
05:2310 اٹھا لے گا
05:242 اٹھا لے گا
05:25اور ان کو
05:26i کو assign کرے گا
05:27اور i کو ہم add کرتے جائیں گے
05:28total میں
05:28however
05:29یہ again ہمارے
05:31آپ کو پتا ہے
05:32کہ یہ total کا
05:33basically
05:33error دے گا
05:34کہے گا
05:34total not defined
05:35کیونکہ total ہم نے define نہیں کیا
05:37تو یہاں پہ ہم
05:37total کو کہہ دیتے ہیں
05:39یا تو 0 سے initialize کر
05:40ٹھیک ہے
05:40اب میں اس کو execute کرتا ہوں
05:42you will see
05:42کہ یہ
05:4345 ہو گیا
05:44کیونکہ
05:45اس نے 10
05:46اور 2
05:47اس کے اندر add کر دیا
05:47مثال کے طور پر
05:48میں اس کو مزید simplify کرتا ہوں
05:49تاکہ آپ کو پتا چلے
05:50کہ یہ صحیح خام کر رہا ہے
05:52یہ 3 ہوگا
05:53اب 3 کے اندر
05:5410 گیا
05:5413 اور 15
05:55اس کو ہمیں 15
05:56show کرانا چاہیے
05:57میں اس کو execute کرتا ہوں
05:58as you see
05:59یہ ہمیں 15
06:00show کرانا رہا ہے
06:00ٹھیک ہے
06:01تو اب basically
06:01ہم named parameter
06:03یہاں پہ دے رہے ہیں
06:04اور آپ
06:04multiple named parameter
06:05دے سکتے ہیں
06:06مثال کے طور پر
06:06آپ کہتے ہیں
06:07recovery
06:08is equal to 3
06:10تو اب recovery
06:113 اس کے اندر
06:12add ہو جائے گی
06:12یا let's say
06:13اس کو 5 کر دیتا ہے
06:13تاکہ 20 پورا ہو جائے
06:14میں اس کو execute کرتا ہوں
06:16and
06:20kaboon
06:21shaka
06:22laaka
06:22you see
06:2320
06:23ٹھیک ہے
06:24تو یہ ہے
06:24فرق basically
06:25quars کا
06:26اور
06:27keyword argument
06:28اور argument کا
06:29کہ آپ
06:29اب argument میں بھی
06:31variable number
06:32of argument
06:33دے سکتے ہیں
06:33اور keyword
06:37argument میں بھی
06:38variable number
06:38of keyword
06:39argument
06:39دے سکتے ہیں

Recommended