<jupyter_start><jupyter_text>Unpacking<jupyter_code>a, b = 2, 3
# swap values

a, b = b, a
print(a, b)
# creating another tuple in the memory
print(type((1)))
print(type((1,)))
# () not important for tuple, ',' is important
a, *b = (1,2,3,4,5)
print(a, " - ", b)

a, *b = [1,2,3,4,5]
print(a, " - ", b)

a, *b = {'1','2','3','4','5'}
print(a, " - ", b)
# unordered set
a, *b, c = (1,2,3,4,5)
print(a, " - ", b, " - ", c)
l1 = [1,2,3]
l2 = [4,5,6]

l = [*l1, *l2]
print(l)
h1 = {'a':1, 'b':2}
h2 = {'c':3, 'd':4}
h3 = {'e':5, 'f':6}

h = {**h1, **h2, **h3}
print(h)
def func(a, b, *args):
    print(a)
    print(b)
    print(args)
l = [1, 2, 3, 4] # list is 1 element so it should not work
func(l)
func(*l) # we are unpacking list to tuple
func(**l) # ** using for mapping such as dictionary
def ave(*args):
    count = len(args)
    sume = sum(args)
    return count and sume/count
ave(1, 2, 3, 4, 5)
ave()
# positional arguments (a, b, c) , keyword argument d (a, b, c, d=10)
def func(*args, **kwargs):
    print(args)
    print(kwargs)
func(1, 2, 3, 4, 5, c=1, d=3)
def func(*, d=10):
    print(d)
    # only * means, we dont accept any positional argument after that
func(1, d=10)
def calc(*args, to_console=False):
    hi = int(bool(args)) and max(args)
    lo = min(args) if len(args)>0 else 0
    avg = (hi+lo)/2
    
    if to_console:
        print(hi, lo, avg)
    return format(avg, '0.0f')
avg = calc(1, 2, 3, 4, 5)
avg
calc()
calc(to_console=True)
is_debug = True
avg = calc(1, 2, 3, 4, 5, to_console=is_debug)
import time
def time_it(fn, *args, rep=1, **kwargs):
    start = time.perf_counter()
    for i in range(rep):
        fn(*args, **kwargs)
    end = time.perf_counter()
    return (end-start)/rep
time_it(print, 1,2,3, rep=5, sep=' - ', end=' ***\n')<jupyter_output>1 - 2 - 3 ***
1 - 2 - 3 ***
1 - 2 - 3 ***
1 - 2 - 3 ***
1 - 2 - 3 ***
{'fraction_non_alphanumeric': 0.22252155172413793, 'fraction_numerical': 0.056573275862068964, 'mean_word_length': 3.2204545454545452, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '527631', 'license': 'MIT', 'n_tokens_mistral': 875, 'n_tokens_neox': 831, 'n_words': 244, 'path': 'function_parameters.ipynb', 'repo_name': 'ntgai/deep-dive-python', 'size': '1856'}