Course Content
Introduction to Python
Tuple Methods
Let's take a look at the main tuple methods.
len(t)
- returns the length of the tuplet
(i.e., number of elements)t * n
- returnsn
copies of the tuplet
t.count(x)
- returns the number of timesx
appears in the tuplet
In the last chapter, we pointed out that tuples are immutable. Is there a way to update a tuple with new values? Unlike lists, methods like .append()
or .extend()
are not available for tuples. However, three chapters ago, you used another method called concatenation. This one is available for tuples:
tuple1 + tuple2
- concatenation (both terms must be tuples);
For example, let's update the tuple from the previous example with new information.
Note that the new data must be stored in a tuple in order to perform concatenation.
Section 4.
Chapter 7