>>> def sample(a, b, *args):
... print('a', a)
... print('b', b)
... print('args:', args)
...
>>> sample(1,2)
a 1
b 2
args: ()
>>>
>>> sample(1,2, 3)
a 1
b 2
args: (3,)
>>> sample(1,2, 3, 4)
a 1
b 2
args: (3, 4)
>>> def sample2(a, b, **kwargs):
... print('a', a)
... print('b', b)
... print('kwargs', kwargs)
...
>>> sample2(1,2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sample2() takes 2 positional arguments but 3 were given
>>> sample2(a=1,b=2, d=3)
a 1
b 2
kwargs {'d': 3}
>>> sample2(a=1,b=2)
a 1
b 2
kwargs {}
>>> sample2(a=1,b=2, c=3, d=4)
a 1
b 2
kwargs {'c': 3, 'd': 4}
>>> sample_final(a,b,*args, **kwargs):
File "<stdin>", line 1
sample_final(a,b,*args, **kwargs):
^
SyntaxError: invalid syntax
>>> def sample_final(a,b,*args, **kwargs):
... print("a", a)
... print("b", b)
... print("args:", args)
... print('kwargs:', kwargs)
...
>>> sample_final(c=1, a=2, b=3)
a 2
b 3
args: ()
kwargs: {'c': 1}
>>> sample_final(1,2,3,4,5, sample=10)
a 1
b 2
args: (3, 4, 5)
kwargs: {'sample': 10}
>>> l1 = [1,2,3]
>>> sample_final(*l1)
a 1
b 2
args: (3,)
kwargs: {}
>>> sample_final(3,4, *l1)
a 3
b 4
args: (1, 2, 3)
kwargs: {}
>>> dict1 = {
... 'name': 'ys',
... }
>>> dict1
{'name': 'ys'}
>>> dict1['aa'] = 'sample'
>>> dict1
{'name': 'ys', 'aa': 'sample'}
>>> sample_final(1,2, **kwargs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'kwargs' is not defined
>>> sample_final(1,2, **dict1)
a 1
b 2
args: ()
kwargs: {'name': 'ys', 'aa': 'sample'}
>>> sample_final(1,2, dict1)
a 1
b 2
args: ({'name': 'ys', 'aa': 'sample'},)
kwargs: {}
>>> sample_final(1,2, **dict1)
a 1
b 2
args: ()
kwargs: {'name': 'ys', 'aa': 'sample'}
>>>
>>> def sample_sum(*args):
... return sum(args)
...
>>> sample_sum(1,2,3)
6
>>> sample_sum(1,2,3, 4,5)
15
>>> sample_sum(1,2,3, 4,5, 6)
21
>>> l1 = [1, 2, 3, 4,5,6]
>>> sample_sum(*l1)
21
'Data Analytics' 카테고리의 다른 글
Data Science vs. Big Data vs. Data Analytics (0) | 2020.02.24 |
---|