• Register
1 vote
23 views

Problem:

I am an intermediate level java programmer, and I can write codes in java bit fluently. Recently, I am learning code in python. If anybody could tell me how can I return multiple values in python it would be very helpful to me. I know the ways how to do it in java but not in python.

Thanks

12 7 7
15,250 points

1 Answer

2 votes
 
Best answer

Solution:

Like java, you can return multiple values in python, There are four ways to do this.

1) Using Object

2) Using Tuple

3) Using a list

4) Using a Dictionary

I am giving you an example of the first method, using an object:

 

This is similar to Java, we can create a class to hold multiple values and return an object of the class.

class Test: 
    def __init__(self): 
        self.str = "I am a String"
        self.x = 20   
  
# This function returns an object of Test 
def fun(): 
    return Test() 
      
# Driver code to test above method 
t = fun()  
print(t.str) 
print(t.x) 

Output:

I am a String
20

 

13 9 6
94,240 points

Related questions

1 vote
1 answer 17 views
17 views
Problem: I am a beginner level java student. So my question might be very easy for you guys. I am just wondering if it is possible to return multiple values in a single program? If it is possible, can you guys tell me the process, please? -Best regards
asked Mar 9 Gavin 15.3k points
0 votes
1 answer 2 views
2 views
Problem: I am working with functions in C++. I am encountering an error that say “not all code paths return a value” while calling the following function: public static int divide(int a, int b) { int div = a / b; Console.Write(div); } I have no idea whats happening. How to resolve the error and why is occuring??
asked 2 days ago Sheeza 2.9k points
1 vote
1 answer 318 views
318 views
Problem: Hello Pal, I have a query regarding the python programming language. The question is, which method will return an empty string when it has attempted to read beyond the end of a file? Do you have any idea about this? Thanks
asked Mar 16 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
I need to know how to return multiple values from a function in Java.
asked Oct 3 Daniel Anderson 4k points
0 votes
1 answer 2 views