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

1 vote
1 answer 19 views
19 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 51 views
51 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 5.5k points
0 votes
1 answer 13 views
13 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 5.5k points
1 vote
1 answer 675 views
675 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, 2020 Gavin 15.3k points
0 votes
1 answer 21 views
21 views
I need to know how to return multiple values from a function in Java.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 8 views
8 views
Problem: "Not all code paths return a value" means that inside a function that's supposed to explicitly return something why it's not void or a constructor/IEnumerator)?how compiler found a way to hit the end of the function without a return statement telling it what it's supposed to return?
asked Dec 9, 2020 Mashhoodch 5.6k points
0 votes
1 answer 18 views
18 views
Problem: The strftime() method returns a string representing date and time using date, time or datetime object.ou will to convert date, time and datetime objects to its equivalent string (with the help of examples).The datetime object containing current date and time is stored in now variable.
asked Jan 10 Mashhoodch 5.6k points
0 votes
1 answer 14 views
14 views
In this code : def __repr__(self):   return '<%s %s (%s:%s) %s>' % (     self.__class__.__name__, self.urlconf_name, self.app_name,     self.namespace, self.regex.pattern) What is the significance/purpose of def__repr__(self) ?
asked Oct 23, 2020 psandprop 2.4k points
0 votes
1 answer 26 views
0 votes
1 answer 14 views
14 views
Problem: Hello guys, While working on file handling in python, I know the basic operation like how to open a file, how to read a file, update a file in python but I want to use python file to open multiple file but don't know how to do this. The syntax to open a file object: file_object = open(“filename”, “mode”)The syntax to close the file: file.close()
asked Jun 10, 2020 Gavin 15.3k points