Computer Science
If a = (5, 4, 3, 2, 1, 0) evaluate the following expressions:
(a) a[0]
(b) a[1]
(c) a[a[0]]
(d) a[a[-1]]
(e) a[a[a[a[2]+1]]]
Python Tuples
12 Likes
Answer
(a) 5
(b) 4
(c) 0
(d) 5
(e) 1
Explanation:
- The first index of tuple a is 5. Therefore, a[0] ⇒ 5
- The second index of tuple a is 4. Therefore,
a[1] ⇒ 4 a[0]
represents first index of tuple a which is 5.
Now the expressiona[a[0]]
has becomea[5]
which implies 0.a[-1]
represents last index of tuple a which is 0.
Now the expressiona[a[-1]]
has becomea[0]
which represents first element of a i.e. 5.a[2]
represents third index of tuple a which is 3. Addition of 3 and 1 gives 4. Now the expressiona[a[a[a[2]+1]]]
has becomea[a[a[4]]]
where a[4] represents fifth index of tuple a i.e., 1.
Now the expressiona[a[a[4]]]
has becomea[a[1]]
wherea[1]
represents second index of tuple a i.e., 4.
Now the expressiona[a[1]]
has becomea[4]
wherea[4]
represents fifth index of a i.e. 1.
Answered By
5 Likes
Related Questions
How are individual elements of tuples accessed?
How do you create the following tuples?
(a) (4, 5, 6)
(b) (-2, 1, 3)
(c) (-9, -8, -7, -6, -5)
(d) (-9, -10, -11, -12)
(e) (0, 1, 2)
Can you change an element of a sequence? What if a sequence is a dictionary? What if a sequence is a tuple?
What does a + b amount to if a and b are tuples?