Why the output of 1%2 is 1 in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
22 Answers
+ 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
+ 7
Hey Mohammad Esmail Qasimi. First of all you have to understand the meaning of the % symbol in Python. In mathematics, that symbol is known to be a percentage but in python it is not. It is actually an operator symbol called a modulo operator. Now what does this mean? A modulo operator symbol is one that retains the remainder in an operation. Suppose x%y, this will output the remainder of x divided by y. So in your question, 1%2 simply means that when 1 is divided by 2, what is the remainder. The remainder is 1 because normal division of 1/2 will retain a 0 but remainder of 1. Hope you are answered.
24th Feb 2022, 5:34 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 4
% is the module operator, it always give the remainder of an division.
23rd Feb 2022, 6:11 PM
Vaibhav
Vaibhav - avatar
+ 4
Quantum , the remainder is 1. Suppose we have a/b=c. a is the divident b is the divisor c is the quotient And now suppose we have x%y=z. x is the divident y is the divisor z is the remainder. I hope it all makes sense
24th Feb 2022, 6:57 PM
Takudzwa A Svosve
Takudzwa A Svosve - 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
Thanks Mohammad Esmail Qasimi for marking my answer as best, im glad i was of help to you.
24th Feb 2022, 8:06 PM
Takudzwa A Svosve
Takudzwa A Svosve - avatar
+ 3
Thanks HungryTradie , i think you have put it in the simplest form anyone should be able to understand.
24th Feb 2022, 8:24 PM
Takudzwa A Svosve
Takudzwa A Svosve - 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