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

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 700
    Comment on it
    # A Python program returning multiple values from a method using tuple
     
    # Function is defined that returns a tuple
    
    def fun():
        str = "Demo"
        x   = 20
        return str, x;  # Returning a tuple
     
    # Driver code to test above method
    
    str, x = fun() # Assign returned tuple
    print(str)
    print(x)

    Output:

    Demo
    20

    Explanation of above program:

    A tuple is comma seperated values enclosed within () or without (). The values are of different datatypes but tuple is immutable that is values within tuple cannot be changed via indexes. The value can be accessed via key or index. In above program, a string and integer variable is defined which are then returned from fun(). When fun() is called tuple is received into two variables and thus printing their values. The other method of printing values could be receiving tuple in a variable and printing values via indexes i.e

    tuple= fun() # Assign returned tuple
    print(tuple[0])
    print(tuple[1])

     

 2 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: