1. 곱(*) or 거듭제곱(**)

2. 리스트형 컨테이터 데이터 반복 확장

l = [1] # [1]
l = l * 10 # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

3. 가변인자(Variadic Parameters) 사용

파이썬 인자 종류

  1. positional arguments
  2. keyword arguments
  3. variadic positional arguments
def save_ranking(first, second, third=None, fourth=None):
    rank = {}
    rank[1], rank[2] = first, second
    rank[3] = third if third is not None else 'Nobody'
    rank[4] = fourth if fourth is not None else 'Nobody'
    print(rank)

firstsecond라는 두 개의 positional arguments를 받으며 thirdfourth라는 두 개의 keyword arguments 받음

keyword arguments : 생략가능 ( default값이 들어감 ) 그러므로 positional argument이전에 들어가면 안됨.

가변인자의 경우

def save_ranking(*args, **kwargs):
    print(args)
    print(kwargs)
save_ranking('ming', 'alice', 'tom', fourth='wilson', fifth='roy')
# ('ming', 'alice', 'tom') - 튜플
# {'fourth': 'wilson', 'fifth': 'roy'} - dict

args는 임의의 갯수의 positional arguments를 받음을 의미하며, *kwargs는 임의의 갯수의 keyword arguments를 받음을 의미한다. 이 때 args*kwargs 형태로 가변인자를 받는걸 packing이라고 한다.

4. 컨테이너타입 데이터 unpacking

from functools import reduce
# reduce()는 입력받은 컨테이너 타입(iterable)을 지정한 함수에 따라 계산한 후 단일값으로 결과 반환

primes = [2, 3, 5, 7, 11, 13]

def product(*numbers):
    p = reduce(lambda x, y: x * y, numbers)
    return p

product(*primes)
# 30030

product(primes)
# [2, 3, 5, 7, 11, 13]

그냥 primes로 인자를 전달하면 primes 리스트 그 자체가 하나의 원소로 들거어가게되지만

*primes로 전달하면 unpacking되어 하나하나 인자로 전달된다.

tuple도 list와 정확히 동일하게 동작하며 dict의 경우  대신 ******을 사용하여 동일한 형태로 사용할 수 있다.