+ 2

Why the output of 1%2 is 1 in python

Print(1%2) Output: 1 Why shouldn't be 0 ??? Can someone explain

22nd Feb 2022, 8:28 PM
Mohammad Esmail Qasimi
Mohammad Esmail Qasimi - avatar
26 odpowiedzi
+ 4
Hi Mohammad! 1 % 2 is not 0 because 1 / 2 = 0.5 and 0.5 is not a whole number. If the result of any division isn‘t a whole number then there is a remainder, thats why you have a comma in 0.5. So I give you some examples of Modulo: 7 % 3 means how many 3‘s you have in 7 and what remains: So 7 = 2 * 3 + 1 and what comes after the (+) is the remainder. Therefore 1 is the remainder here. 3 % 4 means how many 4‘s do you have in 3 and what remains: 3 = 0 * 4 + 3 so 3 is the remainder. 1 % 2 means how many 2‘s you have in 1 and what remains: 1 = 0 * 2 + 1 so 1 is the remainder thats why 1 % 2 = 1.
24th Feb 2022, 6:46 PM
Ferdaws Kukcha
Ferdaws Kukcha - avatar
+ 18
% is the modulo operator, it give the remainder of an integer division. How often does 2 go in one? 0 So what remains? 1
22nd Feb 2022, 8:31 PM
Lisa
Lisa - avatar
+ 13
Quite simply, this operator is called modulo which returns the remainder of a division: For instance, 9 divided by 4 equals 2 but it remains 1. Here, 9 / 4 = 2 and 9 % 4 = 1. 👤 🍎🍎🍎🍎 👤 🍎 🍏 🍎 👤 🍎🍎 👤 ______________________ 👤 🍎🍎 🍎🍎 👤 🍏 👤 🍎🍎 🍎🍎 👤 Think of it as if I asked you what's REMAINDER after dividing APPLES by 4 people. That's the purpose of this division process, so there's no need to look at anything other than a GREEN apple. In your example: 1 divided by 2 gives 0 but it remains 1 (1 % 2 == 1) So the modulo operation can be calculated using this equation: =================== a % b = a - floor(a / b) * b =================== ▪️ floor(a / b) represents the number of times you can divide a by b. ▪️ floor(a / b) * b is the amount that was successfully shared entirely. ▪️ The total (a) minus what was shared equals the remainder of the division. Applied to your example, this gives: 1 % 2 = 1 - floor(1/2) * 2 = 1
24th Feb 2022, 6:28 PM
Moree
Moree - avatar
+ 4
% is the module operator, it always give the remainder of an division.
23rd Feb 2022, 6:11 PM
Vaibhav
Vaibhav - avatar
+ 3
Hi Mohammad Esmail Qasimi You put it so well... How often 2(divisor) goes into 1(dividend) is 0 (quotient)... 1//2 would return this... 1%2 on the other hand returns the remainder, ergo, 1-0...
22nd Feb 2022, 8:58 PM
SAM
SAM - avatar
+ 3
Oh you are going to be stubborn. 😔 New example: You and a friend (= 2 people) want to go to the zoo. You have 1 zoo entry ticket. You try to share 1 between 2 but you can't do it without fractions (or decimals called float). You each get 0 tickets, the remaining tickets are 1. 1%2=1.
24th Feb 2022, 8:41 PM
HungryTradie
HungryTradie - avatar
+ 2
You can remember it like this... a%b = a if a<b Otherwise it's remainder of a/b
24th Feb 2022, 1:49 PM
Abhishek verma
Abhishek verma - avatar
+ 2
Quantum what likely hood is it that you are right & all these other programmers are wrong? Could it be a chance for you to learn, or are you going to be stubborn (and wrong)? Example using words: you have 7 grapes and 2 friends, you want to give each person an even share, how many are left? 7%3=1 because 7 split into 3 gives 3 groups of 2 with 1 remainder. Could you cut the last grape and share it? Yes, but not without using fractions (or decimals called float in programming), so keeping whole numbers (called integers in programming) is the remainder result of modulo.
24th Feb 2022, 8:15 PM
HungryTradie
HungryTradie - avatar
+ 2
🤭 7 % 5 = 2 ✅ Quantum (7/5=1) with 2 remainder. But 5 % 7 = 5 🙃 Manav Roy remainder of (5/7=0) is 5
25th Feb 2022, 6:54 AM
HungryTradie
HungryTradie - avatar
+ 1
See if i multiple 2 by 0.5, it's equal to 1 right? So in floats there is an option call round-off. If the number after "." Is greater than or equal to 5 so we remove that number and increase it's left sided number by 1. If i round-off 0.5 that will be equal to 1👍
27th Feb 2022, 4:44 PM
Hassan Ali
Hassan Ali - avatar
0
It's simple, a%2 : 1 if the number is odd else 0 if number is even
22nd Feb 2022, 8:39 PM
Sousou
Sousou - avatar
0
2%2 is like the words "what is the remainder of 2 divided by 2" = 0 (no remainder) 1%2 is like the words "what is the remainder of 1 divided by 2" = 1. (because 2 doesn't go into 1, but there is 1 remainder.) Modulo result is the remainder.
22nd Feb 2022, 8:44 PM
HungryTradie
HungryTradie - avatar
0
Hello team, I am new here. Who is also new here to team up with me.
24th Feb 2022, 4:07 PM
Maxwell Odonkor
Maxwell Odonkor - avatar
0
In Python, the `%` operator is the modulo operator, which returns the remainder of the division between two numbers. In the case of `1 % 2`, the division `1 / 2` has a quotient of 0 with a remainder of 1. When calculating `1 % 2`, the remainder is computed, not the quotient. Since the dividend (1) is smaller than the divisor (2), the remainder is equal to the dividend itself. Therefore, the output of `1 % 2` is 1. To illustrate further: print(1 % 2) # Output: 1 Here, `1` is the remainder obtained when dividing `1` by `2`, and thus it is the result of the modulo operation. If the dividend were larger than or equal to the divisor, the remainder would be different. For example, `5 % 2` would be `1` because `5` divided by `2` equals `2` with a remainder of `1`.
5th Jul 2023, 4:32 AM
Jarvis Silva
Jarvis Silva - avatar
- 1
As you said how often 2 goes to 1 is 0 So the answer should be 0 ??? Sorry if i am dumb Like 3%2 is 1 bcz 2 goes 3 once and the remaining is 1.
22nd Feb 2022, 8:34 PM
Mohammad Esmail Qasimi
Mohammad Esmail Qasimi - avatar
- 1
Because Modulo (%) gives the remainder
24th Feb 2022, 1:31 PM
Stella Matchinda
Stella Matchinda - avatar
- 2
در عالم ریاضی در اعداد صحیح یک رو تقسیم بر دو کنی باقیمانده میشه یک. چرا؟ چون یک بر دو بخش پذیر نیست در اعداد صحیح،
24th Feb 2022, 3:37 PM
HamidReza Yeganeh
HamidReza Yeganeh - avatar
- 3
Mumu 1 percent of 2 is what U no get talent for coding Better resign
24th Feb 2022, 7:58 PM
Festus Adebayo