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:


**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:


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:

so you can call:

expected output:


*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:

β†’ 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:


**19) Use kwargs as Settings

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


*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:


🧠 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:

Basic **kwargs:

Mixing both:


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

Last updated