0% found this document useful (0 votes)
4 views2 pages

Python Functions with Arguments Guide

Uploaded by

saruasim769
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Functions with Arguments Guide

Uploaded by

saruasim769
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Function with Default Argument

Question:
Create a function greet(name, message="Welcome!") that prints:
"Hello, <name>! <message>"

 Call the function with and without the message argument.

Function with args (Variable Number of Arguments)

Question:
Create a function total(*numbers) that takes any number of numeric arguments and
returns their sum.

 Example call: total(5, 10, 15) should return 30.

Function with kwargs (Keyword Arguments)

Question:
Create a function display_info(**details) that prints key-value pairs like:

name: Rajesh
age: 25
city: Kathmandu

Function Using Both args and a Default Argument

Question:
Create a function repeat_words(*words, times=2) that repeats each word the given
number of times.

 Example: repeat_words("Hi", "Bye", times=3)


Output:

Function Using All Types of Parameters Together

Question:
Create a function user_profile(name, age=18, *hobbies, **extra_info) that:
 Prints the name and age.
 Prints all hobbies (if any).
 Prints any additional info passed via **kwargs.

You might also like