Re: [Tutor] x%2
bob gailer wrote: On 1/10/2012 1:47 PM, Noah Hall wrote: a % b is the remainder operator. It returns what's "left" after dividing a by b. Not to beat a dead horse- but % is the modulo operator. That depends on how you define "remainder" and "modulo". There is no definition agreed on by all people, and so we get into terminology disputes. To a mathematician, "modulo operator" is meaningless. Modulo is a modifier to a statement, not an operator, and is written "mod" as in: 1 = 5*3 mod 7 It returns the residue class of the 2 operands. When a is positive this is the same as remainder, but not so for negative a. "Remainder" is ambiguous for negative values. -7/5 could be given as -1 with -2 remainder, or as -2 with 3 remainder. One might define a remainder operation as returning a result: - which is always positive - with the sign of the divisor - with the sign of the dividend - which is closest to zero - which is furthest away from zero The last two require further variations, depending on how you resolve ties. One might also define i%0 to be i, or as undefined. So by my count, there are at least 18 consistent ways to define a remainder/modulo operator. A very few languages define two operators, or functions, e.g. Ada defines a rem operator which returns the remainder with the sign of the dividend and a mod operator which returns the remainder with the sign of the operator. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] x%2
On 1/10/2012 1:47 PM, Noah Hall wrote: a % b is the remainder operator. It returns what's "left" after dividing a by b. Not to beat a dead horse- but % is the modulo operator. It returns the residue class of the 2 operands. When a is positive this is the same as remainder, but not so for negative a. >>> 5%3 2 >>> -5%3 1 >>> FWIW the Python documentation (at least as of 2.7.2 has this wrong!) -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] x%2
On Tue, Jan 10, 2012 at 1:47 PM, Noah Hall wrote: > On Tue, Jan 10, 2012 at 6:24 PM, emin wrote: >> answers = ["yes","no"] >> reaction = ["OK.I GOT IT.But why symbol of percent % not symbol of division >> / ?","PLEASE EXPLAIN MORE"] >> >> print "1st SORRY FOR BAD ENGLISH & DISTURBING:((i am beginner)" >> print "So you want to say it doesnt mean 2 percent of x and it means x >> divisible by 2?" >> print "Yes" >> print "or" >> print "No" >> answerChoise = raw_input("Type here: ") >> >> if answerChoise == "yes" or answerChoise == "Yes": >> print reaction[0] >> >> elif answerChoise == "no" or answerChoise == "No": >> print reaction[1] > > Urm, yeah, while this is cute and all, it's not a great way of asking > a question. > > I'm not totally sure what you mean, so I'm just going to say - > > a / b is the division operator. It divides things. It returns a divided by b. > a % b is the remainder operator. It returns what's "left" after dividing a by > b. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- The / operator signifies division. The % signifies modulo. see this: http://en.wikipedia.org/wiki/Modulo_operation and see this: http://docs.python.org/reference/expressions.html#index-998 Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] x%2
On Tue, Jan 10, 2012 at 6:24 PM, emin wrote: > answers = ["yes","no"] > reaction = ["OK.I GOT IT.But why symbol of percent % not symbol of division > / ?","PLEASE EXPLAIN MORE"] > > print "1st SORRY FOR BAD ENGLISH & DISTURBING:((i am beginner)" > print "So you want to say it doesnt mean 2 percent of x and it means x > divisible by 2?" > print "Yes" > print "or" > print "No" > answerChoise = raw_input("Type here: ") > > if answerChoise == "yes" or answerChoise == "Yes": > print reaction[0] > > elif answerChoise == "no" or answerChoise == "No": > print reaction[1] Urm, yeah, while this is cute and all, it's not a great way of asking a question. I'm not totally sure what you mean, so I'm just going to say - a / b is the division operator. It divides things. It returns a divided by b. a % b is the remainder operator. It returns what's "left" after dividing a by b. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] x%2
emin wrote: http://www.youtube.com/watch?src_vid=QaYAOR4Jq2E&feature=iv&annotation_id=annotation_149056&v=M3g1GEkmyrw in this tutorial what does mean x%2 ? x % 2 gives the remainder when you divide x by 2. i think: i * 2% = always even number What does that mean? i*2% does not work in Python. In Python, % does not mean percentage and so you can't multiply by 2%. I guess you mean i % 2 == 0 means i is an even number and this would be correct. If i % 2 == 0 then i is even. If it equals 1 then i is odd. but why not 4,6 or 8? but i * 4(6,8,10,12...)% = always even number too The remainder i % n will be a number between 0 and n-1. i%2 is useful because there are only two possible results, 0 (i is even) or 1 (i is odd). i%4 is less useful, because there are four possibilities: 0 (i is even) 1 (i is odd) 2 (i is even) 3 (i is odd) i%10 is less useful still, because there are ten possibilities: 0,2,4,6,8: i is even 1,3,5,7,9: i is odd for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...) even numbers = {2,4,6,8,10,12...} I don't understand what you are trying to say here. and how pyton understanding 0(even or odd number or it is an exception?)? 0 is always an even number, because it has 0 remainder when you divide by 2. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] x%2
2012/1/8 emin : > http://www.youtube.com/watch?src_vid=QaYAOR4Jq2E&feature=iv&annotation_id=annotation_149056&v=M3g1GEkmyrw > in this tutorial what does mean x%2 ? > i think: i * 2% = always even number > but why not 4,6 or 8? but i * 4(6,8,10,12...)% = always even number too > for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...) > even numbers = {2,4,6,8,10,12...} > and how pyton understanding 0(even or odd number or it is an exception?)? While it is true that all numbers divisible by 4 are even, not all even numbers are divisible by 4. All even numbers, however, are divisible by 2. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor