Python Function Optional Argument

Python Function Optional Argument

In Python, you can define functions that accept optional arguments, also known as default arguments.

These are arguments that have a default value assigned to them.

So if the caller does not provide a value for that argument, the default value is used instead.

What is the syntax for optional arguments?

The syntax for the functions with optional arguments is pretty much similar to normal functions.

The only difference is that now there should be some optional arguments defined along with required arguments.

def function_name(required_argument, optional_argument = value){
    #body of the function
}

Keep one thing in mind regarding Python functions with optional arguments.

The optional argument must come after the required argument.

Here’s an example of a function with an optional argument:

Python Function With Optional Argument

def greet(name, greeting="Hello"):
    print(greeting + ", " + name)

greet("John") 
greet("Mary", "Hi")

Output

Hello, John
Hi, Mary

In this example, the greet() function takes two arguments name and greeting.

The greeting argument is optional, and has a default value of “Hello”.

This means that if the caller does not provide a value for greeting,

the function will use “Hello” as the default value.

In the first call to greet(), we only provide a value for the name.

So the function uses the default value of “Hello” for greeting.

In the second call, we provide values for both name and greeting.

So the function uses the provided value of “Hi” for greeting.

If you have both optional and required arguments in a function, the required arguments must come before the optional arguments.

Here are some more example programs to illustrate the concept.

1: Python function to repeat string with optional argument

repeat_string() repeats a given string a specified number of times.

The number of times to repeat is optional and defaults to 3.

def repeat_string(string, times=3):
    return string * times

result = repeat_string("Python Mania\n")
print(result)

Output

Python Mania
Python Mania
Python Mania

2: Python function to join a list of strings to one string

join_strings() joins a list of strings into a single string, with an optional delimiter.

A delimiter is one or more characters that separate text strings.

The delimiter defaults to an empty string.

def join_strings(strings, delimiter=" "):
    return delimiter.join(strings)

list = ["I", "Love", "Python", "Mania!"]
result = join_strings(list)
print(result)

Output

I Love Python Mania!

Now, let’s pass the comma as a delimiter.

result = join_strings(list, ",")
print(result)

Output

I,Love,Python,Mania!

3: Python function to get the current time with optional argument

get_current_time() returns the current time as a string, with an optional format.

The format defaults to “%H:%M:%S”.

from datetime import datetime

def get_current_time(format="%H:%M:%S"):
    return datetime.now().strftime(format)


time = get_current_time()
print(time)

Output

11:37:18

You can change the format by passing the required format as an argument to the get_current_time().

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x