Tuples
Python - Tuples
Tuples:
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed, unlike lists and tuples that use parentheses, whereas lists use square brackets.
- In Python tuples are very similar to lists, however, unlike lists, Tuples are immutable. It means they can not be changed.
- We would use tuples to present things that should not be changed, such as days of the week, or dates on a calendar, etc.
- Any set of multiple objects, comma-separated, written without identifying symbols, i.e., brackets for lists, parentheses for tuples, etc., default to tuples.
- Updating or Deleting will not be possible with tuples, because tuples are immutable.
- Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.
- The construction of a tuple using
()with elements separated by commas.
Example:
tuple_1 = ('India', 'Andhrapradesh', 1993,21)
tuple_2 = (1, 2, 3, 4, 5 )
tuple_3 = "a", "b", "c", "d" The empty tuple is written as two parentheses containing nothing.
Empty_Tuple = ()
Tuple Indexing:
- Tuples Indexing work just like in lists.
- A tuple index refers to the location of an element in a tuple.
- Remember the indexing begins from 0 in Python.
- The first element is assigned an index of 0, the second element is assigned an index of 1.
Tuple Slicing:
- We can use a
:to perform slicing which gives everything up to a designated point.
- The starting index is specified on the left of the "
:"and the ending index is specified on the right of the ":".
- Remember the element located at the right index is not included.
Tuple indexing and Slicing can be understood easily by the below example.
Basic Tuples Operations:
Tuples respond to the + and * operators much like strings. It means concatenation and repetition here too, except that the result is a new tuple, not a string.
In fact, tuples respond to all of the general sequence operations we used on strings in the lists.
| Python Expression | Results | Description |
|---|---|---|
| len((1, 2, 3)) | 3 | Length |
| (a, b, c) + (4, 5, 6) | (a, b, c, 4, 5, 6) | Concatenation |
| ('abc',) * 4 | ('abc', 'abc', 'abc', 'abc') | Repetition |
| 3 in (1, 2, 3) | True | Membership |
| for x in (1, 2, 3) print(x) | 1 2 3 | Iteration |
Built-in Tuple Functions:
| Sr.No. | Description to the Function |
|---|---|
| 1 | cmp(tuple_1, tuple_2) It compares elements of both the tuples. |
| 2 | len(tuple) It gives the total length of the tuple. |
| 3 | max(tuple) It returns item from the tuple with max value. |
| 4 | min(tuple) It returns item from the tuple with min value. |
| 5 | tuple(seq) It converts a list into tuple. |
Why and when do we use Tuples?
- You may be wondering, "Why to bother using tuples when they have fewer available methods?" To be honest, tuples are not used as often as lists in programming but are used when immutability is necessary. If in your program you are passing around an object and need to make sure it does not get changed, then a tuple becomes your solution. It provides a convenient source of data integrity.
- You will find them often in functions when you are returning some values.
- You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.
This is all about Tuples in Python...
You can also read the below links...

thanks for this. got complete idea on tuples.
ReplyDelete