On Sun, Dec 01, 2013 at 02:57:33PM +0530, Reuben wrote: > I mean occurrence of 2 from numbers 1 to 100. The number could be the > first digit or second digit in a two digit number..for e.g. In number 21 it > appears as first digit. For number 92 it appears as second digit
The most efficient way is to use a bit of reasoning: 2, 12, 20 through 29, then 32, 42, 52 etc. But if you have to do it computationally: for i in range(1, 101): print "2" in str(i) -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor