Trong Python, bạn sử dụng toán tử dấu gạch chéo kép
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 để thực hiện phép chia tầng. Toán tử num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 này chia số thứ nhất cho số thứ hai và làm tròn kết quả xuống số nguyên [hoặc số nguyên] gần nhấtTrong bài viết này, tôi sẽ chỉ cho bạn cách sử dụng toán tử
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 và so sánh nó với phép chia thông thường để bạn có thể thấy nó hoạt động như thế nàoTuy nhiên, nó không kết thúc ở đó – bạn cũng sẽ tìm hiểu về một phương thức toán học Python đồng nghĩa với toán tử dấu gạch chéo kép
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7Những gì chúng tôi sẽ bao gồm
- Cú pháp cơ bản của toán tử
7num1 = 12 num2 = 5 num3 = num1 // num2 print["floor division of", num1, "by", num2, "=", num3] # Output: floor division of 12 by 5 = 2
- Ví dụ về phân chia tầng
- Dấu gạch chéo kép
7 Toán tử hoạt động nhưnum1 = 12 num2 = 5 num3 = num1 // num2 print["floor division of", num1, "by", num2, "=", num3] # Output: floor division of 12 by 5 = 2
3num1 = 12 num2 = 5 num3 = num1 // num2 print["floor division of", num1, "by", num2, "=", num3] # Output: floor division of 12 by 5 = 2
- Người vận hành
7 Double Slash hoạt động như thế nào ở hậu trườngnum1 = 12 num2 = 5 num3 = num1 // num2 print["floor division of", num1, "by", num2, "=", num3] # Output: floor division of 12 by 5 = 2
- Sự kết luận
Cú pháp cơ bản của toán tử num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
Để sử dụng toán tử dấu gạch chéo kép
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7, bạn làm những việc gần giống như trong phép chia thông thường. Sự khác biệt duy nhất là thay vì một dấu gạch chéo num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7, bạn sử dụng dấu gạch chéo kép num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7firstNum // secondNum
Ví dụ về phân chia tầng
Trong ví dụ bên dưới, phép chia sàn của 12 cho 5 dẫn đến 2
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
Trong khi đó, phép chia thông thường của 12 cho 5 sẽ bằng 2. 4. Tức là 2 dư 4
num2 = 5
num3 = num1 / num2
print["normal division of", num1, "by", num2, "=", num3]
# Output: normal division of 12 by 5 = 2.4
Điều này cho bạn thấy rằng toán tử
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 làm tròn kết quả của phép chia hai số thành số nguyên gần nhấtNgay cả khi dấu thập phân là 9, toán tử
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 vẫn sẽ làm tròn kết quả xuống số nguyên gần nhấtnum1 = 29
num2 = 10
num3 = num1 / num2
num4 = num1 // num2
print["normal division of", num1, "by", num2, "=", num3]
print["but floor division of", num1, "by", num2, "=", num4]
"""
Output:
normal division of 29 by 10 = 2.9
but floor division of 29 by 10 = 2
"""
Và nếu thực hiện phép chia sàn với số âm thì kết quả vẫn được làm tròn xuống
Để chuẩn bị tinh thần cho kết quả, làm tròn xuống một số âm có nghĩa là đi xa khỏi 0. Vì vậy, -12 chia cho 5 kết quả là -3. Đừng nhầm lẫn – mặc dù thoạt nhìn có vẻ như con số đang trở nên "lớn hơn", nhưng nó thực sự đang nhỏ đi [xa hơn từ 0/số âm lớn hơn]
num1 = -12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# floor division of -12 by 5 = -3
Dấu gạch chéo kép num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 Toán tử hoạt động như num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
3
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
Trong Python,
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
3 làm tròn một số xuống số nguyên gần nhất, giống như toán tử dấu gạch chéo kép num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 thực hiệnVì vậy,
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
3 là một thay thế cho toán tử num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 bởi vì họ làm điều tương tự đằng sau hậu trườngĐây là một ví dụ
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
0Bạn có thể thấy rằng
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
3 làm điều tương tự như toán tử num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7Người vận hành num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 Double Slash hoạt động như thế nào ở hậu trường
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
Khi bạn sử dụng toán tử
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 để chia hai số, phương thức được gọi đằng sau hậu trường là num1 = 29
num2 = 10
num3 = num1 / num2
num4 = num1 // num2
print["normal division of", num1, "by", num2, "=", num3]
print["but floor division of", num1, "by", num2, "=", num4]
"""
Output:
normal division of 29 by 10 = 2.9
but floor division of 29 by 10 = 2
"""
1Bạn cũng có thể sử dụng trực tiếp phương thức
num1 = 29
num2 = 10
num3 = num1 / num2
num4 = num1 // num2
print["normal division of", num1, "by", num2, "=", num3]
print["but floor division of", num1, "by", num2, "=", num4]
"""
Output:
normal division of 29 by 10 = 2.9
but floor division of 29 by 10 = 2
"""
1 này thay cho toán tử num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
4Sự kết luận
Trong bài viết này, bạn đã học được cách bạn có thể sử dụng toán tử dấu gạch chéo kép
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 và cách nó hoạt động đằng sau hậu trườngNgoài ra, bạn đã học về hai phương án của toán tử
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 – phương pháp num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
3 và phương pháp num1 = 29
num2 = 10
num3 = num1 / num2
num4 = num1 // num2
print["normal division of", num1, "by", num2, "=", num3]
print["but floor division of", num1, "by", num2, "=", num4]
"""
Output:
normal division of 29 by 10 = 2.9
but floor division of 29 by 10 = 2
"""
1Đừng bối rối về việc sử dụng cái nào. Ba cách bạn có thể thực hiện phân chia tầng hoạt động theo cùng một cách. Nhưng tôi khuyên bạn nên sử dụng toán tử dấu gạch chéo kép
num1 = 12
num2 = 5
num3 = num1 // num2
print["floor division of", num1, "by", num2, "=", num3]
# Output: floor division of 12 by 5 = 2
7 vì bạn có thể gõ ít hơn với nóCảm ơn bạn đã đọc
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Nhà phát triển web và nhà văn kỹ thuật tập trung vào các công nghệ giao diện người dùng
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu