0% found this document useful (0 votes)
2 views2 pages

Python Tuple and Dictionary Errors Explained

The document provides examples of Python code demonstrating tuple modification, error handling, dictionary updating, and nested print statements. It explains that tuples are immutable but can contain mutable objects, and that the remove() method is not applicable to tuples. Additionally, it shows how updating a dictionary replaces existing values and clarifies the behavior of nested print functions.

Uploaded by

mahiiagarwal70
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Tuple and Dictionary Errors Explained

The document provides examples of Python code demonstrating tuple modification, error handling, dictionary updating, and nested print statements. It explains that tuples are immutable but can contain mutable objects, and that the remove() method is not applicable to tuples. Additionally, it shows how updating a dictionary replaces existing values and clarifies the behavior of nested print functions.

Uploaded by

mahiiagarwal70
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Code Output/Error (Class 10 Level)

1. Modify Tuple's List Element

Code:

t = (23, 56, [4, 8, 9], 34)

t[2][1] = 3

print(t)

Output:

(23, 56, [4, 3, 9], 34)

Explanation:

Tuples are immutable, but if they contain a mutable object (like a list), the contents of that object can

be changed.

2. Remove from Tuple (Error)

Code:

t = (23, 56, [4, 8, 9], 34)

[Link](t[2][1])

print(t)

Error:

AttributeError: 'tuple' object has no attribute 'remove'

Explanation:

Tuples do not support item removal. The remove() method is available only for lists.
Python Code Output/Error (Class 10 Level)

3. Update Dictionary

Code:

d = {1:2, 2:1, 3:5, 5:3}

d1 = {3:8}

[Link](d1)

print(d)

Output:

{1: 2, 2: 1, 3: 8, 5: 3}

Explanation:

[Link](d1) replaces the value of key 3 with 8.

4. Nested Print Statement

Code:

print(print("hello"))

Output:

hello

None

Explanation:

The inner print("hello") executes first and prints 'hello'. Then print() returns None, which is printed by

the outer print.

You might also like