Rima
Rima

Reputation: 1455

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

Upvotes: 1

Views: 86

Answers (2)

leed
leed

Reputation: 312

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.

Upvotes: 3

JMA
JMA

Reputation: 813

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

Upvotes: 2

Related Questions