Đối số hoặc đối số trong Python là gì?

Bài viết này giải thích các đối số chức năng khác nhau của Python với các ví dụ rõ ràng về cách sử dụng chúng. Nhưng trước khi tìm hiểu chi tiết tất cả các đối số của hàm, trước tiên, hãy hiểu cách sử dụng đối số hoặc tham số trong hàm

Cũng thấy

  • Bài tập hàm Python
  • Trắc nghiệm hàm Python

Mục lục

Đối số chức năng là gì?

Khi chúng ta định nghĩa và gọi hàm Python, thuật ngữ tham số và đối số được sử dụng để truyền thông tin cho hàm

  • tham số. Nó là biến được liệt kê bên trong dấu ngoặc đơn trong định nghĩa hàm
  • tranh luận. Nó là một giá trị được gửi đến hàm khi nó được gọi. Đó là dữ liệu mà hàm thực hiện một số hành động và trả về kết quả

Thí dụ

Trong ví dụ này, hàm

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
2 được xác định với ba tham số,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
3,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
4,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
5 và in tổng của cả ba giá trị của các đối số được truyền trong khi gọi hàm

# a, b, c are arguments of the function
def my_sum(a, b, c):
    s = a + b + c
    return s

print('Total is:', my_sum(30, 40, 50))

đầu ra

Total is: 120

Đối số hoặc đối số trong Python là gì?
Đối số hoặc đối số trong Python là gì?
đối số hàm và tham số

Ghi chú. Hàm phải được gọi với số lượng đối số chính xác theo mặc định. Ví dụ, hàm trên cần 3 đối số, vì vậy bạn phải gọi hàm

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
6 với 3 đối số;

Chức năng có cần đối số không?

Không bắt buộc phải sử dụng đối số trong định nghĩa hàm. Nhưng nếu bạn cần xử lý dữ liệu người dùng, bạn cần có các đối số trong định nghĩa hàm để chấp nhận dữ liệu đó

Ngoài ra, chúng tôi sử dụng đối số trong định nghĩa hàm khi chúng tôi cần thực hiện cùng một tác vụ nhiều lần với các dữ liệu khác nhau

Có thể gọi hàm mà không có đối số không?

Nếu hàm được xác định bằng các tham số, các đối số được truyền vào phải khớp với một trong các đối số mà hàm chấp nhận khi gọi

Các loại đối số chức năng

Có nhiều cách khác nhau để sử dụng các đối số trong một hàm. Trong Python, chúng ta có 4 loại đối số hàm sau

  1. Đối số mặc định
  2. Đối số từ khóa (đối số được đặt tên)
  3. đối số vị trí
  4. Đối số tùy ý (đối số có độ dài thay đổi
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    7 và
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    8)

Đối số hoặc đối số trong Python là gì?
Đối số hoặc đối số trong Python là gì?
Các loại đối số hàm Python được giải thích bằng các ví dụ

Đối số mặc định

Trong một hàm, các đối số có thể có các giá trị mặc định. Chúng tôi gán các giá trị mặc định cho đối số bằng cách sử dụng toán tử '=' (gán) tại thời điểm định nghĩa hàm. Bạn có thể định nghĩa một hàm với bất kỳ số lượng đối số mặc định nào

Giá trị mặc định của một đối số sẽ được sử dụng bên trong một hàm nếu chúng ta không truyền giá trị cho đối số đó tại thời điểm gọi hàm. Do đó, các đối số mặc định trở thành tùy chọn trong khi gọi hàm

Nó ghi đè giá trị mặc định nếu chúng ta cung cấp một giá trị cho các đối số mặc định trong khi gọi hàm

Hãy để chúng tôi hiểu điều này với một ví dụ

Thí dụ

Hãy định nghĩa một hàm

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
9 với bốn đối số
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
00,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
01,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
02 và
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
03. Trong hàm này,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
02 và
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
03 là các đối số mặc định có giá trị mặc định

  • Nếu bạn gọi một hàm không có đối số
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    03 và
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    02 thì giá trị mặc định của
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    02 và
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    03 sẽ được sử dụng
  • Các tham số
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    01 và
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    00 không có giá trị mặc định và được yêu cầu (bắt buộc) trong khi gọi hàm
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School

