What are Tuples?
Tuples are sequences that are used to store a sequence of values belonging to any type.
Tuples are almost the same as Lists but the major difference is,
- Tuples are immutable and Lists are mutable.
- Tuples are depicted through ( ) and Lists are depicted through [ ].
NOTE
Tuples are
Immutable (cannot be modified) i.e., you cannot change elements of the tuple. You cannot perform item-assignment in immutable types.
Creating Tuples
There are many ways of creating tuples, you need to put the number of expressions in round brackets to indicate the start and end of the tuple and separated by commas. Below are the various ways of creating Tuples,
Empty tuple
T1 and T2 tuple represent Empty Tuple.
T= ( ) or T= (value,…) ; This construct is called Tuple Display Construct.
Single Element tuple
T3, T4 and T5 tuple from the above example represent Single Element Tuple.
In T3, (1) is treated as integer value, so it stores an integer not tuple.
T4 and T5, will both create tuples.
Long tuple
T6 is treated as a Long Tuple, as ut takes many values, you can split it across several lines.
Nested tuple
If a tuple contains an element which is a tuple itself then it is Nested Tuple, T7 is a Nested Tuple.
Tuple T7 hs three values: 11, 12 and (13,14)
Tuples from Existing Sequences
Tuple T8 is created from another sequence- a string “hello”. It generates individual elements from the individual.
Tuple T9 is created from another sequence- a list L. It generates individual elements from the individual elements of the passed list L
Taking Input from Keyboard
Tuple T10 is created by using each of the character inputs.
tuple( ) around input( ), even if you do not put parenthesis, it will create a tuple using individual characters as elements.
TUPLE OPERATIONS
Joining/ Concatenation
The + operator, concatenation operator is used to joining two tuples.
We can join more than two tuples to form a new tuple.
When + operator is used both the operands should be of tuple types. You cannot add a number or any other value to tuple.
In case you want to add just one element into the tuple, then you must follow the below type,
Repeating/ Replicate
You can use * operator to replicate a tuple a specified number of times.
Slicing the Tuples
Slicing refers to extracting a subpart of the mentioned sequence. Tuple- slices are same as the List- slices.
Syntax
seq= T[start:stop]
Tuple slice seq has elements of tuple T on indexes start, start+1, start+2… ,stop-1
Tuple slice is a tuple itself.
Giving upper limit or lower limit way too high or low will return a tuple within the range of the maximum or minimum size.
Tuples also support slice steps too.
Syntax
seq= T[start:stop:step]
Comparing Tuples
You can compare two tuples using operators (>, >=, <, <=, !=). Python compares individual elements of two tuples.
Unpacking Tuples
Packing- Creating tuples from a set of values.
Unpacking- Creating individual values from a tuple’s element.
Deleting Tuples
The del statement is used to delete elements and objects. But, since tuples are immutable as discussed above, individual item of a tuple cannot be deleted.
But you can delete a complete tuple with del statement:
The error indicates that the tuple mentioned does not exist now, i.e., it has been deleted.
TUPLE METHODS
len()
This method returns length of a tuple, so it returns the count of number of elements present in the tuple.
max()
This method returns the element from the tuple having maximum value.
min()
This method returns the element from the tuple having minimum value.
index()
This method returns the index of the mentioned and existing element.
count()
This method returns the number of times the mentioned element occurred.
tuple()
This method is also a constructor. It can be used to create tuples from different values.
Entering argument is optional, which returns a tuple and no argument returns an empty tuple.
Summary
In this article, we discussed all the methods of Tuple and learned how to create, access and manipulate tuples. I hope this will help the readers to understand how to use and implement Tuples in Python.
Feedback or queries related to this article are most welcome. Thanks for Reading!!