[Tutor] Occurrence of number 2 in a range from 1 to 100

Reuben reuben.dlink at gmail.com
Sun Dec 1 15:31:47 CET 2013


I tried it with the python interpreter as mentioned below:

test at 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 <steve at 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 at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131201/f034d9f4/attachment.html>


More information about the Tutor mailing list