On Sat, Jul 13, 2019 at 09:59:16AM +1000, Cameron Simpson wrote:

> Mike has probably confused this with tuples. Because tuples are 
> delineated with parentheses, there is ambiguity between a tuple's 
> parentheses and normal "group these terms together" parentheses.

There are no "tuple parentheses". Tuples are created by the *comma*, 
not the parens. The only exception is the empty tuple, since you can't 
have a comma on its own.

    x = ()    # Zero item tuple.
    x = 1,    # Single item tuple.
    x = 1, 2  # Two item tuple.


Any time you have a tuple, you only need to put parens around it to 
dismbiguate it from the surrounding syntax:

    x = 1, 2, (3, 4, 5), 6     # Tuple containing a tuple.
    function(0, 1, (2, 3), 4)  # Tuple as argument to a function.


or just to make it more clear to the human reader.


> Here is a 2 element tuple:
> 
>  (9, 7)
> 
> How does one write a one element tuple? Like this:
> 
>  (9,)

To be clear, in both cases you could drop the parentheses and still get 
a tuple:

    9, 7

    9,

provided that wasn't in a context where the comma was interpreted as 
something with higher syntactic precedence, such as a function call:

    func(9, 7)    # Two integer arguments, not one tuple argument.
    func((9, 7))  # One tuple argument.


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

Reply via email to