Accessing Elements of Tuples:-
Tuple से element को access करने के लिए 2 तरीके का use किया जाता है-
Two methods are used to access element from tuple-
i. By using index number
ii. By using slice operator
i. By using index number:-
a |
10 |
20 |
“apple” |
30 |
40 |
“mango |
Postive index |
0 |
1 |
2 |
3 |
4 |
5 |
Negative index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
print(a[1]) # 20
print(a[5]) # mango
print(st[-1]) # mango
print(st[-5]) #20
ii. By using slice operator:-
Syntax:- [start:stop:step]
a |
10 |
20 |
“apple” |
30 |
40 |
“mango |
Postive index |
0 |
1 |
2 |
3 |
4 |
5 |
Negative index |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
*
a=(10,20,”apple”,30,40,”mango”)
print(a[1:5]) # (20, ‘apple’, 30, 40)
a=(10,20,”apple”,30,40,”mango”)
print(a[::2]) # (10, ‘apple’, 40)
a=(10,20,”apple”,30,40,”mango”)
print(a[::-1]) # (‘mango’, 40, 30, ‘apple’, 20, 10)