Skip to content

Instantly share code, notes, and snippets.

@ashutoshkrris
Created March 23, 2022 03:06
Show Gist options
  • Save ashutoshkrris/fe85f95ced7f0df2488aef122a7e1910 to your computer and use it in GitHub Desktop.
Save ashutoshkrris/fe85f95ced7f0df2488aef122a7e1910 to your computer and use it in GitHub Desktop.
Code file for *args and **kwargs
"""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))
@SnappyRiffs
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment