Learn Python - Python tutorial - python val - Python examples - Python programs
In Python, we can return multiple values from a function. Following are different ways
1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class.
python - Sample - python code :
Output:
wikitechy 20
Below are interesting methods for somebody shifting C++/Java world.
2) Using Tuple: A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple and list.
python - Sample - python code :
Output:
wikitechy 20
3) Using a list: A list is like an array of items created using square brackets. They are different from arrays as they can contain items of different types. Lists are different from tuples as they mutable.
python - Sample - python code :
Output:
['wikitechy', 20]
4) Using a Dictionary: A Dictionary is similar to hash or map in other languages.
python - Sample - python code :
Output:
{'x': 20, 'str': 'wikitechy'}