How to pass optional parameters to a function in Python?

PythonServer Side ProgrammingProgramming

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Furthermore, arguments can be specified in any order by using named arguments.

For the given code


('Hello', 'Archie, Good morning! Come on in')
('Hello', 'Richie, How do you do? Come on in')

Messages msg and msg2 are optional, because they have default values defined. name is a required argument, because it has no default value. If greet is called with only one argument, msg defaults to “Good Morning” and msg2 defaults to “Come on in”. If greet is called with two arguments, msg2 still defaults to “Come on in”.

In most languages, we would need to call the function with three arguments. But in Python, arguments can be specified by name, in any order.

Published on 07-Dec-2017 11:24:48