[Tutor] Truckers Log....Me Again

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 21 Jan 2002 23:16:21 -0800 (PST)


On Mon, 21 Jan 2002, Erik Price wrote:

> Today I learned about tuples and dictionaries.  I'm not going to come
> to any conclusions about the language until I've gotten to play with
> it some (just too busy with Work at the moment), but I am initially
> surprised by the separate distinction of lists and dictionaries
> (which, unconsciously, I end up replacing with numeric and associative
> arrays -- a habit that I am trying to break).


I'm not too familiar with PHP, but I read a little of:

    http://www.intranetjournal.com/articles/200005/phpe.html

and I think I have a better idea about it.  I believe that Python
dictionaries are similar in concept to PHP's arrays, though PHP's arrays
do allow you to keep the array in some sort of order.


> its absence in last night's pseudocode, is about the 'break'
> statement.  Between the tutorials I'm reading and discussion on this
> list, it seems to be used more frequently than the 'break' statement
> which appears in PHP.  Is this normal for Python?

Yes, I think that Python programmers use 'break' quite a bit.


> I'm also a stickler for detail.  A question about quoting -- I have
> seen double and single quotes used fairly interchangeably.  My
> reference doesn't distinguish a difference between these (only that
> escaping singlequotes inside doublequotes or vice versa isn't
> necessary, or in triple (single|double) quotes).

In Python, double and single quotes are interchangable --- there is no
difference between them.


> But in PHP/Perl/bash, variables expand in double-quotes so the
> difference is important.  Where does Python stand on this subject?  
> (I'm surprised that I didn't read something about this already.)

Expansion happens when we do string interpolation --- in Python terms,
it's called "string formatting".  Here's an example:

###
>>> mygreeting = "Hello %s!"
>>> mygreeting % ("Erik",)
'Hello Erik!'
>>> mystory = "Once there was a %(adj)s monkey, who lived in a %(noun)s."
>>> mystory % { 'adj' : 'chunky',                                              
...             'noun' : 'beehive' }
'Once there was a chunky monkey, who lived in a beehive.'
###

So we don't even need to worry about what happens with single or double
quotes.  String formatting is an operation that's separated from building
a string literal.

We can find out more information on string formatting here:

    http://www.python.org/doc/tut/node9.html#SECTION009100000000000000000

and there some more reference material here that might help:

    http://www.python.org/doc/current/lib/typesseq-strings.html

If you have questions on it, please feel free to ask on Tutor, and people
here can cook up more interesting examples.



> And I apologize for constantly making reference to PHP or other
> languages, it's just that in programming, they're my only frame of
> reference.

It's the right thing to learn something by borrowing ideas from something
you already know.  You don't have to apologize!  Please feel free to make
references to PHP and other languages; it's instructive to see how to get
ideas to translate between languages.

Good luck!