Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f902fe1

Browse files
authored
Update Functions_1.py
1 parent 8edffc7 commit f902fe1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Functions_1.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,41 @@ def fun1(x, y, *args, **kwargs):
120120
fun(a=3, d=4):
121121

122122
# 11) Parameters packing and unpacking
123+
def sum(*args):
124+
print(args)
125+
126+
sum(1, 2, 3) # (1, 2, 3)
127+
128+
# another example. The list will be unpacked to separate parameters
129+
def xy(x, y):
130+
print(x)
131+
print(y)
132+
133+
nums = [2, 4]
134+
xy(*nums)
135+
# 2
136+
# 4
137+
138+
# another example. In this case "x" in dict should be named the same as param in function xy
139+
def xy(x, y):
140+
print(x)
141+
print(y)
142+
143+
xxx = {"x": 1, "y": 2}
144+
xy(**xxx)
145+
# 1
146+
# 2
147+
148+
# another example
149+
def multiply(*args):
150+
total = 1
151+
for arg in args:
152+
total *= arg
153+
print(total)
154+
155+
multiply(1, 2, 3, 5) # 30
156+
157+
# another example
123158
def fun2(a, *args):
124159
print(a, args)
125160
if args:

0 commit comments

Comments
 (0)