Differentiate between list and tuples. class 12
waiting for early replies guys....
Share
Differentiate between list and tuples. class 12
waiting for early replies guys....
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
Explanation:
The main difference between lists and a tuples is the fact that lists are mutable whereas tuples are immutable.
What does that even mean, you say?
A mutable data type means that a python object of this type can be modified.
An immutable object can’t.
Let’s see what this means in action.
Let’s create a list and assign it to a variable.
>>> a = ["apples", "bananas", "oranges"]
Now let’s see what happens when we try to modify the first item of the list.
Let’s change “apples” to “berries”.
>>> a[0] = "berries"
>>> a
['berries', 'bananas', 'oranges']
Perfect! the first item of a has changed.
Now what if we want to try the same thing with a tuple instead of a list? Let’s see.
>>> a = ("apples", "bananas", "oranges")
>>> a[0] = "berries"
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
We get an error saying that a tuple object doesn’t support item assignment.
The reason we get this error is because tuple objects, unlike lists, are immutable which means you can’t modify a tuple object after it’s created.
But you might be thinking, Karim, my man, I know you say you can’t do assignment the way you wrote it but how about this? Doesn’t the following code modify a?
>>> a = ("apples", "bananas", "oranges")
>>> a = ("berries", "bananas", "oranges")
>>> a
('berries', 'bananas', 'oranges')
Fair question!
Let’s see, are we actually modifying the first item in tuple a with the code above?
The answer is No, absolutely not.
To know why, you first have to understand the difference between a variable and a python object.
Answer:
values of List can be changed but tuples cannot
Explanation: