Hàm trong Python là gì với ví dụ
Ý tưởng là đặt một số tác vụ thường được thực hiện hoặc lặp đi lặp lại với nhau và tạo một hàm sao cho thay vì viết đi viết lại cùng một mã cho các đầu vào khác nhau, chúng ta có thể thực hiện các lệnh gọi hàm để sử dụng lại mã chứa trong đó nhiều lần. Show
cú pháp. Hàm Python
Tạo một hàm PythonChúng ta có thể tạo một hàm Python bằng cách sử dụng từ khóa def Python3def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression9
The addition of 5 and 15 results 20.0 The addition of 5 and 15 results 20.1 The addition of 5 and 15 results 20.2 The addition of 5 and 15 results 20.3_______1_______4 The addition of 5 and 15 results 20.5_______1_______6 Gọi hàm PythonSau khi tạo một hàm, chúng ta có thể gọi nó bằng cách sử dụng tên của hàm theo sau là dấu ngoặc đơn chứa các tham số của hàm cụ thể đó Python3def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression9 The addition of 5 and 15 results 20.0 The addition of 5 and 15 results 20.1 False True0_______1_______3 The addition of 5 and 15 results 20.4 The addition of 5 and 15 results 20.5_______1_______6
False True5 False True6 đầu ra Welcome to GFG Định nghĩa và gọi hàm có tham sốNếu bạn có kinh nghiệm về C/C++ hoặc Java thì bạn phải suy nghĩ về kiểu trả về của hàm và kiểu dữ liệu của đối số. Điều đó cũng có thể xảy ra trong Python (dành riêng cho Python 3. 5 trở lên) cú pháp. Hàm Python có tham số def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression Ví dụ sau sử dụng các đối số mà bạn sẽ tìm hiểu sau trong bài viết này để bạn có thể quay lại nếu chưa hiểu. Nó được định nghĩa ở đây cho những người có kinh nghiệm trước đây về các ngôn ngữ như C/C++ hoặc Java Python3The addition of 5 and 15 results 20.0 False True8 False True9 even odd0 False True9 The addition of 5 and 15 results 20.6 even odd3 even odd4 False True9 even odd6 False True0_______23_______8 False True0_______33_______0 x: 10 y: 501 x: 10 y: 502 x: 10 y: 503 x: 10 y: 504
False True0____33_______6 x: 10 y: 500
x: 10 y: 508 x: 10 y: 509_______33_______1 Geeks Practice Geeks Practice1 Geeks Practice Geeks Practice2 Geeks Practice Geeks Practice3 Geeks Practice Geeks Practice4 x: 10 y: 501 Geeks Practice Geeks Practice6 The addition of 5 and 15 results 20.3_______44_______8 Geeks Practice Geeks Practice9 The addition of 5 and 15 results 20.6 đầu ra The addition of 5 and 15 results 20. Một số ví dụ khác như sau
Python3Hello Welcome to GeeksforGeeks1 The addition of 5 and 15 results 20.0 Hello Welcome to GeeksforGeeks3 False True0_______55_______5 Hello Welcome to GeeksforGeeks6 Hello Welcome to GeeksforGeeks7 Hello Welcome to GeeksforGeeks8 Hello Welcome to GeeksforGeeks9 Geeks Practice Geeks Practice2 first == Geeks mid == for last == Geeks1 first == Geeks mid == for last == Geeks2 first == Geeks mid == for last == Geeks3 x: 10 y: 506 first == Geeks mid == for last == Geeks5 False True0_______55_______5 first == Geeks mid == for last == Geeks8 x: 10 y: 501 x: 10 y: 501 Syntax: print(function_name.__doc__)1 The addition of 5 and 15 results 20.6 Syntax: print(function_name.__doc__)3 first == Geeks mid == for last == Geeks8 Syntax: print(function_name.__doc__)5 Hello Welcome to GeeksforGeeks9 x: 10 y: 501 x: 10 y: 501 Syntax: print(function_name.__doc__)9 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression00 first == Geeks mid == for last == Geeks3 x: 10 y: 506 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression03 False True0_______0_______05 x: 10 y: 501 first == Geeks mid == for last == Geeks1 False True0_______0_______09 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression05_______0_______11 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression12 x: 10 y: 501 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression14 first == Geeks mid == for last == Geeks3 Hello Welcome to GeeksforGeeks5 Hello Welcome to GeeksforGeeks6 Syntax: print(function_name.__doc__)5 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression05 x: 10 y: 501 x: 10 y: 501 Syntax: print(function_name.__doc__)9 even odd6 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression24 x: 10 y: 506 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression03 first == Geeks mid == for last == Geeks3 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression05 x: 10 y: 503 x: 10 y: 501 Hello Welcome to GeeksforGeeks9 False True0_______33_______6 first == Geeks mid == for last == Geeks5 The addition of 5 and 15 results 20.3_______0_______36 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression37 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression38 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression39 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression40 đầu ra False True Đối số của hàm PythonĐối số là các giá trị được truyền bên trong dấu ngoặc đơn của hàm. Một hàm có thể có bất kỳ số lượng đối số nào được phân tách bằng dấu phẩy Trong ví dụ này, chúng ta sẽ tạo một hàm đơn giản để kiểm tra xem số được truyền dưới dạng đối số cho hàm là chẵn hay lẻ Python3def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression41 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression42
The addition of 5 and 15 results 20.0 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression44 False True0_______55_______5 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression47 Syntax: print(function_name.__doc__)5 Hello Welcome to GeeksforGeeks9 x: 10 y: 501 x: 10 y: 501 Syntax: print(function_name.__doc__)9 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression00 first == Geeks mid == for last == Geeks3 The addition of 5 and 15 results 20.3 The addition of 5 and 15 results 20.4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression57_______1_______6 False True0_______0_______60 even odd6 first == Geeks mid == for last == Geeks3 The addition of 5 and 15 results 20.3 The addition of 5 and 15 results 20.4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression65_______1_______6
def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression67 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression68 Hello Welcome to GeeksforGeeks9 The addition of 5 and 15 results 20.6 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression68 first == Geeks mid == for last == Geeks1 The addition of 5 and 15 results 20.6 đầu ra even odd Các loại lập luậnPython hỗ trợ nhiều loại đối số có thể được chuyển vào thời điểm gọi hàm. Hãy thảo luận chi tiết từng loại đối số mặc địnhĐối số mặc định là tham số giả định giá trị mặc định nếu giá trị không được cung cấp trong lệnh gọi hàm cho đối số đó. Ví dụ sau minh họa Đối số mặc định. Python3def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression74 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression75
The addition of 5 and 15 results 20.0 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression77 x: 10 y: 501 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression79 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression00 False True0_______1_______3 The addition of 5 and 15 results 20.4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression84_______0_______85 False True0_______1_______3 The addition of 5 and 15 results 20.4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression89_______0_______90
def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression91 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression92 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression93_______0_______94 The addition of 5 and 15 results 20.6 Đầu ra x: 10 y: 50 Giống như đối số mặc định của C++, bất kỳ số lượng đối số nào trong một hàm đều có thể có giá trị mặc định. Nhưng một khi chúng ta có một đối số mặc định, tất cả các đối số ở bên phải của nó cũng phải có giá trị mặc định Đối số từ khóaÝ tưởng là cho phép người gọi chỉ định tên đối số với các giá trị để người gọi không cần nhớ thứ tự của các tham số Python3def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression96 The addition of 5 and 15 results 20.0 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression98 False True0_______1_______3 The addition of 5 and 15 results 20.01
The addition of 5 and 15 results 20.02 The addition of 5 and 15 results 20.03_______33_______1 The addition of 5 and 15 results 20.05 The addition of 5 and 15 results 20.06 x: 10 y: 501 The addition of 5 and 15 results 20.08 The addition of 5 and 15 results 20.6 The addition of 5 and 15 results 20.10_______33_______1 The addition of 5 and 15 results 20.08 The addition of 5 and 15 results 20.13 x: 10 y: 501 The addition of 5 and 15 results 20.05 The addition of 5 and 15 results 20.6 Đầu ra Geeks Practice Geeks Practice Đối số có độ dài thay đổiTrong Python, chúng ta có thể truyền một số lượng đối số khác nhau cho một hàm bằng các ký hiệu đặc biệt. Có hai ký hiệu đặc biệt
ví dụ 1. Đối số không phải từ khóa có độ dài thay đổi con trănThe addition of 5 and 15 results 20.17 The addition of 5 and 15 results 20.18
The addition of 5 and 15 results 20.0 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression93 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 The addition of 5 and 15 results 20.22 False True0_______1_______24 The addition of 5 and 15 results 20.25 Hello Welcome to GeeksforGeeks7 The addition of 5 and 15 results 20.27 first == Geeks mid == for last == Geeks3 The addition of 5 and 15 results 20.3 The addition of 5 and 15 results 20.30
def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression93_______1_______32 Geeks Practice Geeks Practice2 The addition of 5 and 15 results 20.34 Geeks Practice Geeks Practice2 The addition of 5 and 15 results 20.36 Geeks Practice Geeks Practice2 The addition of 5 and 15 results 20.38 The addition of 5 and 15 results 20.6 Đầu ra Hello Welcome to GeeksforGeeks ví dụ 2. Đối số từ khóa có độ dài thay đổi Python3The addition of 5 and 15 results 20.17 The addition of 5 and 15 results 20.41
The addition of 5 and 15 results 20.0 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression93 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 The addition of 5 and 15 results 20.46 False True0_______1_______24 The addition of 5 and 15 results 20.49 Hello Welcome to GeeksforGeeks7 The addition of 5 and 15 results 20.51 first == Geeks mid == for last == Geeks3 The addition of 5 and 15 results 20.3 The addition of 5 and 15 results 20.4_______1_______55 Syntax: print(function_name.__doc__)5 The addition of 5 and 15 results 20.57
x: 10 y: 508 The addition of 5 and 15 results 20.59 x: 10 y: 501 The addition of 5 and 15 results 20.05 The addition of 5 and 15 results 20.62 x: 10 y: 501 The addition of 5 and 15 results 20.64 The addition of 5 and 15 results 20.65 x: 10 y: 501 The addition of 5 and 15 results 20.05 The addition of 5 and 15 results 20.6 Đầu ra first == Geeks mid == for last == Geeks chuỗi tài liệuChuỗi đầu tiên sau hàm được gọi ngắn gọn là chuỗi Tài liệu hoặc Chuỗi tài liệu. Điều này được sử dụng để mô tả chức năng của chức năng. Việc sử dụng chuỗi tài liệu trong các chức năng là tùy chọn nhưng nó được coi là một thông lệ tốt Cú pháp dưới đây có thể được sử dụng để in ra chuỗi tài liệu của hàm Syntax: print(function_name.__doc__) Ví dụ. Thêm Docstring vào chức năng Python3def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression41 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression42
The addition of 5 and 15 results 20.0 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression44 False True0____1_______74 False True0 False True0_______55_______5 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression47 Syntax: print(function_name.__doc__)5 Hello Welcome to GeeksforGeeks9 x: 10 y: 501 x: 10 y: 501 Syntax: print(function_name.__doc__)9 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression00 first == Geeks mid == for last == Geeks3 The addition of 5 and 15 results 20.3 The addition of 5 and 15 results 20.4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression57_______1_______6 False True0_______0_______60 even odd6 first == Geeks mid == for last == Geeks3 The addition of 5 and 15 results 20.3 The addition of 5 and 15 results 20.4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression65_______1_______6
def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression67 The addition of 5 and 15 results 20.3_______11_______00 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression0 Câu lệnh return trong hàm PythonCâu lệnh trả về hàm được sử dụng để thoát khỏi một hàm và quay lại trình gọi hàm và trả về giá trị hoặc mục dữ liệu đã chỉ định cho trình gọi. cú pháp. def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression1 Câu lệnh trả về có thể bao gồm một biến, một biểu thức hoặc một hằng số được trả về khi kết thúc thực thi hàm. Nếu không có điều nào ở trên xuất hiện với câu lệnh trả về, một đối tượng Không được trả về Ví dụ. Tuyên bố trả về hàm Python Python3The addition of 5 and 15 results 20.0 False True02 False True0____11_______04 False True0_______11_______06 False True0_______33_______6 False True09 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 Hello Welcome to GeeksforGeeks9
The addition of 5 and 15 results 20.3_______11_______14 Hello Welcome to GeeksforGeeks9 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression40 The addition of 5 and 15 results 20.3_______11_______14 even odd3 False True20 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression40 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression2 Chuyển theo tham chiếu hoặc chuyển theo giá trịMột điều quan trọng cần lưu ý là, trong Python mọi tên biến đều là một tham chiếu. Khi chúng ta truyền một biến cho hàm, một tham chiếu mới đến đối tượng được tạo. Truyền tham số trong Python giống như truyền tham chiếu trong Java Python3False True22 The addition of 5 and 15 results 20.0 False True24 False True0_______11_______26 Syntax: print(function_name.__doc__)9 False True28 x: 10 y: 501 False True30
False True31 False True32 False True33 x: 10 y: 501 Hello Welcome to GeeksforGeeks8 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression94 Geeks Practice Geeks Practice2 False True38 Geeks Practice Geeks Practice2 False True40 Geeks Practice Geeks Practice2 False True42 Geeks Practice Geeks Practice2 False True44 Geeks Practice Geeks Practice2 Geeks Practice Geeks Practice3 False True28 False True48 The addition of 5 and 15 results 20.3_______11_______50 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression3 Khi chúng tôi chuyển một tham chiếu và thay đổi tham chiếu đã nhận thành một thứ khác, kết nối giữa tham số đã truyền và nhận bị ngắt. Ví dụ, hãy xem xét chương trình dưới đây như sau Python3The addition of 5 and 15 results 20.0 False True24
False True0_______11_______54 False True0____11_______56 False True0____11_______58 False True0_______11_______60 x: 10 y: 501 Hello Welcome to GeeksforGeeks8_______11_______30 Geeks Practice Geeks Practice2 False True65 Geeks Practice Geeks Practice2 False True67 False True28
False True69 False True32 False True33 x: 10 y: 501 Hello Welcome to GeeksforGeeks8 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression94 Geeks Practice Geeks Practice2 False True38 Geeks Practice Geeks Practice2 False True40 Geeks Practice Geeks Practice2 False True42 Geeks Practice Geeks Practice2 False True44 Geeks Practice Geeks Practice2 Geeks Practice Geeks Practice3 False True28 False True48 The addition of 5 and 15 results 20.3_______11_______50 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression4 Một ví dụ khác để chứng minh rằng liên kết tham chiếu bị hỏng nếu chúng ta gán một giá trị mới (bên trong hàm). Python3The addition of 5 and 15 results 20.0 False True24
False True0_______11_______54 False True0____11_______56 False True0____11_______58 False True0_______11_______60 x: 10 y: 501 False True30
False True69 False True32 False True60_______33_______1 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression94 even odd06 The addition of 5 and 15 results 20.3_______23_______08 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression5 Bài tập. Hãy thử đoán đầu ra của đoạn mã sau. Python3The addition of 5 and 15 results 20.0 even odd10 False True0_______23_______12 x: 10 y: 501 False True60 False True0_______11_______60 x: 10 y: 501 even odd18 False True0_______23_______18____33_______1 even odd12
x: 10 y: 508 False True60_______33_______1 Hello Welcome to GeeksforGeeks9 even odd18_______33_______1 first == Geeks mid == for last == Geeks1 even odd30 The addition of 5 and 15 results 20.3_______23_______08 The addition of 5 and 15 results 20.3_______23_______34 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression6 Hàm ẩn danh trong Python HàmTrong Python, một hàm ẩn danh có nghĩa là một hàm không có tên. Như chúng ta đã biết từ khóa def được sử dụng để xác định các hàm thông thường và từ khóa lambda được sử dụng để tạo các hàm ẩn danh. Vui lòng xem cái này để biết chi tiết Python3even odd35 even odd36
The addition of 5 and 15 results 20.0 even odd38 x: 10 y: 506 False True60 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 False True60 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 False True60
even odd45_______33_______1 even odd47 even odd48 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 False True60 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression11 False True60
The addition of 5 and 15 results 20.3_______23_______54 even odd55 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression40 The addition of 5 and 15 results 20.3_______23_______58 even odd55 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression40 Đầu ra def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression7 Hàm Python trong HàmMột hàm được xác định bên trong một hàm khác được gọi là hàm bên trong hoặc hàm lồng nhau. Các hàm lồng nhau có thể truy cập các biến của phạm vi kèm theo. Các chức năng bên trong được sử dụng để chúng có thể được bảo vệ khỏi mọi thứ xảy ra bên ngoài chức năng 4 loại hàm trong Python là gì?Sau đây là các loại Hàm Python khác nhau. . Hàm tích hợp Python Hàm đệ quy Python Hàm Lambda trong Python Các hàm do người dùng định nghĩa trong Python Các chức năng cơ bản trong Python là gì?Các hàm tích hợp sẵn của Python . chức năng in ( ) loại ( ) chức năng hàm đầu vào ( ) hàm abs( ) hàm pow() hàm dir( ) hàm sắp xếp ( ) hàm tối đa ( ) Hàm LÀ GÌ và cách sử dụng nó trong Python?Bạn có thể xác định chức năng để cung cấp chức năng cần thiết. Dưới đây là các quy tắc đơn giản để xác định hàm trong Python. Các khối hàm bắt đầu bằng từ khóa def theo sau là tên hàm và dấu ngoặc đơn ( ( ) ). Mọi tham số hoặc đối số đầu vào phải được đặt trong các dấu ngoặc đơn này
3 chức năng trong Python là gì?Có ba chức năng trong python mang lại tính thực tế và hữu dụng khi lập trình. Ba chức năng này, cung cấp một phong cách lập trình chức năng trong ngôn ngữ python hướng đối tượng, là map(), filter() và reduce() . |