To elaborate a little more this is comparing the 'one' bit. Any odd number will have the 'one' bit set. On Oct 14, 2015 6:30 PM, "Joel Goldstick" <joel.goldst...@gmail.com> wrote:
> On Wed, Oct 14, 2015 at 4:47 PM, ਨਿਹੰਗ ਪੰਜਾਬੀ <niih...@gmail.com> wrote: > > > 'if (n & 1)' below works but I don't understand why/how. Kindly help. > > > > ============== > > >>> def fn(n): > > ... if (n & 1): > > ... print "n is odd" > > ... else: > > ... print "n is even" > > ... > > >>> fn(5) > > n is odd > > >>> fn(4) > > n is even > > > > =============== > > > > & is a bitwise operator, so any odd number and 1 will be one (true), and > any even number will be zero (false) > Any > > > > > Thanks > > Ni > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Joel Goldstick > http://joelgoldstick.com/stats/birthdays > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor