❮ Chức năng tích hợp sẵn
Thí dụ
Hiển thị thương số và phần còn lại của 5 chia cho 2:
x = divmod (5, 2)
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Hàm divmod() trả về một tuple chứa thương số & nbsp; và phần còn lại khi đối số1 (cổ tức) được chia cho đối số2 (chia).
Cú pháp
Divmod (Cổ tức, Cổ thích)
Giá trị tham số
cổ tức | Một số. Số bạn muốn chia |
Ưu điểm | Một số. Số bạn muốn chia với |
❮ Chức năng tích hợp sẵn
Phương thức divMod () trong Python lấy hai số và trả về một cặp số bao gồm thương số và phần còn lại của chúng. & NBSP;
Cú pháp: & nbsp;
divmod(x, y) x and y : x is numerator and y is denominator x and y must be non complexExamples:
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)Giải thích: Phương thức divmod () lấy hai tham số x và y, trong đó x được coi là tử số và y được coi là mẫu số. Phương thức tính toán cả x // y và x % y và trả về cả hai giá trị. & Nbsp;The divmod() method takes two parameters x and y, where x is treated as numerator and y is treated as the denominator. The method calculates both x // y and x % y and returns both the values.
- Nếu x và y là số nguyên, giá trị trả về là
- Nếu x hoặc y là một chiếc phao, kết quả là
Python3
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)2Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1(x // y, x % y)2Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1(x // y, x % y)6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3(x // y, x % y)8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1(q, x % y), where q is the whole part of the quotient.2Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1(q, x % y), where q is the whole part of the quotient.6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3(q, x % y), where q is the whole part of the quotient.6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Các
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : n = 7 Output :Prime Input : n = 15 Output :Not Prime2Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : n = 7 Output :Prime Input : n = 15 Output :Not Prime6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : n = 7 Output :Prime Input : n = 15 Output :Not Prime8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Not Prime2Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : n = 7 Output :Prime Input : n = 15 Output :Not Prime8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : n = 7 Output :Prime Input : n = 15 Output :Not Prime6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1142Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1146Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3148Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1231 2Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1231 6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)3231 8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)9
Output:
(5, 4) = (1, 1) (10, 16) = (0, 10) (11, 11) = (1, 0) (15, 13) = (1, 2) (6.0, 5) = (2.0, 2.0) (3, 9.0) = (0.0, 3.0) (13.5, 6.2) = (3.0, 0.0) (1.6, 10.7) = (5.0, 0.10000000000000009)Lỗi và ngoại lệ & nbsp;
- Nếu một trong hai đối số (giả sử x và y), là một chiếc phao, kết quả là (q, x%y). Ở đây, Q là toàn bộ phần của thương số.
- Nếu đối số thứ hai là 0, nó sẽ trả về lỗi phân chia không
- Nếu đối số đầu tiên là 0, nó sẽ trả về (0, 0)
Ứng dụng thực tế: Kiểm tra xem một số là số nguyên tố hoặc không sử dụng hàm divmod (). & Nbsp; & nbsp;Check if a number is prime or not using divmod() function.
Examples:
Input : n = 7 Output :Prime Input : n = 15 Output :Not PrimeAlgorithm
- Khởi tạo một biến mới, giả sử x với số nguyên đã cho và bộ đếm biến thành 0
- Chạy một vòng lặp cho đến khi số nguyên đã cho trở thành 0 và tiếp tục giảm nó.
- Lưu giá trị được trả về bởi divmod (n, x) trong hai biến, nói p và q
- Kiểm tra xem q là 0, điều này sẽ ngụ ý rằng N hoàn toàn chia hết cho x và do đó tăng giá trị bộ đếm
- Kiểm tra xem giá trị bộ đếm lớn hơn 2, nếu có, số không phải là số nguyên tố, nếu không thì đó là số nguyên tố
PYTHON3
divmod()0divmod()1 (5, 4) = (1, 1) (10, 16) = (0, 10) (11, 11) = (1, 0) (15, 13) = (1, 2) (6.0, 5) = (2.0, 2.0) (3, 9.0) = (0.0, 3.0) (13.5, 6.2) = (3.0, 0.0) (1.6, 10.7) = (5.0, 0.10000000000000009)6
divmod()3divmod()1 divmod()5
divmod()6divmod()1 divmod()8
divmod()9 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)00divmod()1 divmod()8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)03
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)05divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)08
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04divmod()3Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)11divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)13
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)15 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)16divmod()1divmod()1 divmod()8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)03
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)21divmod()6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)23divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)13
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)15 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)27Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)28Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)03
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)33Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)34
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)35Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)03
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)1Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)40Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)34
Output:
Not PrimeNhiều ứng dụng hơn: & nbsp;
Ví dụ 1: & nbsp;
Python3
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)42divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)44
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)45divmod()1 divmod()8
divmod()9 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)49divmod()1 divmod()8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)03
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)54divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)57(x // y, x % y)6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)34
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)61divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)63Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)13Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)65
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)45divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)45Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)23 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)71
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)42divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)63divmod()8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)65
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)79
Output:
14Ví dụ 2:
Python3
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)42divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)82
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)83divmod()1 divmod()8
divmod()9 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)49divmod()1 divmod()8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)03
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)54divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)4Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)57(x // y, x % y)6Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)34
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)61divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)63Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)13Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)65
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)45divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)45Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)23 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)71
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)04Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)42divmod()1 Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)63divmod()8Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)65
Input : x = 9, y = 3 Output :(3, 0) Input : x = 8, y = 3 Output :(2, 2)0(x // y, x % y)19
Output:
231