Ý 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.
cú pháp. Hàm Python
Tạo một hàm Python
Chúng ta có thể tạo một hàm Python bằng cách sử dụng từ khóa def
Python3
def 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
The addition of 5 and 15 results 20.4
The addition of 5 and 15 results 20.5
The addition of 5 and 15 results 20.6
Gọi hàm Python
Sau 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ể đó
Python3
def 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.10
The addition of 5 and 15 results 20.3
The addition of 5 and 15 results 20.4
The addition of 5 and 15 results 20.5
The addition of 5 and 15 results 20.6
The addition of 5 and 15 results 20.15
The addition of 5 and 15 results 20.16
đầu ra
The addition of 5 and 15 results 20.8
Đị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
Python3
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.18
The addition of 5 and 15 results 20.19_______230
The addition of 5 and 15 results 20.19
The addition of 5 and 15 results 20.6
The addition of 5 and 15 results 20.33
The addition of 5 and 15 results 20.34
The addition of 5 and 15 results 20.19
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.38
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.30
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.32
The addition of 5 and 15 results 20.33
The addition of 5 and 15 results 20.34
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.30
The addition of 5 and 15 results 20.38
The addition of 5 and 15 results 20.39
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.41____442
The addition of 5 and 15 results 20.43
The addition of 5 and 15 results 20.44______331
The addition of 5 and 15 results 20.46
The addition of 5 and 15 results 20.3____448
The addition of 5 and 15 results 20.49
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
Ghi chú. Các ví dụ sau được định nghĩa bằng cú pháp 1, các bạn thử convert sang cú pháp 2 để thực hành nhé
Python3
The addition of 5 and 15 results 20.51
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.56
The addition of 5 and 15 results 20.57
The addition of 5 and 15 results 20.58
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.51
The addition of 5 and 15 results 20.52
The addition of 5 and 15 results 20.53______336
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.58
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.51
The addition of 5 and 15 results 20.6
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.58
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.59
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression00
The addition of 5 and 15 results 20.53____336
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression03
The addition of 5 and 15 results 20.10
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression05____331
The addition of 5 and 15 results 20.51
The addition of 5 and 15 results 20.10
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression09
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression05
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 expression12
The addition of 5 and 15 results 20.31
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression14
The addition of 5 and 15 results 20.53______555
The addition of 5 and 15 results 20.56
The addition of 5 and 15 results 20.55
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression05
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.36
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression24
The addition of 5 and 15 results 20.36
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression03
The addition of 5 and 15 results 20.53______005____333
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.3____036____037
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
The addition of 5 and 15 results 20.1
Đố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ẻ
Python3
def 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
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.55
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression47
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.59
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression00
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.3____14
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression57
The addition of 5 and 15 results 20.6
The addition of 5 and 15 results 20.10
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression60
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.3____14
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression65
The addition of 5 and 15 results 20.6
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression67
_______068____559____16
_______068____651____16
đầu ra
The addition of 5 and 15 results 20.3
Các loại lập luận
Python 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.
Python3
def 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____331
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
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.3____14
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression84
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression85
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.3____14
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression89
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression90
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____094
The addition of 5 and 15 results 20.6
Đầu ra
The addition of 5 and 15 results 20.3
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ố
Python3
def 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
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.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
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.05
The addition of 5 and 15 results 20.06
The addition of 5 and 15 results 20.31
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
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.08
The addition of 5 and 15 results 20.13
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.05
The addition of 5 and 15 results 20.6
Đầu ra
The addition of 5 and 15 results 20.4
Đối số có độ dài thay đổi
Trong 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
- *args [Đối số không phải từ khóa]
- **kwargs [Đối số từ khóa]
ví dụ 1. Đối số không phải từ khóa có độ dài thay đổi
con trăn
The 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____011____122
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.24
The addition of 5 and 15 results 20.25
The addition of 5 and 15 results 20.57
The addition of 5 and 15 results 20.27
The addition of 5 and 15 results 20.53____13____130
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression93______132____442
The addition of 5 and 15 results 20.34
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.38
The addition of 5 and 15 results 20.6
Đầu ra
The addition of 5 and 15 results 20.5
ví dụ 2. Đối số từ khóa có độ dài thay đổi
Python3
The 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
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.24
The addition of 5 and 15 results 20.49
The addition of 5 and 15 results 20.57
The addition of 5 and 15 results 20.51
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.3____14
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.57
The addition of 5 and 15 results 20.38
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.31______105
The addition of 5 and 15 results 20.62
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.64
The addition of 5 and 15 results 20.65
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.05
The addition of 5 and 15 results 20.6
Đầu ra
The addition of 5 and 15 results 20.5
chuỗi tài liệu
Chuỗ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
The addition of 5 and 15 results 20.5
Ví dụ. Thêm Docstring vào chức năng
Python3
def 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
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.74
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.55
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression47
The addition of 5 and 15 results 20.55
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.59
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression00
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.3____14
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression57
The addition of 5 and 15 results 20.6
The addition of 5 and 15 results 20.10
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression60
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.53
The addition of 5 and 15 results 20.3____14
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression65
The addition of 5 and 15 results 20.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____1100
Đầ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 Python
Câ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
Python3
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.102
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.104
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.106
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.109
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.59
The addition of 5 and 15 results 20.3____1114____559
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression40
The addition of 5 and 15 results 20.3____1114
The addition of 5 and 15 results 20.33
The addition of 5 and 15 results 20.120
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
Python3
The addition of 5 and 15 results 20.122
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.124
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.126
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.128
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.130
The addition of 5 and 15 results 20.131
The addition of 5 and 15 results 20.132
The addition of 5 and 15 results 20.133
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.58
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression94
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.138
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.140
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.142
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.144
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.43
The addition of 5 and 15 results 20.128
The addition of 5 and 15 results 20.148
The addition of 5 and 15 results 20.3____1150
Đầ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
Python3
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.124
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.154
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.156
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.158
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.58
The addition of 5 and 15 results 20.130
The addition of 5 and 15 results 20.42______1165
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.167
The addition of 5 and 15 results 20.128
The addition of 5 and 15 results 20.169
The addition of 5 and 15 results 20.132
The addition of 5 and 15 results 20.133
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.58
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression94
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.138
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.140
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.142
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.144
The addition of 5 and 15 results 20.42
The addition of 5 and 15 results 20.43
The addition of 5 and 15 results 20.128
The addition of 5 and 15 results 20.148
The addition of 5 and 15 results 20.3____1150
Đầ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].
Python3
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.124
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.154
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.156
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.158
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.130
The addition of 5 and 15 results 20.169
The addition of 5 and 15 results 20.132
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.31
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression94
The addition of 5 and 15 results 20.306
The addition of 5 and 15 results 20.3____2308
Đầu ra
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression5
Tập thể dục. Hãy thử đoán đầu ra của đoạn mã sau.
Python3
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.310
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.312
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.318
The addition of 5 and 15 results 20.10
The addition of 5 and 15 results 20.318
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.312
The addition of 5 and 15 results 20.38
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.59
The addition of 5 and 15 results 20.318
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.51
The addition of 5 and 15 results 20.330
The addition of 5 and 15 results 20.3____2308
The addition of 5 and 15 results 20.3____2334
Đầu ra
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression6
Hàm ẩn danh trong Python Hàm
Trong 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
Python3
The addition of 5 and 15 results 20.335
The addition of 5 and 15 results 20.336
The addition of 5 and 15 results 20.0
The addition of 5 and 15 results 20.338
The addition of 5 and 15 results 20.36
The addition of 5 and 15 results 20.160
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression11
The addition of 5 and 15 results 20.160
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression11
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.345
The addition of 5 and 15 results 20.31
The addition of 5 and 15 results 20.347
The addition of 5 and 15 results 20.348
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression11
The addition of 5 and 15 results 20.160
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression11
The addition of 5 and 15 results 20.160
The addition of 5 and 15 results 20.3____2354
The addition of 5 and 15 results 20.355
def function_name[parameter: data_type] -> return_type: """Doctring""" # body of the function return expression40
The addition of 5 and 15 results 20.3____2358
The addition of 5 and 15 results 20.355
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àm
Mộ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