python - Why are tuples enclosed in parentheses? -
a tuple comma-separated list of values
so valid syntax declare tuple is:
tup = 'a', 'b', 'c', 'd' but see declaration this:
tup = ('a', 'b', 'c', 'd') what benefit of enclosing tuples in parentheses ?
the parentheses parentheses - work changing precedence. exception if nothing enclosed (ie ()) in case generate empty tuple.
the reason 1 use parentheses nevertheless result in consistent notation. can write empty tuple , other tuple way.
another reason 1 want literal have higher precedence other operations. example adding 2 tuples written (1,2)+(3,4) (if omit parentheses here 1,2+3,4 means add 2 , 3 first form tuple - result 1,5,4). similar situations when want pass tuple function f(1,2) means send arguments 1 , 2 while f((1,2)) means send tuple (1,2). yet if want include tuple inside tuple ((1,2),(3,4) , (1,2,3,4) 2 different things.
Comments
Post a Comment