Skip to content

Commit 963b7b0

Browse files
Adding argskwargs
1 parent f9a7a79 commit 963b7b0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Beginers/Functions/ArgsKwargs.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Understanding Args and Kwargs
3+
4+
What will be the output from the following functions and why
5+
"""
6+
7+
def f(x, y, z):
8+
# uncomment for debug
9+
# print "x=%s y=%s, z=%s" % (x, y, z)
10+
return (x + y) / float(z)
11+
12+
print f(10, 5, 3)
13+
14+
x = 20
15+
y = 10
16+
z = 3
17+
18+
# using kwargs
19+
print f(x=x, y=y, z=z)
20+
21+
# using args and kwargs
22+
print f(y, x, z=2)
23+
24+
# using args and kwargs out of order
25+
print f(z=3, y=x, x=y)
26+
27+
# all x
28+
print f(z=x, y=x, x=x)
29+
30+
"""
31+
With defaults
32+
"""
33+
34+
def ff(x=20, y=10, z=3):
35+
# uncomment for debug
36+
# print "x=%s y=%s, z=%s" % (x, y, z)
37+
return (x + y) / float(z)
38+
39+
print ff(10, 5, 3)
40+
41+
# using args
42+
print ff(10)
43+
44+
# using kwargs
45+
print ff(z=10)
46+
47+
x = 20
48+
print ff(z=x, y=x)
49+
50+

0 commit comments

Comments
 (0)