Python Function Arguments

1) Positional Arguments

Write a function add(a, b) that returns the sum of two numbers. Call it with user input values.


2) Keyword Arguments

Define info(name, age) that prints:

Name: <name>, Age: <age>

Call it using keyword arguments.


3) Default Argument

Write greet(name, msg="Hello") that prints:

<msg>, <name>!

Call it with and without msg.


4) Mixed Arguments

Write describe(name, age=18) that prints a line. Call it once with only name and once with both.


*5) Function with args

Write sum_all(*nums) that returns the sum of all numbers passed.

Test with 3, 5, and 7 values.


**6) Function with kwargs

Write print_info(**data) that prints keys and values.

Call it with:


7) Positional Only (Basic)

Write multiply(x, y) β€” both must be positional. Call it normally.


8) Return Product

Create product(*nums) that returns product of all numbers.


9) Keyword Argument Order

Write book_info(title, author) and call it with keywords in swapped order.


10) Function with Optional Argument

Write display(msg=None). If msg is None, print:

else print msg.


11) Filtering Args

Write filter_even(*nums) that returns only even numbers.


*12) Combining Positional and args

Write first_and_rest(first, *rest) that prints the first value and list of remaining.


13) Sum and Count

Write stats(*nums) that returns a tuple with (sum, count of numbers).


14) Greet Multiple People

Write greet_all(*names) that prints:

for each.


*15) Setting Defaults and args

Write info(name, *args, gender="Not specified") and print all values passed.


**16) Keyword Only After *

Write:

This forces age to be passed as a keyword.

Call it correctly.


17) Combine args and kwargs

Write show(*args, **kwargs) that prints both.

Test with:


18) Swap Arguments

Write function swap(a, b) that returns (b, a). Call with user inputs.


19) Greeting with Formality

Write greet(name, formal=False). If formal is True:

Else:


20) Sum of List with Default

Write:

If nums is None, print 0, else sum.


πŸ“ Topics Practiced

βœ” Positional arguments βœ” Keyword arguments βœ” Default arguments βœ” Variable positional *args βœ” Variable keyword **kwargs βœ” Combination of the above


πŸ‘©β€πŸ’» Example Template

*Sum with args


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

Last updated