Hướng dẫn what value do functions that do not contain return statement return in python? - giá trị nào làm các hàm không chứa câu lệnh trả về trả về trong python?
Đưa ra chức năng ví dụ này: Show
Tuyên bố này có làm bất cứ điều gì hữu ích không? Sự khác biệt giữa nó và không có nó là gì? Điều gì sẽ xảy ra nếu không có chức năng trả lại?
Karl Knechtel 59.4K10 Huy hiệu vàng86 Huy hiệu bạc132 Huy hiệu đồng10 gold badges86 silver badges132 bronze badges Đã hỏi ngày 12 tháng 3 năm 2013 lúc 6:52Mar 12, 2013 at 6:52
3 Nếu một hàm không chỉ định giá trị trả về, nó sẽ trả về Trong một câu lệnh IF/SEN có điều kiện, Đã trả lời ngày 12 tháng 3 năm 2013 lúc 6:57Mar 12, 2013 at 6:57
Travis Beartravis gấuTravis Bear 12.3k6 Huy hiệu vàng40 Huy hiệu bạc49 Huy hiệu đồng6 gold badges40 silver badges49 bronze badges 2 Hàm luôn trả về def fun(): statements . . return [expression]2 không được viết. Đã trả lời ngày 12 tháng 3 năm 2013 lúc 7:00Mar 12, 2013 at 7:00
GodmangodmanGodMan 2.5292 Huy hiệu vàng23 Huy hiệu bạc39 Huy hiệu Đồng2 gold badges23 silver badges39 bronze badges Nếu bạn có def fun(): statements . . return [expression]4 Tuy nhiên, bởi vì nó sẽ luôn luôn là def fun(): statements . . return [expression]5, nó hoàn toàn vô nghĩa. Sẽ tốt hơn nếu trả về đúng nếu tệp được viết chính xác, v.v. Nếu bạn không trả lại rõ ràng bất cứ điều gì, giá trị sẽ là Đã trả lời ngày 12 tháng 3 năm 2013 lúc 7:04Mar 12, 2013 at 7:04
Will Richardswill RichardsonWill Richardson 7.5626 Huy hiệu vàng43 Huy hiệu bạc56 Huy hiệu Đồng6 gold badges43 silver badges56 bronze badges 1 Không có nhiều ý nghĩa khi tự mình có một tuyên bố trả lại mà không được quy kết hoặc kiểm tra chức năng. Python trả lại không nếu không có gì được trả lại. Trong trường hợp của bạn, có lẽ bạn nên trả về đúng nếu tệp mở và ghi thành công Đã trả lời ngày 12 tháng 3 năm 2013 lúc 7:11Mar 12, 2013 at 7:11
Dora Doradora 1.1942 Huy hiệu vàng20 Huy hiệu bạc38 Huy hiệu Đồng2 gold badges20 silver badges38 bronze badges 1 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.
Syntax: def fun(): statements . . return [expression] Example: def cube(x): r=x**3 return r Example: Python3Mộ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 fun(): statements . . return [expression]7 def fun(): statements . . return [expression]8 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 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 fun(): statements . . return [expression]7 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 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 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 True0Result of add function is 5 Result of is_true function is True1 Result of add function is 5 Result of is_true function is True2Result of add function is 5 Result of is_true function is True3Result of add function is 5 Result of is_true function is True4Result of add function is 5 Result of is_true function is True5Result of add function is 5 Result of is_true function is True6Result of add function is 5 Result of is_true function is True0 Result of add function is 5 Result of is_true function is True1 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
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; Python3Sử 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 fun(): statements . . return [expression]9 def fun(): statements . . return [expression]7 {'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 200 {'x': 20, 'str': 'GeeksforGeeks'}6 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 ________ 73 & nbsp; & nbsp; def fun(): statements . . return [expression]7 The result is 25 The result is: 1005 Result of add function is 5 Result of is_true function is True7 return True 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 return True 7
Python3{'x': 20, 'str': 'GeeksforGeeks'}3 {'x': 20, 'str': 'GeeksforGeeks'}1 geeksforgeeks 200 {'x': 20, 'str': 'GeeksforGeeks'}6 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 ________ 73 & nbsp; & nbsp; def fun(): statements . . return [expression]7 The result is 25 The result is: 1005 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 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 return True 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 fun(): statements . . return [expression]11
geeksforgeeks 20
Python3{'x': 20, 'str': 'GeeksforGeeks'}3 {'x': 20, 'str': 'GeeksforGeeks'}1 geeksforgeeks 200 {'x': 20, 'str': 'GeeksforGeeks'}6 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 ________ 73 & nbsp; & nbsp; def fun(): statements . . return [expression]7 The result is 25 The result is: 1005 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 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 return True 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 fun(): statements . . return [expression]27 Result of add function is 5 Result of is_true function is True6
['geeksforgeeks', 20]
Python3{'x': 20, 'str': 'GeeksforGeeks'}3 {'x': 20, 'str': 'GeeksforGeeks'}1 geeksforgeeks 200 {'x': 20, 'str': 'GeeksforGeeks'}6 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 ________ 73 & nbsp; & nbsp; def fun(): statements . . return [expression]7 The result is 25 The result is: 1005 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 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 return True 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 fun(): statements . . return [expression]60
{'x': 20, 'str': 'GeeksforGeeks'} def fun(): statements . . return [expression]9{'x': 20, 'str': 'GeeksforGeeks'}6 Result of add function is 5 Result of is_true function is True1 {'x': 20, 'str': 'GeeksforGeeks'}8def fun(): statements . . return [expression]9 None 5Result of add function is 5 Result of is_true function is True1 The result is 25 The result is: 1003 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 {'x': 20, 'str': 'GeeksforGeeks'}6 def fun(): statements . . return [expression]01 Python3{'x': 20, 'str': 'GeeksforGeeks'}6 def fun(): statements . . return [expression]03 Result of add function is 5 Result of is_true function is True1 def fun(): statements . . return [expression]05 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. Xem điều này để biết chi tiết về danh sách. def fun(): statements . . return [expression]9 None 5Result of add function is 5 Result of is_true function is True1 ________ 73 & nbsp; & nbsp; def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 def fun(): statements . . return [expression]24 {'x': 20, 'str': 'GeeksforGeeks'}6 def fun(): statements . . return [expression]26 def fun(): statements . . return [expression]27 Result of add function is 5 Result of is_true function is True1 return True 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 fun(): statements . . return [expression]81 def fun(): statements . . return [expression]82 def fun(): statements . . return [expression]83 def fun(): statements . . return [expression]84 def fun(): statements . . return [expression]7 def fun(): statements . . return [expression]86 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 None 5def fun(): statements . . return [expression]90 def fun(): statements . . return [expression]83 def fun(): statements . . return [expression]7 def fun(): statements . . return [expression]93 def fun(): statements . . return [expression]9 def fun(): statements . . return [expression]2 def fun(): statements . . return [expression]96 Result of add function is 5 Result of is_true function is True0 Result of add function is 5 Result of is_true function is True1 def fun(): statements . . return [expression]99 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 r02 def cube(x): r=x**3 return r03 def fun(): statements . . return [expression]83 def fun(): statements . . return [expression]84 Output: The result is 25 The result is: 100 Hàm không trả về bất kỳ giá trị nào trong Python là gì?Giải thích: Python xác định rõ ràng đối tượng không được trả về nếu không có giá trị nào được chỉ định.None object that is returned if no value is specified.
Hàm nào không có giá trị trả về?Các hàm vô hiệu (không trả lại giá trị): Các hàm void được tạo và sử dụng giống như các hàm trả lại giá trị ngoại trừ chúng không trả về một giá trị sau khi hàm thực thi.: Void functions are created and used just like value-returning functions except they do not return a value after the function executes. |