@yasirbhutta #yasirbhutta
In this quick Python quiz, we explore the concept of `**kwargs` and how it works inside functions! 🐍💡
Code Explanation:
We use `**kwargs` in a function to accept an arbitrary number of keyword arguments and pack them into a dictionary. In this example, the function `display_data` takes in keyword arguments and prints the type of the `kwargs` object. Can you guess what the output will be when we pass arguments like `name="Ali"` and `age=25`?
Question:
*What will the following code output?*
```python
def display_data(**kwargs):
print(type(kwargs))
display_data(name="Ali", age=25)
```
Options:
A) class 'set'
B) class 'tuple`
C) class 'list'
D) class 'dict'
Watch the video to test your Python skills and learn more about how `**kwargs` works! 🎥👨💻
Code Analysis
```python
def display_data(**kwargs):
print(type(kwargs))
display_data(name="Ali", age=25)
```
*`def display_data(**kwargs):`*
This function uses `**kwargs`, which allows you to pass a variable number of keyword arguments (i.e., named arguments) to the function. The keyword arguments are collected in the form of a dictionary inside the function.
*`print(type(kwargs))`*
This line prints the type of the object `kwargs`.
*`display_data(name="Ali", age=25)`*
When this function is called, two keyword arguments are passed: `name="Ali"` and `age=25`. These arguments will be stored in `kwargs` as a dictionary:
```python
kwargs = {'name': 'Ali', 'age': 25}
```
Output Explanation
Since `kwargs` is a dictionary, the `print(type(kwargs))` statement will output the type of this object. The type of `kwargs` is class 'dict`.
Correct Answer:
*D) class 'dict'*
Thus, the output will be class 'dict'.
👉 *Make sure to like, comment, and subscribe for more Python quizzes and tutorials!*
You can also follow me on:
Website: https://yasirbhutta.github.io/
Facebook: / yasirbhutta786
YouTube: / @yasirbhutta
Twitter: / yasirbhutta
Thanks for watching! 🙏**
#yasirbhutta #codingshorts #pythonprogramming #codingshortvideo #pythonbasics #pythontutorial #codinglife #python #Python #PythonQuiz #kwargs #PythonFunctions #PythonCoding #PythonMCQ #PythonProgramming #LearnPython #PythonForBeginners #PythonBasics #PythonTutorial #PythonInterviewQuestions #CodingQuiz #ProgrammingQuiz #PythonDictionary #PythonDataTypes #PythonTips #TechQuiz #PythonChallenge #CodingChallenges
On this page of the site you can watch the video online Python Quiz #41: function Quiz for Beginners | def Quiz | Python MCQs | Python For Beginners with a duration of hours minute second in good quality, which was uploaded by the user Learn with Yasir 28 August 2024, share the link with friends and acquaintances, this video has already been watched 196 times on youtube and it was liked by 1 viewers. Enjoy your viewing!