Truyền một trong các đối số mặc định

Nếu bạn chuyển các giá trị của đối số

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
02 và
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
03 trong khi gọi một hàm thì các giá trị đó sẽ được sử dụng thay cho giá trị mặc định

Thí dụ

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
0

đầu ra

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
6

Đối số từ khóa

Thông thường, tại thời điểm gọi hàm, các giá trị được gán cho các đối số theo vị trí của chúng. Vì vậy, chúng ta phải chuyển các giá trị theo cùng một trình tự được xác định trong định nghĩa hàm

Ví dụ: khi chúng ta gọi

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
64, giá trị “Jon” được gán cho tên đối số và tương tự, 12 đến
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
01, v.v.

Chúng ta có thể thay đổi hành vi này bằng cách sử dụng đối số từ khóa

Đối số từ khóa là những đối số trong đó giá trị được gán cho đối số theo từ khóa (tên) của chúng khi hàm được gọi. Trước nó là tên biến và toán tử gán (

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
66). Keyword Argument còn được gọi là đối số được đặt tên

Thí dụ

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
0

đầu ra

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
1

Thay đổi trình tự của các đối số từ khóa

Ngoài ra, bạn có thể thay đổi trình tự của các đối số từ khóa bằng cách sử dụng tên của chúng trong lệnh gọi hàm

Python cho phép các chức năng được gọi bằng cách sử dụng đối số từ khóa. Nhưng tất cả các đối số từ khóa phải khớp với các tham số trong định nghĩa hàm. Khi chúng ta gọi hàm theo cách này, thứ tự (vị trí) của các đối số có thể bị thay đổi

Thí dụ

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
2

Đối số vị trí

Đối số vị trí là những đối số mà các giá trị được gán cho các đối số theo vị trí của chúng khi hàm được gọi. Ví dụ: đối số vị trí đầu tiên phải là số 1 khi hàm được gọi. Đối số vị trí thứ 2 cần phải là thứ 2 khi hàm được gọi, v.v.

Theo mặc định, các hàm Python được gọi bằng các đối số vị trí

Thí dụ. Chương trình trừ 2 số bằng đối số vị trí

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
3

Ghi chú. Nếu bạn cố gắng chuyển nhiều đối số hơn, bạn sẽ gặp lỗi

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
4

đầu ra

Total is: 120
0

Ghi chú. Trong số đối số vị trí và vị trí của đối số phải được khớp. Nếu chúng ta thay đổi thứ tự, thì kết quả có thể thay đổi. Ngoài ra, nếu chúng tôi thay đổi số lượng đối số, chúng tôi sẽ gặp lỗi

Những điểm quan trọng cần nhớ về đối số chức năng

Điểm 1. Các đối số mặc định phải tuân theo các đối số không mặc định

Thí dụ

Total is: 120
1

Điểm. Đối số mặc định phải tuân theo đối số mặc định trong định nghĩa hàm

Đối số mặc định phải tuân theo đối số mặc định. Ví dụ: Khi bạn sử dụng đối số mặc định trong một định nghĩa, tất cả các đối số ở bên phải của chúng cũng phải có giá trị mặc định. Nếu không, bạn sẽ gặp lỗi

Thí dụ

Total is: 120
2

điểm 2. đối số từ khóa chỉ nên theo đối số vị trí

chúng ta có thể trộn các đối số vị trí với các đối số từ khóa trong khi gọi hàm. Tuy nhiên, đối số từ khóa phải luôn ở sau đối số không phải từ khóa (đối số vị trí). Nếu không, bạn sẽ gặp lỗi

I. e. , tránh sử dụng đối số từ khóa trước đối số vị trí

Thí dụ

Total is: 120
3

