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.  

cú pháp. Hàm Python

Hàm trong Python là gì với ví dụ

 

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 expression
9

 

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  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 expression
9

The addition of 5 and 15 results 20.
0
The addition of 5 and 15 results 20.
1

False True
0_______1_______3
The addition of 5 and 15 results 20.
4
The addition of 5 and 15 results 20.
5_______1_______6

 

 

False True
5

False True
6

đầ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

Python3




The addition of 5 and 15 results 20.
0
False True
8
False True
9
even
odd
0
False True
9
The addition of 5 and 15 results 20.
6
even
odd
3
even
odd
4
False True
9
even
odd
6

False True
0_______23_______8

False True
0_______33_______0
x:  10
y:  50
1
x:  10
y:  50
2
x:  10
y:  50
3
x:  10
y:  50
4

 

False True
0____33_______6
x:  10
y:  50
0

 

x:  10
y:  50
8

x:  10
y:  50
9_______33_______1
Geeks Practice
Geeks Practice
1
Geeks Practice
Geeks Practice
2
Geeks Practice
Geeks Practice
3

Geeks Practice
Geeks Practice
4
x:  10
y:  50
1
Geeks Practice
Geeks Practice
6

The addition of 5 and 15 results 20.
3_______44_______8
Geeks Practice
Geeks Practice
9
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




Hello
Welcome
to
GeeksforGeeks
1

The addition of 5 and 15 results 20.
0
Hello
Welcome
to
GeeksforGeeks
3

False True
0_______55_______5
Hello
Welcome
to
GeeksforGeeks
6
Hello
Welcome
to
GeeksforGeeks
7
Hello
Welcome
to
GeeksforGeeks
8
Hello
Welcome
to
GeeksforGeeks
9
Geeks Practice
Geeks Practice
2
first == Geeks
mid == for
last == Geeks
1
first == Geeks
mid == for
last == Geeks
2

first == Geeks
mid == for
last == Geeks
3
x:  10
y:  50
6
first == Geeks
mid == for
last == Geeks
5

False True
0_______55_______5
first == Geeks
mid == for
last == Geeks
8
x:  10
y:  50
1
x:  10
y:  50
1
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 == Geeks
8
Syntax: print(function_name.__doc__)
5
Hello
Welcome
to
GeeksforGeeks
9
x:  10
y:  50
1
x:  10
y:  50
1
Syntax: print(function_name.__doc__)
9
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
00

first == Geeks
mid == for
last == Geeks
3
x:  10
y:  50
6
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
03

False True
0_______0_______05
x:  10
y:  50
1
first == Geeks
mid == for
last == Geeks
1

False True
0_______0_______09
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
05_______0_______11
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
12
x:  10
y:  50
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
14

first == Geeks
mid == for
last == Geeks
3
Hello
Welcome
to
GeeksforGeeks
5
Hello
Welcome
to
GeeksforGeeks
6
Syntax: print(function_name.__doc__)
5
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
05
x:  10
y:  50
1
x:  10
y:  50
1
Syntax: print(function_name.__doc__)
9
even
odd
6

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
24
x:  10
y:  50
6
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
03

first == Geeks
mid == for
last == Geeks
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
05
x:  10
y:  50
3
x:  10
y:  50
1
Hello
Welcome
to
GeeksforGeeks
9

False True
0_______33_______6
first == Geeks
mid == for
last == Geeks
5

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 expression
37
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
38
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
39
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
40

đầ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ẻ

Python3




 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
41

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
42

 

 

The addition of 5 and 15 results 20.
0
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
44

False True
0_______55_______5
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
47
Syntax: print(function_name.__doc__)
5
Hello
Welcome
to
GeeksforGeeks
9
x:  10
y:  50
1
x:  10
y:  50
1
Syntax: print(function_name.__doc__)
9
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
00

first == Geeks
mid == for
last == Geeks
3
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 expression
57_______1_______6

False True
0_______0_______60
even
odd
6

first == Geeks
mid == for
last == Geeks
3
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 expression
65_______1_______6

 

 

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
67

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
68
Hello
Welcome
to
GeeksforGeeks
9
The addition of 5 and 15 results 20.
6

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
68
first == Geeks
mid == for
last == Geeks
1
The addition of 5 and 15 results 20.
6

đầu ra

even
odd

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 expression
74

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
75

 

 

The addition of 5 and 15 results 20.
0
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
77
x:  10
y:  50
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
79
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
00

False True
0_______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 expression
84_______0_______85

False True
0_______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 expression
89_______0_______90

 

 

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
91

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
92

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
93_______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ố

Python3




 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
96

The addition of 5 and 15 results 20.
0
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
98

False True
0_______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:  50
1
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:  50
1
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 đổ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 expression
93
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
The addition of 5 and 15 results 20.
22

False True
0_______1_______24
The addition of 5 and 15 results 20.
25
Hello
Welcome
to
GeeksforGeeks
7
The addition of 5 and 15 results 20.
27

