QUESTION IMAGE
Question
what happens when you run the following code?
my_tuple = (1, 2, 3)
my_tuple.append(4)
print(my_tuple)
the tuple is modified to (1, 2, 3, 4)
the code runs without any issues.
the code raises an attributeerror because tuples dont have an append method.
the code raises a typeerror because tuples are immutable.
question 7
1 pts
which data structure uses keys to access its values?
string
list
tuple
dictionary
Brief Explanations
- For the first code - related question:
- Tuples in Python are immutable data structures. They do not have an
appendmethod. When we try to callmy_tuple.append(4), Python checks if theappendattribute exists for themy_tupleobject (which is of typetuple). Since tuples do not have anappendmethod, it raises anAttributeError. - If it were a
TypeError(as in the option "The code raises a TypeError because tuples are immutable"), it would be related to an operation that tries to modify the tuple in a way that is conceptually wrong for its type (e.g., trying to re - assign an element by index in a wrong - type context). But here, the error is specifically about the non - existence of theappendmethod. - For the data - structure question:
- A
stringis accessed by index. For example,s = "hello",s[0]gives'h'. - A
listis also accessed by index. For example,l = [1, 2, 3],l[0]=1. - A
tupleis accessed by index. For example,t=(1, 2, 3),t[0]=1. - A
dictionaryis a key - value store. We access its values using keys. For example,d = {"key1": "value1", "key2": "value2"},d["key1"]gives"value1".
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
- First question: C. The code raises an AttributeError because tuples don't have an append method.
- Second question: D. Dictionary