Ask coding questions

← Back to all posts
How to create an optional positional argument in python
Dunce (44)

How would I create an optional positional argument in python? All the instructions I've been able to find are unclear.

Answered by InvisibleOne (2672) [earned 5 cycles]
View Answer
Comments
hotnewtop
InvisibleOne (2672)

something like this:

def banana(name, color='yellow'): # color will default to yellow if not argument is given
    print("Nice " + name + " banana that is " + color)

banana("joey") # will print out the color as yellow
banana("bill", "green") # will print out the color as green
Dunce (44)

@InvisibleOne That's an optional keyword argument, I know how to do that. I was wondering how to do an optional positional argument.

Dunce (44)

@InvisibleOne Oh ok, I thought you had to use a module. Thanks!

RYANTADIPARTHI (5999)

wdym by optional position?