On 08/19/10 02:10, Νίκος wrote:
("nikos",) is a single element tuple.
["nikos"] is a single element list.
["nikos",] is also a single element list, just written like the tuple.

It makes more sense if i:

"nikos" is just a string
("nikos") is a single element tuple
["nikos"] is also a single element list

After all () used to define tuples and [] usedd to define lists. Why
commas?

You have to look at the other side: what *else* they're used for. Python also uses () to override order of operations (and to call functions, but that's contextually different) which can occur in the same context as tuples, while [] are used only within contexts where they can be disambiguated. Going back to one of the originals example posted in this thread:

  (1) + (2)

do you think this should yield (1, 2) or 3? It would be crazy if evaluation of

  (3*14) + (7*21)

was reduced, treated as "(42) + (147)" and then reduced to "(42,147)" instead of 189.

So Python needs a way to express that you *explicitly* mean "this is one of those rare one-element tuples, not an order of operations prioritization":

  (1,) + (2,)

to return "(1,2)"

Also is there a difference between 'nikos' or "nikos" or '''nikos''' ?
What's and why best to use to enclose strings?

Internally, there's no difference other than how easily you can include " or ' characters in your string. Thus you might write:

  with_dquote = 'He said "Hello"'
  with_apos = "It's 2:00am"
  with_both1 = """She said "Don't touch me" to her boss"""
  with_both2 = '''She said "Don't touch me" to her boss'''

You can also prefix any of them with "r" such as

  file_path = r"c:\path\to\file.txt"
  file_path = r'c:\path\to\file.txt
  file_path = r"""c:\path\to\file.txt"""
  file_path = r'''c:\path\to\file.txt'''

to alter how "\" are treated.

Otherwise, if it doesn't make a difference, I tend to use C-ish conventions of using " for strings and ' for single characters:

  if 'w' in "hello world":

but the important/kind thing is to be internally consistent to make your own life easier. :)

-tkc





--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to