One requirement is that the number of variables on the left must match the number of elements in the tuple. Once in a while, it is useful to swap the values of two variables. With conventional assignment statements, we have to use a temporary variable. For example, to swap a and b :. The left side is a tuple of variables; the right side is a tuple of values. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments.
This feature makes tuple assignment quite versatile. Naturally, the number of variables on the left and the number of values on the right have to be the same:. Functions can always only return a single value, but by making that value a tuple, we can effectively group together as many values as we like, and return them together. For example, we could write a function that returns both the area and the circumference of a circle of radius r:.
So why do they exist? Because their presence in a program indicates that this here is something that will never change. If you're new to programming and haven't written any massively complicated programs yet, it will likely make no sense why anyone would want to impose this restriction upon their code, e. Can't you just get your life together rather than create a whole new data type? Ha ha, you'll see how easy it is to confuse yourself with your own code…but the precaution is not just to protect a programmer from messing up their own code, but for the situation in which you use someone else's code and want to be assured that a particular collection of objects will not change.
There's also some performance optimizations that come from objects that are declared as immutable…but we're not doing anything at such a scale that we would benefit from such optimization. The optimization here is solely on the human side — i. So this is a bit of trivia. In fact, it's something I constantly forget about: Tuples may be immutable, but like lists, they can contain sequences of any other kind of Python object, including mutable objects.
So what's the big deal? Not much, practically speaking. It just means that we can effectively "mutate" a tuple by changing i. In the following snippet, a tuple is declared with a list as its second element.
I then mutate the second element of that list , effectively changing the contents of the tuple:. What does this have to do with anything? Not much, in terms of our day-to-day practical programming. You have to actually go out of your way to do this, as in, you want to prove a point. But generally, there is never a reason to create a tuple that includes a list among its members, nevermind then futzing about with that list and changing its contents.
Python is able to make some optimizations knowing that the values in a tuple will never change. But having your code run a few nanoseconds faster is not important. Otherwise the programmer would have used a list.
A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers.
The important difference is that tuples are immutable. Tuples are also comparable and hashable so we can sort lists of them and use tuples as key values in Python dictionaries.
0コメント