default function arguments in python code example

Example 1: python default arguments

def my_function(name, age = 20):
  print(name + " is " + str(age) + " years old"

my_function("Mitro") # Mitro is 20 years old
my_function("Mitro", 26) #Mitro is 26 years old

Example 2: Python Default Arguments

def greet(name, msg="Good morning!"):
    """
    This function greets to
    the person with the
    provided message.

    If the message is not provided,
    it defaults to "Good
    morning!"
    """

    print("Hello", name + ', ' + msg)


greet("Kate")
greet("Bruce", "How do you do?")

Example 3: python 3 define type in arguments with default value

def foo(opts: dict = {}):
    pass

print(foo.__annotations__)