-2
def minimum (*n):
        print(n)
minimum(1)
minimum(1,2)
    
    
def func(*args):
     print(args)
    
values1 = (1,2)
values2 = ((1,2), (3,4))
func(values1)
func(values2)

OUTPUT:
(1,)
(1, 2)
((1, 2),)
(((1, 2), (3, 4)),)

Process finished with exit code 0

First O/p: I think python is expecting multiple arguments to be passed so there is a comma (,) after 1. ?

Second O/p: Now the python sees multiple arguments being passed there is no comma. It stores the args a tuple?

Third O/p and Fourth O/p: Why is there still a comma? even after I passed 2 tuples assuming that python is expecting multiple tuples like the above?

Help me understand this.

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
redpy
  • 143
  • 5
  • 2
    `func(val)` passes exactly one argument. Which ends up in a tuple, because of `*args`. If you're passing a tuple as that one argument, well, then you get a tuple in a tuple. – deceze Aug 17 '21 at 09:54
  • did you see this : https://stackoverflow.com/questions/11315010/what-do-and-before-a-variable-name-mean-in-a-function-signature – I'mahdi Aug 17 '21 at 09:54
  • 1
    Does this answer your question? [What is the syntax rule for having trailing commas in tuple definitions?](https://stackoverflow.com/questions/7992559/what-is-the-syntax-rule-for-having-trailing-commas-in-tuple-definitions) – mhhabib Aug 17 '21 at 09:54
  • 1
    Are you aware that both ``(1,)`` and ``(1, 2)`` are tuples? Note how both are of the form ``(..., ...)``. – MisterMiyagi Aug 17 '21 at 10:00
  • so, the trailing comma is a good style of writing code and hence python prints those tuples with comma in the end? – redpy Aug 17 '21 at 10:04
  • 1
    The comma makes a tuple. ``(1,)`` is a tuple, ``(1)`` is not. – MisterMiyagi Aug 17 '21 at 10:05
  • @MisterMiyagi as mhhabib pointed out trailing comma (,) is a good style of writing. .. – redpy Aug 17 '21 at 10:06
  • 2
    It's unclear what "good style" means. It's *the way* to represent tuples. – deceze Aug 17 '21 at 10:06
  • I think what @Daniele Lin says makes sense. I wanted to know why the comma was present even after I passed more than one tuple. Hope you all agree.. – redpy Aug 17 '21 at 10:13

2 Answers2

1

The first output shows a comma because without it, 1 being the only element, (1) would be just a integer (parentheses are wrapping the expression 1), (1,) is shown to differentiate tuples and simple parentheses.

in the second one, no trailing comma is needed to differentiate tuples, since there are more than one element.

In the third O/p, you are not passing 1 and 2, but instead you're passing the whole (1,2), so it shows only one item (which is (1,2)) in a tuple, and adds an extra comma. Same for the fourth: your passing the entire ((1,2), (3,4)).

Daniele Lin
  • 78
  • 1
  • 6
-1

In the function call func(value2) , you're passing a list of arguments. It should be passed as func(*value2)

func(value2) --> func(((1, 2), (3, 4)))

func(*value2) --> func((1, 2), (3, 4))

Amith Lakkakula
  • 506
  • 3
  • 8
  • Thanks! my question was why is the comma trailing? even after passing more than one tuple. I assumed that once we pass multiple tuples the comma should not be present. – redpy Aug 17 '21 at 10:03
  • because without the * `((1, 2), (3, 4))` is being treated as a single entity. Count the number of braces in the output. – Amith Lakkakula Aug 17 '21 at 10:08