# Python \*args and \*\*kwargs

> All these tasks involve writing small functions that use \*args, \*\*kwargs, or both.

***

#### \**1) Basic args Function*

Write a function `print_args(*args)` that prints all arguments passed to it.

***

#### \**2) Sum Using args*

Write a function `sum_all(*args)` that returns the sum of all numeric arguments.

Example:

```
sum_all(1, 2, 3) → 6
```

***

#### \**3) Count args*

Write a function `count_args(*args)` that returns how many arguments were passed.

***

#### \**4) Max Using args*

Write a function `max_value(*args)` that returns the largest number.

***

#### \**5) Min Using args*

Write a function `min_value(*args)` that returns the smallest number.

***

#### \**6) Average Using args*

Write a function `average(*args)` that calculates and returns the average of numbers passed.

***

#### \*\*7) Greet Using **kwargs**

Write a function `greet(**kwargs)` that expects a keyword `name` and prints:

```
Hello, <name>!
```

***

#### \*\*8) Default in **kwargs**

Modify `greet(**kwargs)` so it prints “Hello, Guest!” if no `name` provided.

***

#### **9) Print All Keys**

Write a function `print_keys(**kwargs)` that prints only the keys passed to it.

Example:

```
print_keys(a=1, b=2) → prints a, b
```

***

#### **10) Print All Values**

Write `print_values(**kwargs)` that prints only values.

***

#### \*\*11) Combine \*args and **kwargs**

Write a function `show(*args, **kwargs)` that prints both args and kwargs separately.

***

#### **12) Sum With Mixed**

Write a function that accepts both \*args and \*\*kwargs — sum numeric \*args and any numeric \*\*kwargs values.

***

#### **13) Personalized Message**

Write:

```python
def message(*args, **kwargs)
```

so you can call:

```
message("Welcome", user="Bob")
```

expected output:

```
Welcome Bob
```

***

#### \**14) Count Characters in args*

Write a function that takes \*args (strings) and prints total character count across all.

***

#### **15) Filter Even Numbers**

Write `filter_even(*args)` returning only even numbers.

***

#### \*\*16) Update Dictionary Using **kwargs**

Write a function that accepts a dictionary and \*\*kwargs to update it.

Example:

```
update_dict({"a":1}, b=2, c=3)
```

→ results in `{"a":1, "b":2, "c":3}`

***

#### \**17) Combine Lists in args*

Write a function that takes multiple lists via \*args and returns one combined list.

***

#### **18) Print Custom Format**

Use both \*args and \*\*kwargs to print:

```
Args: <args_tuple>
Kwargs: key1=value1, key2=value2
```

***

#### \*\*19) Use **kwargs as Settings**

Write a function `settings(**kwargs)` that prints each setting in the format:

```
setting_name → value
```

***

#### \**20) Pass args to Another Function*

Define a helper function `helper(a,b,c)` and write another function that uses \*args to pass arguments to it.

Example:

```
helper_args(*[1,2,3]) → helper(1,2,3)
```

***

### 🧠 Concepts Practiced

| Topic                              | What You Learn                         |
| ---------------------------------- | -------------------------------------- |
| Positional variable args           | Use of `*args`                         |
| Keyword variable args              | Use of `**kwargs`                      |
| Mixed args/kwargs                  | Functions accepting both               |
| Looping over args                  | Accessing multiple inputs              |
| Using kwargs values                | Accessing named parameters dynamically |
| Passing through to other functions | Unpacking                              |

***

### 📌 Mini Examples

**Basic `*args`:**

```python
def print_args(*args):
    for item in args:
        print(item)
```

**Basic `**kwargs`:**

```python
def print_keys(**kwargs):
    for key in kwargs:
        print(key)
```

**Mixing both:**

```python
def show(*args, **kwargs):
    print("Positional:", args)
    print("Keyword:", kwargs)
```

***

canvil:\
333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1
