• Register
1 vote
36 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

Please log in or register to answer this question.

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

0 votes
1 answer 4 views
4 views
Problem: I need someone to understand this problem and find out the solution... can a function return multiple values python
asked Mar 26 Ifra 24.4k points
0 votes
1 answer 3 views
3 views
Problem: how to I Solve this: error: what does the return function do in python HELP! I need help understanding this: what does the return function do in python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
1 vote
1 answer 41 views
41 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, 2020 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Problem: My question is simple. Why do we use return 0 in a C/C++ program if it does not do anything? For example, if I have a program whose output is 100 somehow, then I use return 0 after the print command, the output will still be 100. Then, what is the use of return 0?
asked Mar 2 Code Learner 9.2k points
0 votes
1 answer 263 views
263 views
Problem: Hi there! I am a bit curious about what happens if I skip the return type in function declaration. Does the compiler generate error or the program executes normally? If it executes normally, what return type is used as function return type by default?
asked Nov 28, 2020 Code Learner 9.2k points
0 votes
1 answer 22 views
22 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 Oct 22, 2020 Code Learner 9.2k points
1 vote
1 answer 2 views
2 views
Problem: I know how to return multiple values and that there are various ways to achieve this . My question is which method is most efficient and best for doing this ?
asked 2 days ago Ashty 6k points
0 votes
1 answer 4 views
4 views
Problem: I need assistance in resolving this problem: what types of joins will return the unmatched values from both tables in the join?
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 1 view
1 view
Problem I want to write a c++ source code where return multiple values from a single function, how can i write it, Any help me
asked 1 day ago Subhendu 4.4k points
0 votes
1 answer 20 views
20 views
Problem: I am a noob and my question is simple. What does return 0 do in every C++ program? Is it important to end each program with return 0 and what if we use any number other than 0 in the return statement? I am confused so looking forward to a detailed answer. Thanks!
asked Feb 24 Code Learner 9.2k points