Practice mode Python

What is the output of the following Python code? print(-7 // 2, -7 % 2)

Correct answer B. -4 1

Explanation

The // operator floors the result towards negative infinity, so -3.5 becomes -4, not -3 as it would with truncation in C or Java. Python keeps the identity (a // b) * b + (a % b) == a, so the remainder must be -7 - (-8) = 1. The result of % always takes the sign of the divisor, which is positive here.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.