On 19/01/14 13:59, rahmad akbar wrote:
hey guys, super  noob here, i am trying to understand the following code
from google tutorial which i failed to comprehend

Others have answered the specifics but some general advice here:

1) never forget the Python >>> prompt.
Try things out if you don't understand them.
Look at the results. Its a great learning tool.

2) use the built in help() function at the >>> prompt

Those two aids will answer about 80% of your questions.
And for the rest there is the tutor list! :-)

def not_bad(s):
   n = s.find('not')
   b = s.find('bad')
   if n != -1 and b != -1 and b > n:
     s = s[:n] + 'good' + s[b+3:]
   return s

  on the following lines, what is -1, is that index number?


>>> help(''.find)
Help on built-in function find:

find(...)
    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
(END)

>>> s = "Test string thats not interesting but not bad either"
>>> s.find('not')
18
>>> s.find('bad')
42
>>> s.find('gobbledegook')
-1

understand the entire second line
if n != -1 and b != -1 and b > n:
     s = s[:n] + 'good' + s[b+3:]

Again try this in bits at the >>> prompt:

>>> s[:18]    # using the value found above
'Test string thats '
>>> s[42+3:]   # again using value above
' either'
>>> s[:18] + 'good' + s[45:]
'Test string thats good either'

Hopefully that gives you some ideas on how
to use the >>> prompt to answer questions
as they arise.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to