[Tutor] not a good tutorial, in my view

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 18 Feb 2001 16:19:08 -0800 (PST)

On Sun, 18 Feb 2001 bxuef@freemail.sx.cn wrote:

> It appears the Python 2.0 tutorial are not written for beginners
> without programming background.

In one sense, the tutorial's is a "good" one because it is specifically
tailored for people who have previous programming experience.  For those
who haven't programmed before though, it stinks.  *grin*

Take a look at the Introductions section of python.org: it has tutorials
that are targeted toward newcomers:


> 1. I typed:
> >>>"doesn\t"
> # and it produced the following result:
> >>>'doesn\011'
> >>> 'yes\'he said'
> "yes'he said"
> >>>>>> "\"yes,\" he said"
> '"yes," he said'
> what is the use of this slash? 

Let's see what happens when we don't put the slash.

>>> ""yes, " he said"
  File "<stdin>", line 1
    ""yes, " he said"
SyntaxError: invalid syntax

Strings in Python are surrounded by pairs of quotation marks.  For

    "hello world"
    "how to solve it"
    "harry potter"

The tricky part is when we want to put quotation marks within a
string.  When we tried:

   ""yes", he said"

we wanted Python to understand that we wanted quotes around
"yes".  However, here's that Python sees, piece by piece:

   "    : Ok, we have an opening quote, so I'm reading a string.

   "    : There's the closing quote.  We must mean the empty string.

   yes  : What does yes mean?  It's outside quotes, and I don't 
          know what to do!

And that's the problem; how can we put quotes within a string?  The
solution that Python chooses is to make a character that's special;
whenever Python sees it during string reading, it'll escape out of it's
regular set of rules.  For example, when we feed it:


here's what Python's thinking:

    "  : ok, opening quote mark
    \  : Oh!  This is an escape character.  That means I should do
         something special to the next thing I see.
    "  : Ah, ok, so I won't close off the string yet.  I'll just add
         the literal quote character to our string.  Let's go back
         to our normal set of rules.
    "  : Closing quote

and we end up with the string that contains one quotation mark.

> 2. What is the use of 'strip' here?
> >>> string.strip('str') + 'ing'
> 'string'
> and why this does not work:
> >>> string.strip('str') 'ing'
> SyntaxError: invalid syntax
> It is explained in the tutorial, but I can not understand.

Ah.  Really subtle point; you don't need to worry about this for a while.  
This is what they mean:  Whenever we put two strings together like this:

>>> "this is" "two literal strings"
'this istwo literal strings'

Python can see that both are obviously strings, so it'll put them together
automatically.  However, when we do:

>>> string.strip('str') 'ing'
  File "<stdin>", line 1
    string.strip('str') 'ing'
SyntaxError: invalid syntax

The idea is that Python has no clue if string.strip() will return a string
or not, so it'll go bonkers again.  What we need to do is convince Python
that we really want it to put strings together:

>>> string.strip('str') + 'ing'

Adding a '+' in the middle of those two tells Python to try adding the
result of string.strip() with 'ing', in any way possible.

> 3. In the tutorial 3.1.3. Why there is the unicode part. what's the
> use of unicode in programming?

Unicode's important for people who plan to write multilingual programs;
that is, programs that need to deal with more than english text.  Since
the tutorials tailored to professional programmers, that's why it talks
about this issue.

To tell the truth, I don't know Unicode either.  *grin*

Good luck to you!