first == Geeks
mid == for
last == Geeks
3
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 expression
93_______1_______32
Geeks Practice
Geeks Practice
2
The addition of 5 and 15 results 20.
34
Geeks Practice
Geeks Practice
2
The addition of 5 and 15 results 20.
36
Geeks Practice
Geeks Practice
2
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

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 expression
93
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
The addition of 5 and 15 results 20.
46

False True
0_______1_______24
The addition of 5 and 15 results 20.
49
Hello
Welcome
to
GeeksforGeeks
7
The addition of 5 and 15 results 20.
51

first == Geeks
mid == for
last == Geeks
3
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:  50
8

The addition of 5 and 15 results 20.
59
x:  10
y:  50
1
The addition of 5 and 15 results 20.
05
The addition of 5 and 15 results 20.
62
x:  10
y:  50
1
The addition of 5 and 15 results 20.
64
The addition of 5 and 15 results 20.
65
x:  10
y:  50
1
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ệ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

Syntax: print(function_name.__doc__)

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 expression
41

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
42

 

 

The addition of 5 and 15 results 20.
0
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
44

False True
0____1_______74

False True
0

False True
0_______55_______5
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
47
Syntax: print(function_name.__doc__)
5
Hello
Welcome
to
GeeksforGeeks
9
x:  10
y:  50
1
x:  10
y:  50
1
Syntax: print(function_name.__doc__)
9
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
00

first == Geeks
mid == for
last == Geeks
3
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 expression
57_______1_______6

False True
0_______0_______60
even
odd
6

first == Geeks
mid == for
last == Geeks
3
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 expression
65_______1_______6

 

 

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
67

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 expression
0

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 expression
1

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
False True
02

False True
0____11_______04

False True
0_______11_______06

False True
0_______33_______6
False True
09
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
Hello
Welcome
to
GeeksforGeeks
9

 

 

The addition of 5 and 15 results 20.
3_______11_______14
Hello
Welcome
to
GeeksforGeeks
9
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
40

The addition of 5 and 15 results 20.
3_______11_______14
even
odd
3
False True
20
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
40

Đầu ra

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
2

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




False True
22

The addition of 5 and 15 results 20.
0
False True
24

False True
0_______11_______26
Syntax: print(function_name.__doc__)
9
False True
28
x:  10
y:  50
1
False True
30

 

 

False True
31

False True
32

False True
33
x:  10
y:  50
1
Hello
Welcome
to
GeeksforGeeks
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
94
Geeks Practice
Geeks Practice
2
False True
38
Geeks Practice
Geeks Practice
2
False True
40
Geeks Practice
Geeks Practice
2
False True
42
Geeks Practice
Geeks Practice
2
False True
44
Geeks Practice
Geeks Practice
2
Geeks Practice
Geeks Practice
3
False True
28

False True
48

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 expression
3

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
False True
24

 

False True
0_______11_______54

False True
0____11_______56

False True
0____11_______58

False True
0_______11_______60
x:  10
y:  50
1
Hello
Welcome
to
GeeksforGeeks
8_______11_______30
Geeks Practice
Geeks Practice
2
False True
65
Geeks Practice
Geeks Practice
2
False True
67
False True
28

 

 

False True
69

False True
32

False True
33
x:  10
y:  50
1
Hello
Welcome
to
GeeksforGeeks
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
94
Geeks Practice
Geeks Practice
2
False True
38
Geeks Practice
Geeks Practice
2
False True
40
Geeks Practice
Geeks Practice
2
False True
42
Geeks Practice
Geeks Practice
2
False True
44
Geeks Practice
Geeks Practice
2
Geeks Practice
Geeks Practice
3
False True
28

False True
48

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 expression
4

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
False True
24

 

False True
0_______11_______54

False True
0____11_______56

False True
0____11_______58

False True
0_______11_______60
x:  10
y:  50
1
False True
30

 

 

False True
69

False True
32

False True
60_______33_______1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
94

even
odd
06

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 expression
5

Bài tập. Hãy thử đoán đầu ra của đoạn mã sau.  

Python3




The addition of 5 and 15 results 20.
0
even
odd
10

False True
0_______23_______12
x:  10
y:  50
1
False True
60

False True
0_______11_______60
x:  10
y:  50
1
even
odd
18

False True
0_______23_______18____33_______1
even
odd
12

 

 

x:  10
y:  50
8

False True
60_______33_______1
Hello
Welcome
to
GeeksforGeeks
9

even
odd
18_______33_______1
first == Geeks
mid == for
last == Geeks
1

even
odd
30

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 expression
6

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




even
odd
35

even
odd
36

 

 

The addition of 5 and 15 results 20.
0
even
odd
38
x:  10
y:  50
6
False True
60
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
False True
60
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
False True
60

 

even
odd
45_______33_______1
even
odd
47
even
odd
48
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
False True
60
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
11
False True
60

 

The addition of 5 and 15 results 20.
3_______23_______54
even
odd
55
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
40

The addition of 5 and 15 results 20.
3_______23_______58
even
odd
55
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
40

Đầu ra

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7

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

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() .