How to Pass Arguments to a Function in Python? (Positional, Keyword, Default, *args, **kwargs)

preview_player
Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ How to Pass Arguments to a Function in Python? (Positional, Keyword, Default, *args, **kwargs)

You can send parameters to a Function in Python by passing its values, also known as arguments. ALL arguments in Python are declared after the function name, in parentheses. When you call a function with arguments in Python, the values of these arguments are copied into their corresponding parameters, inside the function.

def hello(name):
print('Hello,', name)

hello('Jérémy') # OUTPUT: Hello, Jérémy
hello('Digital Academy') # OUTPUT: Hello, Digital Academy

Obviously, you can send as many arguments as you like, separated by commas ",".

def func(name, job):
print(name, 'is a', job)

func('Jérémy', 'YouTuber') # OUTPUT: Jérémy is a YouTuber

---

○ Positional Arguments in Python

The most common arguments in Python are positional arguments, whose values are copied to their corresponding parameters - and in order. The only downside of positional arguments is that you need to pass arguments in the order in which they are defined.

def func(name, job):
print(name, 'is a', job)

func('Jérémy', 'YouTuber') # OUTPUT: Jérémy is a YouTube
func('YouTuber', 'Jérémy') # OUTPUT: YouTuber is a Jérémy

---

○ Keyword Arguments in Python

To avoid positional argument confusion, you can pass arguments using the names of their corresponding parameters. In this case, the order of the arguments does no longer matter, because arguments are matched by name - not by its position.

def func(name, job):
print(name, 'is a', job)

func(name='Jérémy', job='developer') # OUTPUT: Jérémy is a developer
func(job='YouTuber', name='Digital Academy') # OUTPUT: Digital Academy is a YouTuber

Please note that: It is also possible to combine positional and keyword arguments, in a single call. If you do so, specify the positional arguments BEFORE keyword arguments.

---

○ Default Arguments in Python

You can specify default values for arguments, when defining a function in Python. And the default value is used if the function is called without a corresponding argument. In short, default arguments in Python allow you to make selected arguments optional.

def func(name, job='developer'):
print(name, 'is a', job)

func('Jérémy') # OUTPUT: Jérémy is a developer
func('Jérémy', 'YouTuber') # OUTPUT: Jérémy is a YouTuber

---

○ Variable Length Arguments in Python (*args, **kwargs)

Variable length arguments in Python are very useful, when you want to create functions that take unlimited number of arguments. Unlimited, in the sense that: you don't know beforehand how many arguments can be passed to a function by the user.

○ *args

When you prefix a parameter with an asterisk * , it collects ALL the unmatched positional arguments into a tuple. Because it is a normal tuple object, you can perform any operation that a tuple supports, like: indexing, iteration, etc.

def print_arguments(*args):
print(args)

print_arguments(1, 54, 60, 8, 98, 12)
# OUTPUT: (1, 54, 60, 8, 98, 12)

○ **kwargs

The ** syntax is similar, but it ONLY works for keyword arguments. It collects them into a new dictionary in Python, where the argument names are the keys and their values are the dictionary values.

def print_arguments(**kwargs):
print(kwargs)

print_arguments(name='Jérémy', age=27, job='developer')
# OUTPUT: {'name': 'Jérémy', 'age': 27, 'job': 'developer'}

Let's play this video, stick around and watch until the end of this video! 👍🏻

- Digital Academy™ 🎓

***

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡

♡ SUPPORT US ♡

***
Рекомендации по теме
Комментарии
Автор

How to Pass Arguments to Functions in Python? 🤔

DigitalAcademyOnline
Автор

Thanks for sharing this video on Functions in Python 🙏🏻

xJrmy