Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
Hello, On Sun, Dec 1, 2013 at 3:50 PM, Reuben reuben.dl...@gmail.com wrote: Hi, How can we write a logic for detecting the number 2 in range from 1 to 100 You question is unclear. Could you please give more details ? Best, Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On Sun, Dec 01, 2013 at 11:20:02AM +0530, Reuben wrote: Hi, How can we write a logic for detecting the number 2 in range from 1 to 100 2 in range(1, 101) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
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
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On 01/12/2013 05:50, Reuben wrote: Hi, How can we write a logic for detecting the number 2 in range from 1 to 100 Regards, Reuben Paper and pen or pencil should be perfectly adequate for this task. Alternatively, open an editor, type some code, run it, if you have problems ask another question here, preferably one that makes sense :) Unless of course your question above is to be taken literally, in which case Mr. K'Aprano has already answered. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On Sun, Dec 01, 2013 at 08:43:46AM -0500, bruce wrote: hmm... two questions. (new to cmdline py) tried typing in what was typed in above in the python shell: for i in range(1, 101): print 2 in str(i) this did nothing.. Curious. Which Python shell did you use? I would expect that you get a prompt (without the quotes). I've changed the prompt in my Python to py, but by default you should have . Then, when you hit return at the end of the first line, you should get the second level prompt, You'll need to add at least one space, or tab, to indent the second line. Then when you hit enter again you'll get a ... prompt, Enter one last time and the code will run. Here's what I get (changing 101 to a smaller number for brevity: py for i in range(1, 11): ... 2 in str(i) ... False True False False False False False False False False However, I may have inadvertently been misleading. Outside of the interactive shell, even though that code will run, it won't display any output. Only in the interactive shell does that print True and False as above. Outside of the interactive shell, you need to use the print statement or function to see the output, otherwise Python calculates the answer and then doesn't do anything with it. So it may be better to write this as: for i in range(1, 101): print (2 in str(i)) which will work anywhere. def aa(): for i in range(1, 101): print 2 in str(i) aa() error:: aa() Traceback (most recent call last): File stdin, line 1, in module NameError: name 'aa' is not defined That is remarkable. I cannot explain this error. Are you using IDLE or some other shell? the other question, what does the in function within py do?? I've used str.find() to look if a substring is present, but didn't know a in even exists..! The in operator tests whether one object includes another object. For example, with strings it tests substrings: hat in what = returns True hat in h-a-t = returns False With lists and tuples, it tests to see if an item is the given value: 23 in [1, 5, 23, 99] = returns True dog in [cat, dog, mouse] = returns True dog in [cats, dogs, mice] = return False But it only looks one level deep! 23 in [1, 2, 3, [22, 23, 24], 5, 6] = returns False With dictionaries, it checks to see if the given object is a key: 5 in {2: two, 5: five, 7: seven} # {key: value} = returns True but not a value: five in {2: two, 5: five, 7: seven} = returns False -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On Sun, Dec 1, 2013 at 6:14 AM, Steven D'Aprano st...@pearwood.info wrote: 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. [n for n in range(100) if n % 10 == 2 or n // 10 == 2] [2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42, 52, 62, 72, 82, 92] Floor division (//) and modulo (%) are explained here: http://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On 01/12/2013 17:40, eryksun wrote: On Sun, Dec 1, 2013 at 6:14 AM, Steven D'Aprano st...@pearwood.info wrote: 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. [n for n in range(100) if n % 10 == 2 or n // 10 == 2] [2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 42, 52, 62, 72, 82, 92] Floor division (//) and modulo (%) are explained here: http://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations I wish people would use Python 3 references. I believe it would lead to a greater take up of, IMHO, a superior product than Python 2. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On Sun, Dec 1, 2013 at 12:53 PM, Mark Lawrence breamore...@yahoo.co.ukwrote: On 01/12/2013 17:40, eryksun wrote: On Sun, Dec 1, 2013 at 6:14 AM, Steven D'Aprano st...@pearwood.info wrote: 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 This way may not be faster, but it may be simpler to understand: for n in range(100): ... if '2' in str(n): ... print n ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On 01/12/2013 17:59, Joel Goldstick wrote: On Sun, Dec 1, 2013 at 12:53 PM, Mark Lawrence breamore...@yahoo.co.uk mailto:breamore...@yahoo.co.uk wrote: On 01/12/2013 17:40, eryksun wrote: On Sun, Dec 1, 2013 at 6:14 AM, Steven D'Aprano st...@pearwood.info mailto:st...@pearwood.info wrote: 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 This way may not be faster, but it may be simpler to understand: for n in range(100): ... if '2' in str(n): ... print n ... Why do you appear to be replying to me but haven't quoted anything that I've said? It's also difficult to see who said what above as the normal attribution marker levels are missing. Time to boot google products into touch? As for code speed who cares? Good programmers jump that fence when they have to, not because they can. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On 12/01/2013 06:50 AM, Reuben wrote: Hi, How can we write a logic for detecting the number 2 in range from 1 to 100 Do you mean: if 2 in numbers: ? Also for a more general solution, think at the very nice function any(bools), in combination with a generator comprehension: numbers = [2,3,4,5,6,8] if any((n%2 == 1) for n in numbers): print(some is odd) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
I tried it with the python interpreter as mentioned below: test@test-Inspiron-1564:~/learn$ python Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type help, copyright, credits or license for more information. On Sun, Dec 1, 2013 at 7:50 PM, Steven D'Aprano st...@pearwood.info wrote: On Sun, Dec 01, 2013 at 08:43:46AM -0500, bruce wrote: hmm... two questions. (new to cmdline py) tried typing in what was typed in above in the python shell: for i in range(1, 101): print 2 in str(i) this did nothing.. Curious. Which Python shell did you use? I would expect that you get a prompt (without the quotes). I've changed the prompt in my Python to py, but by default you should have . Then, when you hit return at the end of the first line, you should get the second level prompt, You'll need to add at least one space, or tab, to indent the second line. Then when you hit enter again you'll get a ... prompt, Enter one last time and the code will run. Here's what I get (changing 101 to a smaller number for brevity: py for i in range(1, 11): ... 2 in str(i) ... False True False False False False False False False False However, I may have inadvertently been misleading. Outside of the interactive shell, even though that code will run, it won't display any output. Only in the interactive shell does that print True and False as above. Outside of the interactive shell, you need to use the print statement or function to see the output, otherwise Python calculates the answer and then doesn't do anything with it. So it may be better to write this as: for i in range(1, 101): print (2 in str(i)) which will work anywhere. def aa(): for i in range(1, 101): print 2 in str(i) aa() error:: aa() Traceback (most recent call last): File stdin, line 1, in module NameError: name 'aa' is not defined That is remarkable. I cannot explain this error. Are you using IDLE or some other shell? the other question, what does the in function within py do?? I've used str.find() to look if a substring is present, but didn't know a in even exists..! The in operator tests whether one object includes another object. For example, with strings it tests substrings: hat in what = returns True hat in h-a-t = returns False With lists and tuples, it tests to see if an item is the given value: 23 in [1, 5, 23, 99] = returns True dog in [cat, dog, mouse] = returns True dog in [cats, dogs, mice] = return False But it only looks one level deep! 23 in [1, 2, 3, [22, 23, 24], 5, 6] = returns False With dictionaries, it checks to see if the given object is a key: 5 in {2: two, 5: five, 7: seven} # {key: value} = returns True but not a value: five in {2: two, 5: five, 7: seven} = returns False -- Steven ___ 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
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
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 On 01-Dec-2013 2:45 PM, Steven D'Aprano st...@pearwood.info wrote: On Sun, Dec 01, 2013 at 11:20:02AM +0530, Reuben wrote: Hi, How can we write a logic for detecting the number 2 in range from 1 to 100 2 in range(1, 101) -- Steven ___ 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
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
Thanks everyone for all the replies. On Sun, Dec 1, 2013 at 11:29 PM, Joel Goldstick joel.goldst...@gmail.comwrote: On Sun, Dec 1, 2013 at 12:53 PM, Mark Lawrence breamore...@yahoo.co.ukwrote: On 01/12/2013 17:40, eryksun wrote: On Sun, Dec 1, 2013 at 6:14 AM, Steven D'Aprano st...@pearwood.info wrote: 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 This way may not be faster, but it may be simpler to understand: for n in range(100): ... if '2' in str(n): ... print n ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ 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
Re: [Tutor] Occurrence of number 2 in a range from 1 to 100
On 01/12/2013 18:02, Reuben wrote: Thanks everyone for all the replies. No problem but please don't top post, it makes following long threads particularly difficult, thanks. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor