Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Returning Multiple Values in Python using Dictionary?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.61k
    Comment on it
    # A Python program returning multiple values from a method using dictionary
     
    # Function is define that returns a dictionary
    
    def fun():
        d = dict(); 
        d['str'] = "Demo"
        d['x']   = 20
        return d
     
    # Driver code to test above method
    
    d = fun() 
    print(d)

    Output:

    {'x': 20, 'str': 'Demo'}

    Explanation:

    A Dictionary consist of Key Value pairs with syntax Key:Syntax enclosed within {}. The value within Dictionary is accessed via key. Dictionary are updatable i.e values can be edited or deleted via keys. In above program a dictionay is defined via dict() within which key str and x is created with their values. The dictionary is then returned from function fun() and values are then printed. The values can also be printed via keys i.e

    # Driver code to test above method
    d = fun() 
    print(d['str'])
    print(d['x'])

     Output::

    Demo
    20

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: