1

Why the output of following code is 4 and not tuple (4,)?

Why the output of following code is 4 and not tuple (4,) ?

tup = (1, 2, 4, 8)
tup = tup[-2:-1]
tup = tup[-1]
print(tup)

output

4

Submitted May 27th 2021 by Admin

Answers
0

You are selecting one element from a tuple, so it gives you that specific element (an int, in this case).

Admin | 3 months ago


0

When you called tup[-1] you accessed a specific element at index -1. On the other hand, when you called tup[-2:-1] you sliced the tuple, resulting in a tuple.

Admin | 3 months ago



Relevant Questions