KnowledgeBoat Logo
|

Computer Science

Consider the given two statements:

Statement 1: t1 = tuple('python')
Statement 2: t1[4] = 'z'

Assertion (A): The above code will generate an error.

Reasoning (R): Tuple is immutable by nature.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Python Tuples

1 Like

Answer

Both A and R are true and R is the correct explanation of A.

Explanation
The statement t1 = tuple('python') creates a tuple t1 from the string 'python', resulting in t1 being ('p', 'y', 't', 'h', 'o', 'n'). The statement t1[4] = 'z' attempts to modify the element at index 4 of the tuple, which is not allowed because tuples are immutable in Python. Hence, it raises an error.

Answered By

3 Likes


Related Questions