Is it posible to have an optional veriable in a funtion?

Such as this:

>>>def Say(msg, (optional)):
>>>    print msg
>>>    print optional
>>>    
>>>Say('hi', 'option')
hi
option
>>>Say('not')
not

If so How?

Yes is called default argument.

def say(msg, arg='something'):
    print 'first argument <{}> default argument <{}>'.format(msg, arg)

>>> say('hello')
first argument <hello> default argument <something>
>>> say('hello', 'world')
first argument <hello> default argument <world>

Many arguments.

def foo(*arg):
    return sum(arg)

>>> foo(1,2,3,4,5,6,7,8,9,10)
55  

Keyword arguments.

def bar(**kwargs):
    for key, value in kwargs.items():
        print '{}: {}'.format(key, value)

>>> bar(first_name='Clark kent', last_name='Superman')
first_name: Clark kent
last_name: Superman

snippsat nailed it. that's the way

You could do this ...

def say(msg, optional=""):
   print(msg)
   if optional:
       print(optional)

say('hi', 'option')

'''
hi
option
'''

print('-'*12)

say('not')

'''
not
'''

But this is far simpler ...

def say(*args):
    for arg in args:
        print(arg)


say('hi', 'option')

'''
hi
option
'''

print('-'*12)

say('not')

'''
not
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.