Điểm 3. Thứ tự của các đối số từ khóa không quan trọng, nhưng tất cả các đối số từ khóa được truyền phải khớp với một trong các đối số được hàm chấp nhận

Thí dụ

Total is: 120
4

Điểm 4. Không có đối số sẽ nhận được một giá trị nhiều hơn một lần

Total is: 120
5

Đối số có độ dài thay đổi

Trong Python đôi khi có trường hợp chúng ta cần truyền nhiều đối số cho hàm. Các loại đối số như vậy được gọi là đối số tùy ý hoặc đối số có độ dài thay đổi

Chúng tôi sử dụng các đối số có độ dài thay đổi nếu chúng tôi không biết trước số lượng đối số cần thiết cho hàm

Các loại đối số tùy ý

  • đối số vị trí tùy ý (
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    7)
  • đối số từ khóa tùy ý (
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    8)

*args và **kwargs cho phép bạn chuyển nhiều đối số vị trí hoặc đối số từ khóa cho một hàm

Đối số vị trí tùy ý (# function with 2 keyword arguments grade and school def student(name, age, grade="Five", school="ABC School"): print('Student Details:', name, age, grade, school) # without passing grade and school # Passing only the mandatory arguments student('Jon', 12) # Output: Student Details: Jon 12 Five ABC School7)

Chúng ta có thể khai báo một đối số có độ dài thay đổi bằng ký hiệu

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
00 (dấu hoa thị). Đặt dấu hoa thị (
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
00) trước một tham số trong định nghĩa hàm để xác định một đối số vị trí tùy ý

chúng ta có thể truyền nhiều đối số cho hàm. Bên trong, tất cả các giá trị này được biểu diễn dưới dạng một bộ. Hãy hiểu việc sử dụng các đối số có độ dài thay đổi bằng một ví dụ

Đây là một hàm đơn giản nhận ba đối số và trả về giá trị trung bình của chúng

Total is: 120
6

Chức năng này hoạt động, nhưng nó chỉ giới hạn ở ba đối số. Điều gì sẽ xảy ra nếu bạn cần tính điểm trung bình của hơn ba môn học hoặc số lượng môn học chỉ được xác định trong thời gian chạy?

Thí dụ

Total is: 120
7

đầu ra

Total is: 120
8

Ghi chú. args chỉ là một cái tên. Bạn có thể chọn bất kỳ tên nào bạn thích, chẳng hạn như *chủ đề

Total is: 120
9

Đối số từ khóa tùy ý (**kwargs)

Chúng tôi đã thấy cách sử dụng

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
7. Bây giờ hãy xem cách sử dụng đối số
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
8. **kwargs cho phép bạn truyền nhiều đối số từ khóa cho một hàm. Sử dụng
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
8 nếu bạn muốn xử lý các đối số được đặt tên trong một hàm

Sử dụng toán tử giải nén (

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
05) để xác định các đối số từ khóa có độ dài thay đổi. Các đối số từ khóa được truyền tới một kwargs được truy cập bằng cặp khóa-giá trị (giống như truy cập từ điển trong Python)

Đối số trong Python là gì?

Tham số cụm từ và đối số có thể được sử dụng cho cùng một nội dung. thông tin được truyền vào một chức năng. Từ quan điểm của một chức năng. Tham số là biến được liệt kê bên trong dấu ngoặc đơn trong định nghĩa hàm. Đối số là giá trị được gửi đến hàm khi nó được gọi .

3 loại đối số trong Python là gì?

Do đó, chúng tôi kết luận rằng Đối số hàm Python và ba loại đối số hàm của nó. Đây là- các đối số mặc định, từ khóa và tùy ý .

Đối số trong mã là gì?

Đối số là một cách để bạn cung cấp thêm thông tin cho một hàm . Sau đó, hàm có thể sử dụng thông tin đó khi nó chạy, giống như một biến. Nói cách khác, khi bạn tạo một hàm, bạn có thể truyền dữ liệu dưới dạng đối số, hay còn gọi là tham số.