đã đăng. 2019-09-16 / Thẻ. con trăn
tiếng riu ríu
Trong Python, bạn có thể tính thương với
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
3 và phần còn lại với answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
4q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
nguồn. kiểm tra divmod. py
Hàm dựng sẵn
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
5 rất hữu ích khi bạn muốn cả thương và số dư- Hàm tích hợp - divmod[] — Python 3. 7. 4 tài liệu
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6 trả về một tuple answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7Bạn có thể giải nén và gán cho từng biến
- Giải nén một bộ và liệt kê trong Python
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
nguồn. kiểm tra divmod. py
Tất nhiên, bạn có thể nhận nó dưới dạng một tuple
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
nguồn. kiểm tra divmod. py
Liên kết được tài trợ
Đăng lại
thể loại liên quan
- con trăn
Những bài viết liên quan
- gấu trúc. Lặp lại DataFrame với vòng lặp "for"
- Định nghĩa và gọi hàm trong Python [def, return]
- Trăn, Gối. Xoay hình ảnh
- Kiểm tra và thêm đường dẫn tìm kiếm mô-đun với sys. đường dẫn trong Python
- Kiểm tra xem một chuỗi là số, chữ cái, chữ số hay ASCII
- Quay video từ máy ảnh/tệp bằng OpenCV trong Python
- Thêm phần đệm vào hình ảnh với Python, Gối
- "inf" cho vô cực trong Python
- Khởi tạo danh sách với kích thước và giá trị đã cho trong Python
- Cách trả về nhiều giá trị từ một hàm trong Python
- Kiểm tra xem các số dấu phẩy động có gần bằng Python không [math. isclose]
- NumPy. Xoay mảng [np. thối90]
- Cách sử dụng tham số chính trong Python [được sắp xếp, tối đa, v.v. ]
- gấu trúc. Chuyển đổi khung dữ liệu [hoán đổi hàng và cột]
- Định dạng chuỗi và số bằng format[] trong Python
❮ Phương pháp toán học
Thí dụ
Trả về phần còn lại của x đối với y
# Nhập thư viện toán
nhập toán
# Trả về phần còn lại của x/y
print [math. dư[9, 2]]
in [toán. dư[9, 3]]
in [toán. phần còn lại[18, 4]]
Định nghĩa và cách sử dụng
Phương thức
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
8 trả về phần còn lại của x đối với ycú pháp
Giá trị tham số
Tham sốMô tảxBắt buộc. Số bạn muốn chia. yBắt buộc. Số bạn muốn chia với. Nó phải là một số khác không hoặc xảy ra ValueErrorchi tiết kỹ thuật
Giá trị trả về. Giá trịanswer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
9, đại diện cho Phiên bản Python còn lại. 3. 7Thêm ví dụ
Thí dụ
Trả về phần còn lại của x/y
in [toán. phần còn lại [23. 5, 5]]
in [toán. phần còn lại [23, 5. 5]]
in [toán. số dư [12. 5, 2. 5]]
in [toán. phần còn lại[12, 2]]
❮ Phương pháp toán học
Cho hai số n và m. Nhiệm vụ là tìm thương và số dư của hai số bằng cách chia n cho m
ví dụ
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4
Phương pháp 1. Cách tiếp cận ngây thơ
Cách tiếp cận ngây thơ là tìm thương bằng cách sử dụng toán tử chia đôi [//] và phần còn lại sử dụng toán tử mô đun [%]
Thí dụ
Python3
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 40
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 41
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 42
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 43
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 46
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
Quotient: 3 Remainder 1 Quotient: 19 Remainder 42
Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
Quotient: 3 Remainder 1 Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 46
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 48____49
Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 44
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 46______42
Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
Quotient: 3 Remainder 1 Quotient: 19 Remainder 46
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 48____49
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 404
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 405
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 44
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 407
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 408
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 409____310
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 411
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 412
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 408
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 414
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 410
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 416
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 412
đầu ra
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
Thời gian phức tạp. Ô[1]
Không gian phụ trợ. Ô[1]
Phương pháp 2. Sử dụng phương thức divmod[]
Phương thức Divmod[] lấy hai số làm tham số và trả về bộ chứa cả thương và số dư