+ 1

What are tuples in python?

18th Nov 2016, 12:52 PM
Sanket Jain
Sanket Jain - avatar
2 Antworten
+ 2
It's like a list, but there are two differences. You cannot change what a tuple has. So, in a list, you could say: shopping[0] = "Eggs"; shopping[0] = "Bread"; // The value has changed However, you can't do this with a tuple. So, what's the benefit? Well, tuples are quicker than lists. So, whenever you run your code, it'll take less time to do what it needs (good example is my ROT encryption on my profile).
18th Nov 2016, 1:50 PM
Keto Z
Keto Z - avatar
+ 1
Tuple is an immutable list. Thus, everything you do with lists you can do with tuples, except for assignment. Trying to assign a value to a tuple element will cause TypeError: >>> a = (1,2,3) >>> a[1] = 5 Traceback (most recent call last): File "<pyshell#61>", line 1, in <module> a[1] = 5 TypeError: 'tuple' object does not support item assignment Tuples are a bit faster than lists, so if you are not going to change its elements, tuple is a good choice. For example you can use it to store coordinates.
19th Nov 2016, 10:59 AM
donkeyhot
donkeyhot - avatar