How to calculate this7%(5//2)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to calculate this7%(5//2)?

Python code

1st Jan 2020, 5:37 PM
Deepanshu Verma
Deepanshu Verma - avatar
6 Answers
+ 2
We first calculate the code inside the parentheses, 5 // 2. This returns 2(because 5 // 2 is equal to 2 with a remainder of 1). After doing this, we can get the modular value of 7 % 2, which returns the remainder of 7 / 2. This means the answer is 1, as 7 / 2 is 3 with a remainder of 1.
1st Jan 2020, 5:41 PM
Jianmin Chen
Jianmin Chen - avatar
+ 2
Deepanshu Verma 7%2=1 because the result of a % b must always be less than b, but non-negative. The result must also be a - n * b, (n is any whole number) There always exists only 1 possibility for a - n * b, which is less than b, but non-negative. We'll get an equation: 2 > 7 % 2 >= 0 Change a % b to a - n * b: 2 > 7 - n * 2 >= 0 Dividing all 3 sides by 2: 1 > 3.5 - n >= 0 Subtracting 3.5 from all sides: -2.5 > -n >= -3.5 Multiplying by -1 (arrows will change direction): 2.5 < n <= 3.5 Because n must be whole number, it must be 3, because only whole number between 2.5 and 3.5 is 3. We'll put n in a - n * b formula: 7 - 3 * 2 = 7 - 6 = 1 We got 1. Maybe too complicated, but I like mathematical proving.
1st Jan 2020, 8:32 PM
Seb TheS
Seb TheS - avatar
0
5//2 = 2 -> // rounds it to floor value 7%2 = 1
1st Jan 2020, 5:40 PM
Avinesh
Avinesh - avatar
0
How 7%2=1comes?
1st Jan 2020, 5:42 PM
Deepanshu Verma
Deepanshu Verma - avatar
0
Remainder of the division
1st Jan 2020, 5:42 PM
Avinesh
Avinesh - avatar
0
Your tag 'float' makes no sense - your question is not about floats. Can you change it to something more fitting?
1st Jan 2020, 6:58 PM
HonFu
HonFu - avatar