KnowledgeBoat Logo

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:
  1. The first index of tuple a is 5. Therefore, a[0] ⇒ 5
  2. The second index of tuple a is 4. Therefore,
    a[1] ⇒ 4
  3. a[0] represents first index of tuple a which is 5.
    Now the expression a[a[0]] has become a[5] which implies 0.
  4. a[-1] represents last index of tuple a which is 0.
    Now the expression a[a[-1]] has become a[0] which represents first element of a i.e. 5.
  5. a[2] represents third index of tuple a which is 3. Addition of 3 and 1 gives 4. Now the expression a[a[a[a[2]+1]]] has become a[a[a[4]]] where a[4] represents fifth index of tuple a i.e., 1.
    Now the expression a[a[a[4]]] has become a[a[1]] where a[1] represents second index of tuple a i.e., 4.
    Now the expression a[a[1]] has become a[4] where a[4] represents fifth index of a i.e. 1.

Answered By

5 Likes


Related Questions