Bài viết này mô tả cách trả về nhiều giá trị từ một hàm trong Python.
- Trả về nhiều giá trị bằng dấu phẩy
- Trả lại
2result = test[] print[result] print[type[result]] # ['abc', 100] #
Xem bài viết sau đây cho những điều cơ bản của các chức năng trong Python.
- Xác định và gọi các chức năng trong Python [def, return]
Trả về nhiều giá trị bằng dấu phẩy
Trả lại
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
2Xem bài viết sau đây cho những điều cơ bản của các chức năng trong Python.
def test[]:
return 'abc', 100
Xác định và gọi các chức năng trong Python [def, return]
Trong Python, bạn có thể trả về nhiều giá trị bằng cách đơn giản là chúng được phân tách bằng dấu phẩy.
Built-in Types - Tuples — Python 3.7.4 documentation
- Ví dụ, xác định một hàm trả về một chuỗi và số nguyên như sau:
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
Trong Python, các giá trị phân tách bằng dấu phẩy được coi là các bộ đếm không có dấu ngoặc đơn, ngoại trừ khi được yêu cầu bởi cú pháp. Vì lý do này, hàm trong ví dụ trên trả về một tuple với mỗi giá trị dưới dạng một phần tử.
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
Lưu ý rằng đó thực sự là dấu phẩy tạo ra một tuple, không phải dấu ngoặc đơn. Các dấu ngoặc đơn là tùy chọn, ngoại trừ trong trường hợp tuple trống hoặc khi chúng cần thiết để tránh sự mơ hồ của cú pháp. Các loại tích hợp - Tuples - Python 3.7.4 Tài liệu
# print[result[2]]
# IndexError: tuple index out of range
Một tuple với một yếu tố yêu cầu một dấu phẩy trong Python
- Mỗi phần tử có một loại được xác định trong hàm.
a, b = test[]
print[a]
# abc
print[b]
# 100
Tất nhiên, việc chỉ định một chỉ mục vượt quá số lượng giá trị trả về được xác định làm tăng lỗi.
def test2[]:
return 'abc', 100, [0, 1, 2]
a, b, c = test2[]
print[a]
# abc
print[b]
# 100
print[c]
# [0, 1, 2]
Trả lại result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
2
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
Xem bài viết sau đây cho những điều cơ bản của các chức năng trong Python.
def test_list[]:
return ['abc', 100]
result = test_list[]
print[result]
print[type[result]]
# ['abc', 100]
#
Trong Python, chúng ta có thể trả về nhiều giá trị từ một hàm. Sau đây là những cách khác nhau
1] Sử dụng đối tượng: Điều này tương tự như C/C ++ và Java, chúng ta có thể tạo một lớp [tính bằng C, Struct] để giữ nhiều giá trị và trả về một đối tượng của lớp. 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.
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
8 result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
9print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0____21 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
2print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
3print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
4print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
5print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
3print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 # print[result[2]]
# IndexError: tuple index out of range
0print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
5print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
3# print[result[2]]
# IndexError: tuple index out of range
3print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 ________ 35 & nbsp; & nbsp; & nbsp;print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
1 # print[result[2]]
# IndexError: tuple index out of range
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
3 a, b = test[]
print[a]
# abc
print[b]
# 100
0a, b = test[]
print[a]
# abc
print[b]
# 100
1print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 a, b = test[]
print[a]
# abc
print[b]
# 100
3a, b = test[]
print[a]
# abc
print[b]
# 100
4a, b = test[]
print[a]
# abc
print[b]
# 100
5print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8a, b = test[]
print[a]
# abc
print[b]
# 100
7a, b = test[]
print[a]
# abc
print[b]
# 100
4a, b = test[]
print[a]
# abc
print[b]
# 100
9Output:
geeksforgeeks 20
Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.
2] Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có []. Tuples là bất biến. Xem điều này để biết chi tiết về Tuple và Danh sách. 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.
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
1 # print[result[2]]
# IndexError: tuple index out of range
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
3 a, b = test[]
print[a]
# abc
print[b]
# 100
0a, b = test[]
print[a]
# abc
print[b]
# 100
1print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 a, b = test[]
print[a]
# abc
print[b]
# 100
3Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.
2] Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có []. Tuples là bất biến. Xem điều này để biết chi tiết về Tuple và Danh sách.
a, b = test[]
print[a]
# abc
print[b]
# 100
4def test_list[]:
return ['abc', 100]
result = test_list[]
print[result]
print[type[result]]
# ['abc', 100]
#
9print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8a, b = test[]
print[a]
# abc
print[b]
# 100
7a, b = test[]
print[a]
# abc
print[b]
# 100
4geeksforgeeks 203
Output:
geeksforgeeks 20
0print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0A 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 are mutable.# print[result[2]]
# IndexError: tuple index out of range
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
1 # print[result[2]]
# IndexError: tuple index out of range
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
3 a, b = test[]
print[a]
# abc
print[b]
# 100
0a, b = test[]
print[a]
# abc
print[b]
# 100
1print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 a, b = test[]
print[a]
# abc
print[b]
# 100
3Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.
2] Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có []. Tuples là bất biến. Xem điều này để biết chi tiết về Tuple và Danh sách.
a, b = test[]
print[a]
# abc
print[b]
# 100
4def test_list[]:
return ['abc', 100]
result = test_list[]
print[result]
print[type[result]]
# ['abc', 100]
#
9result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
2a, b = test[]
print[a]
# abc
print[b]
# 100
7Output:
['geeksforgeeks', 20]
0print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0 A Dictionary is similar to hash or map in other languages. See
this for details of dictionary.# print[result[2]]
# IndexError: tuple index out of range
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
1 # print[result[2]]
# IndexError: tuple index out of range
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
3 a, b = test[]
print[a]
# abc
print[b]
# 100
0a, b = test[]
print[a]
# abc
print[b]
# 100
1print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 a, b = test[]
print[a]
# abc
print[b]
# 100
3Dưới đây là những phương pháp thú vị cho ai đó thay đổi C ++/Java World.
2] Sử dụng tuple: Một tuple là một chuỗi các mục được phân tách bằng dấu phẩy. Nó được tạo ra có hoặc không có []. Tuples là bất biến. Xem điều này để biết chi tiết về Tuple và Danh sách.
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 # print[result[2]]
# IndexError: tuple index out of range
0a, b = test[]
print[a]
# abc
print[b]
# 100
4result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
22Output:
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
0print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0def test2[]:
return 'abc', 100, [0, 1, 2]
a, b, c = test2[]
print[a]
# abc
print[b]
# 100
print[c]
# [0, 1, 2]
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 # print[result[2]]
# IndexError: tuple index out of range
55] Using Data Class [Python 3.7+]: In Python 3.7 and above the Data Class can be used to return a class with automatically added unique methods. The Data Class module has a decorator and functions for automatically adding generated special methods such as __init__[] and __repr__[] in the user-defined classes.
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
3 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8def test_list[]:
return ['abc', 100]
result = test_list[]
print[result]
print[type[result]]
# ['abc', 100]
#
3result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
27print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8def test_list[]:
return ['abc', 100]
result = test_list[]
print[result]
print[type[result]]
# ['abc', 100]
#
5print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 def test_list[]:
return ['abc', 100]
result = test_list[]
print[result]
print[type[result]]
# ['abc', 100]
#
7print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
31print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
34result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
353] Sử dụng danh sách: Danh sách giống như một mảng các mục được tạo bằng dấu ngoặc vuông. Chúng khác với các mảng vì chúng có thể chứa các vật phẩm của các loại khác nhau. Danh sách khác với các bộ dữ liệu vì chúng có thể thay đổi.
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0geeksforgeeks 201
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 ________ 35 & nbsp; & nbsp; & nbsp;print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
3 geeksforgeeks 206
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
8geeksforgeeks 208
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
2 print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 a, b = test[]
print[a]
# abc
print[b]
# 100
34] Sử dụng từ điển: Từ điển tương tự như băm hoặc bản đồ bằng các ngôn ngữ khác. Xem điều này để biết chi tiết về từ điển.
a, b = test[]
print[a]
# abc
print[b]
# 100
4geeksforgeeks 203
a, b = test[]
print[a]
# abc
print[b]
# 100
4result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
72result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
73print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
75result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
76result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
77result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
78print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
80result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
76result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
77result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
83print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
64a, b = test[]
print[a]
# abc
print[b]
# 100
7Output:
result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
1Reference://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python
//stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
0['geeksforgeeks', 20]9
print[result[0]]
print[type[result[0]]]
# abc
#
print[result[1]]
print[type[result[1]]]
# 100
#
9 result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
01result = test[]
print[result]
print[type[result]]
# ['abc', 100]
#
02Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.‘