filmov
tv
Python *ARGS & **KWARGS are awesome! 📦
Показать описание
#python #tutorial #course
00:00:00 intro
00:00:39 *args example 1
00:03:10 *args example 2
00:04:32 **kwargs
00:07:51 exercise
00:14:26 conclusion
# *args = allows you to pass multiple non-key arguments
# **kwargs = allows you to pass multiple keyword-arguments
# * unpacking operator
# ----- *ARGS Example 1 -----
def add(*nums):
total = 0
for num in nums:
total += num
return total
print(add(1, 2, 3, 4))
# ----- *ARGS Example 2 -----
def display_name(*args):
print(f"Hello", end=" ")
for arg in args:
print(arg, end=" ")
display_name("Dr.", "Spongebob", "Harold", "Squarepants", "III")
# ----- **KWARGS -----
def print_address(**kwargs):
print(value, end=" ")
print_address(street="123 Fake St.",
pobox="P.O Box 777",
city="Detroit",
state="MI",
zip="54321")
# ----- EXERCISE -----
def shipping_label(*args, **kwargs):
for arg in args:
print(arg, end=" ")
print()
if "apt" in kwargs:
elif "pobox" in kwargs:
else:
shipping_label("Dr.", "Spongebob", "Squarepants",
street="123 Fake St.",
pobox="PO box #1001",
city="Detroit",
state="MI",
zip="54321")
00:00:00 intro
00:00:39 *args example 1
00:03:10 *args example 2
00:04:32 **kwargs
00:07:51 exercise
00:14:26 conclusion
# *args = allows you to pass multiple non-key arguments
# **kwargs = allows you to pass multiple keyword-arguments
# * unpacking operator
# ----- *ARGS Example 1 -----
def add(*nums):
total = 0
for num in nums:
total += num
return total
print(add(1, 2, 3, 4))
# ----- *ARGS Example 2 -----
def display_name(*args):
print(f"Hello", end=" ")
for arg in args:
print(arg, end=" ")
display_name("Dr.", "Spongebob", "Harold", "Squarepants", "III")
# ----- **KWARGS -----
def print_address(**kwargs):
print(value, end=" ")
print_address(street="123 Fake St.",
pobox="P.O Box 777",
city="Detroit",
state="MI",
zip="54321")
# ----- EXERCISE -----
def shipping_label(*args, **kwargs):
for arg in args:
print(arg, end=" ")
print()
if "apt" in kwargs:
elif "pobox" in kwargs:
else:
shipping_label("Dr.", "Spongebob", "Squarepants",
street="123 Fake St.",
pobox="PO box #1001",
city="Detroit",
state="MI",
zip="54321")
Комментарии