Created
March 23, 2022 03:06
-
-
Save ashutoshkrris/fe85f95ced7f0df2488aef122a7e1910 to your computer and use it in GitHub Desktop.
Code file for *args and **kwargs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Sum of Two Numbers | |
def add(x, y): | |
return x+y | |
print(add(2, 3)) | |
""" | |
"""Sum of Three Numbers | |
def add(x, y, z): | |
return x+y+z | |
print(add(2, 3, 5)) | |
""" | |
"""Problematic | |
def add(x, y, z): | |
return x+y+z | |
print(add(2, 3)) | |
""" | |
""" *args in Python """ | |
def add(*numbers): | |
total = 0 | |
for num in numbers: | |
total += num | |
return total | |
print(add(2, 3)) | |
print(add(2, 3, 5)) | |
print(add(2, 3, 5, 7)) | |
print(add(2, 3, 5, 7, 9)) | |
""" **kwargs in Python """ | |
def total_fruits(**fruits): | |
total = 0 | |
for amount in fruits.values(): | |
total += amount | |
return total | |
print(total_fruits(banana=5, mango=7, apple=8)) | |
print(total_fruits(banana=5, mango=7, apple=8, oranges=10)) | |
print(total_fruits(banana=5, mango=7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice