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

Pubblicato il: 29 marzo 2023
sul canale di: 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


In questa pagina del sito puoi guardare il video online #5 How to Add Any Number of Arguments with *args | Python Function | Python Tutorial for Beginners della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Learn with Yasir 29 marzo 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 230 volte e gli è piaciuto 4 spettatori. Buona visione!