+ 1

Why this Strange behaviour with tupples

Look at the code and tell me if you don't think that python acts strangely with these tuples of tuples https://code.sololearn.com/cKhnrli86Tah/?ref=app

12th Jul 2018, 3:26 PM
Jose Berlines
Jose Berlines - avatar
2 odpowiedzi
+ 3
x = (2) #int x = 2 x = (2,) #tuple x = 2, #You should add a comma if you want a tuple with one value
12th Jul 2018, 3:40 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
On line 1 you have: F=((2,)) That is not a nested tuple, i.e. tuple(tuple with one element), it's just a (tuple with one element) wrapped in a discarded set of grouping parentheses. Without a comma, Python can't tell the difference between parenthesis used for order of operations and a tuple with one element ... and neither can people. That's why 1-element tuples show/require a trailing comma, and 2+ elements do not: F=((2,),) That's a tuple(tuple with one element)
12th Jul 2018, 4:59 PM
Kirk Schafer
Kirk Schafer - avatar