Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc return statement is used to end the execution of the function call and “returns” the result [value of the expression following the return keyword] to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed.
Bàn luận Return statement can not be used outside the function.
Syntax:
def fun[]: statements . . return [expression]
Example:
def cube[x]: r=x**3 return r
Example:
Python3
Một câu lệnh trả về được sử dụng để kết thúc việc thực hiện cuộc gọi chức năng và trả về kết quả [giá trị của biểu thức theo từ khóa trả về] cho người gọi. Các tuyên bố sau các tuyên bố trả lại không được thực thi. Nếu câu lệnh trả về không có bất kỳ biểu thức nào, thì giá trị đặc biệt không được trả về. & Nbsp; một bản trả về được sử dụng chung để gọi một hàm để có thể thực thi các câu lệnh được truyền.
Lưu ý: Không thể sử dụng câu lệnh trả về bên ngoài chức năng.
def
add[a, b]:
____10
def cube[x]: r=x**3 return r1
def cube[x]: r=x**3 return r2
def cube[x]: r=x**3 return r3
def
def cube[x]: r=x**3 return r5
Result of add function is 5 Result of is_true function is True7
Result of add function is 5 Result of is_true function is True8
Result of add function is 5 Result of is_true function is True9
geeksforgeeks 200
geeksforgeeks 201
geeksforgeeks 202
____10
def cube[x]: r=x**3 return r8
def cube[x]: r=x**3 return r9
Result of add function is 5 Result of is_true function is True7
Result of add function is 5 Result of is_true function is True8
['geeksforgeeks', 20]2
geeksforgeeks 200
geeksforgeeks 201
geeksforgeeks 202
Output:
Result of add function is 5 Result of is_true function is True
Result of add function is 5
Result of is_true function is True
0____21 Result of add function is 5
Result of is_true function is True
2Result of add function is 5
Result of is_true function is True
3Result of add function is 5
Result of is_true function is True
4Result of add function is 5
Result of is_true function is True
5Result of add function is 5
Result of is_true function is True
6
Result of add function is 5 Result of is_true function is True0____21
geeksforgeeks 205
Result of add function is 5 Result of is_true function is True3
geeksforgeeks 207
geeksforgeeks 208
Result of add function is 5 Result of is_true function is True6
- Trả về nhiều giá trị 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.
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. & NBSP; & nbsp;
Python3
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 [trong C, Struct] để giữ nhiều giá trị và trả về một đối tượng của lớp. & NBSP;
Thí dụ
['geeksforgeeks', 20]6
['geeksforgeeks', 20]7
def
{'x': 20, 'str': 'GeeksforGeeks'}0
{'x': 20, 'str': 'GeeksforGeeks'}1
{'x': 20, 'str': 'GeeksforGeeks'}2
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
geeksforgeeks 2003056
Result of add function is 5 Result of is_true function is True1
{'x': 20, 'str': 'GeeksforGeeks'}8
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
The result is 25 The result is: 1001
Result of add function is 5 Result of is_true function is True1 ________ 63 & nbsp; & nbsp;
def
The result is 25 The result is: 1005
Result of add function is 5 Result of is_true function is True7
def
3{'x': 20, 'str': 'GeeksforGeeks'}6
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True7
def
7The result is 25 The result is: 100
8 A Tuple is a comma separated sequence of items. It is created with or without []. Tuples are immutable. See this for details of tuple.
Python3
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
geeksforgeeks 2003056
Result of add function is 5 Result of is_true function is True1
{'x': 20, 'str': 'GeeksforGeeks'}8
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
The result is 25 The result is: 1001
Result of add function is 5 Result of is_true function is True1 ________ 63 & nbsp; & nbsp;
def
The result is 25 The result is: 1005
____10
The result is 25 The result is: 1008
The result is 25 The result is: 1009
Result of add function is 5 Result of is_true function is True1
def
1Result of add function is 5 Result of is_true function is True7
Result of add function is 5 Result of is_true function is True8
{'x': 20, 'str': 'GeeksforGeeks'}6
Result of add function is 5 Result of is_true function is True6
Result of add function is 5 Result of is_true function is True7
def cube[x]: r=x**3 return r01
- Output:
geeksforgeeks 20
- 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. 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 are mutable. See this for details of list.
Python3
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
geeksforgeeks 2003056
Result of add function is 5 Result of is_true function is True1
{'x': 20, 'str': 'GeeksforGeeks'}8
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
The result is 25 The result is: 1001
Result of add function is 5 Result of is_true function is True1 ________ 63 & nbsp; & nbsp;
def
The result is 25 The result is: 1005
____10
The result is 25 The result is: 1008
The result is 25 The result is: 1009
Result of add function is 5 Result of is_true function is True1
def
1Result of add function is 5 Result of is_true function is True7
Result of add function is 5 Result of is_true function is True8
def cube[x]: r=x**3 return r17
Result of add function is 5 Result of is_true function is True6
- Output:
['geeksforgeeks', 20]
- 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. A Dictionary is similar to hash or map in other languages. See this for details of dictionary.
Python3
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
geeksforgeeks 2003056
Result of add function is 5 Result of is_true function is True1
{'x': 20, 'str': 'GeeksforGeeks'}8
{'x': 20, 'str': 'GeeksforGeeks'}3
{'x': 20, 'str': 'GeeksforGeeks'}1
The result is 25 The result is: 1001
Result of add function is 5 Result of is_true function is True1 ________ 63 & nbsp; & nbsp;
def
The result is 25 The result is: 1005
____10
The result is 25 The result is: 1008
The result is 25 The result is: 1009
Result of add function is 5 Result of is_true function is True1
def
1Sử 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.
Result of add function is 5 Result of is_true function is True7
def cube[x]: r=x**3 return r50
- Output:
{'x': 20, 'str': 'GeeksforGeeks'}
{'x': 20, 'str': 'GeeksforGeeks'}
6 Result of add function is 5
Result of is_true function is True
1 {'x': 20, 'str': 'GeeksforGeeks'}
8
add[a, b]:
55____21
The result is 25 The result is: 1003
____10
{'x': 20, 'str': 'GeeksforGeeks'}6
1Python3
{'x': 20, 'str': 'GeeksforGeeks'}6
3Result of add function is 5 Result of is_true function is True1
5Sử 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. Xem điều này để biết chi tiết về danh sách.
add[a, b]:
5
Result of add function is 5 Result of is_true function is True1 ________ 63 & nbsp; & nbsp;
____10
def cube[x]: r=x**3 return r14
{'x': 20, 'str': 'GeeksforGeeks'}6
def cube[x]: r=x**3 return r16
def cube[x]: r=x**3 return r17
Result of add function is 5 Result of is_true function is True1
def
1Result of add function is 5 Result of is_true function is True7
Result of add function is 5 Result of is_true function is True8
def cube[x]: r=x**3 return r71
def cube[x]: r=x**3 return r72
def cube[x]: r=x**3 return r73
def cube[x]: r=x**3 return r74
Sử dụng từ điển: Từ điển tương tự như băm hoặc bản đồ trong các ngôn ngữ khác. Xem điều này để biết chi tiết về từ điển.
def cube[x]: r=x**3 return r27
Result of add function is 5 Result of is_true function is True1
def cube[x]: r=x**3 return r29
def cube[x]: r=x**3 return r30
def cube[x]: r=x**3 return r32
def cube[x]: r=x**3 return r33
def cube[x]: r=x**3 return r34
Result of add function is 5 Result of is_true function is True1
def cube[x]: r=x**3 return r36
def cube[x]: r=x**3 return r32
def cube[x]: r=x**3 return r39
def cube[x]: r=x**3 return r40
Result of add function is 5 Result of is_true function is True1
The result is 25 The result is: 1003
____10
def cube[x]: r=x**3 return r45
Result of add function is 5 Result of is_true function is True7
Result of add function is 5 Result of is_true function is True8
def cube[x]: r=x**3 return r92
def cube[x]: r=x**3 return r93
def cube[x]: r=x**3 return r73
def cube[x]: r=x**3 return r74
Output:
The result is 25 The result is: 100