#5 How to Add Any Number of Arguments with *args | Python Function | Python Tutorial for Beginners

Publié le: 29 mars 2023
sur la chaîne: Learn with Yasir
230
4

‪@yasirbhutta‬ In this video, we explore a powerful feature in Python — the ability to pass a variable number of arguments to a function using `*args`. We define a function called `add()` that takes in any number of arguments and sums them up, no matter how many numbers you provide.

Here’s the code we’ll be working with:

```python
def add(*args):
total = 0
for n in args:
total += n
return total
```

With this `add()` function:
You can pass multiple numbers to be added together.
You can pass any number of arguments (or none at all).
It will handle different input scenarios gracefully, whether you provide several numbers or none.

Example Usage:
```python
print(add(1, 2, 3)) # Output: 6
print(add(4, 5)) # Output: 9
print(add(6)) # Output: 6
print(add()) # Output: 0
```
This is a great introduction to using `*args` in Python to work with variable-length argument lists. Whether you're summing numbers or dealing with flexible inputs, understanding how to use `*args` can make your functions more dynamic and adaptable.

Let me know if you'd like any changes!
If you enjoyed this video, please give it a thumbs up 👍and leave a comment 💬 below. Don't forget to subscribe 🔔to my channel for more Python tutorials like this one.

YouTube Playlists:

Python made easy with YB - join our Beginner's Playlist today!: https://bit.ly/3z9744q

Learn Microsoft Excel:    • Excel 365 Tutorial for Beginners [latest v...  

You can also follow me on:

Website: https://yasirbhutta.github.io/
Facebook:   / yasirbhutta786  
YouTube:   / @yasirbhutta  
Twitter:   / yasirbhutta  

Thanks for watching! 🙏**

#codingshorts #coding #pythonprogramming #codingshortvideo #python #pythonbasics #yasirbhutta #codinglife #pythontutorial


Sur cette page du site, vous pouvez voir la vidéo en ligne #5 How to Add Any Number of Arguments with *args | Python Function | Python Tutorial for Beginners durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Learn with Yasir 29 mars 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 230 fois et il a aimé 4 téléspectateurs. Bon visionnage!