how do you access the last element of the tuple: A=(0,1,2,3) : | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

how do you access the last element of the tuple: A=(0,1,2,3) :

Write a code and please explain.

22nd Mar 2021, 9:43 AM
✨RJ✨
✨RJ✨ - avatar
4 Answers
+ 2
Use negative indexing A=(1,2,3) Print (A[-1]) Output 3 It will return last element Print(A[-2]) Output 2 It will return second last element of tuple
22nd Mar 2021, 10:08 AM
Tarun Gautam
Tarun Gautam - avatar
+ 3
A[-1]
22nd Mar 2021, 9:50 AM
[bool left=True;]
[bool left=True;] - avatar
+ 1
In tuple A if u want to access last element then u have to write like this A[-1] this represent last element If u want to access second last them A[-2] it will print 2 for first one from last A[-3] ..... Similar for more element Start from -1 and do incremet by -1
22nd Mar 2021, 10:14 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
A[-1]
30th Mar 2021, 2:49 AM
Kun
Kun - avatar