Cải thiện bài viết
Lưu bài viết
Từ khóa Python def được sử dụng để xác định hàm, nó được đặt trước tên hàm được cung cấp bởi người dùng để tạo chức năng do người dùng xác định. Trong Python, một hàm là một đơn vị mã logic chứa một chuỗi các câu lệnh được thụt vào dưới một tên được đưa ra bằng cách sử dụng từ khóa của Def def. Trong từ khóa Python def là từ khóa được sử dụng nhiều nhất. is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword. In python def keyword is the most used keyword.
Syntax:
def function_name: function definition statements...
Sử dụng từ khóa def:
- Trong trường hợp của các lớp, từ khóa DEF được sử dụng để xác định các phương thức của một lớp.
- Từ khóa def cũng được yêu cầu để xác định hàm thành viên đặc biệt của một lớp như __init __ [].
Ứng dụng thực tế có thể là nó cung cấp tính năng của khả năng tái sử dụng mã thay vì viết phần mã hết lần này đến lần khác, chúng ta có thể xác định một hàm và ghi mã bên trong hàm với sự trợ giúp của từ khóa DEF. Nó sẽ rõ ràng hơn trong ví dụ minh họa được đưa ra dưới đây. Có thể có thể có nhiều ứng dụng của def tùy thuộc vào các trường hợp sử dụng. & NBSP;def keyword. It will be more clear in the illustrated example given below. There can possibly be many applications of def depending upon the use cases.
Ví dụ 1: Sử dụng từ khóa def.Use of def keyword.
Trong ví dụ này, chúng tôi sẽ tạo một người dùng xác định một chức năng bằng cách sử dụng từ khóa def.
Python3
def
subNumbers[x, y]:
return
[x
-
____10
subtraction of 90 and 50 is = 401
subtraction of 90 and 50 is = 402
subtraction of 90 and 50 is = 403
subtraction of 90 and 50 is = 404
subtraction of 90 and 50 is = 402
subtraction of 90 and 50 is = 406
subtraction of 90 and 50 is = 407
subtraction of 90 and 50 is = 402
subtraction of 90 and 50 is = 409
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 230
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 231
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 232
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 233
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 234
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 235
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 236
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 237
Output:
subtraction of 90 and 50 is = 40
Ví dụ 2: Người dùng xác định một chức năng với 10 số nguyên tố đầu tiên. & nbsp; User defines a function with first 10 prime numbers.
Python3
def
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 239
The factorial of 6 is 7201
subtraction of 90 and 50 is = 402
The factorial of 6 is 7203
The factorial of 6 is 7205
subtraction of 90 and 50 is = 402
The factorial of 6 is 7207
The factorial of 6 is 7209
def
0def
1def
2 def
3def
4 def
5
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 231
def
7def
8The factorial of 6 is 7203
subNumbers[x, y]:
0subNumbers[x, y]:
1subNumbers[x, y]:
2
The factorial of 6 is 7201
subNumbers[x, y]:
4 def
3subtraction of 90 and 50 is = 402
0
The factorial of 6 is 7201
2subtraction of 90 and 50 is = 402
The factorial of 6 is 7203
def
1
6subNumbers[x, y]:
9
subNumbers[x, y]:
1
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 230
return
0subNumbers[x, y]:
1
The factorial of 6 is 7201
2subtraction of 90 and 50 is = 402
The factorial of 6 is 7203
subNumbers[x, y]:
1
The factorial of 6 is 7205
2subtraction of 90 and 50 is = 402
The factorial of 6 is 7203
[x
1
subtraction of 90 and 50 is = 402
[x
3First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 230
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 231
[x
6[x
7[x
8
Output:
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 23
& nbsp; Ví dụ 3: Người dùng định nghĩa một chức năng với số giai thừa.Example 3: User defines a function with a factorial number.
Python3
def
-
0
subNumbers[x, y]:
2 [x
1
subtraction of 90 and 50 is = 402
subtraction of 90 and 50 is = 402
The factorial of 6 is 7203
subNumbers[x, y]:
9def
1return
subtraction of 90 and 50 is = 4000
6subNumbers[x, y]:
9
Các
subtraction of 90 and 50 is = 4012
subtraction of 90 and 50 is = 402
subtraction of 90 and 50 is = 4014
subNumbers[x, y]:
2
subtraction of 90 and 50 is = 4016
The factorial of 6 is 7207
subNumbers[x, y]:
9
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 230
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 231
subtraction of 90 and 50 is = 4022
[x
7subtraction of 90 and 50 is = 4024
subtraction of 90 and 50 is = 4012
subtraction of 90 and 50 is = 402.
_______
6subNumbers[x, y]:
9
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 230
First 10 prime numbers are: 1 2 3 5 7 11 13 17 19 231
subtraction of 90 and 50 is = 4040
subtraction of 90 and 50 is = 4041
subtraction of 90 and 50 is = 4042
subtraction of 90 and 50 is = 4043
Output:
The factorial of 6 is 720