Unpacking a Tuple in Python
Unpacking a Tuple in Python:
In Python, there is a feature of the tuple that assigns the right-hand side values to the left-hand side. In another way, it is called unpacking of a tuple in Python. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable.
- ( a, b, c ) = tuple_name.
Tuples in a python are nothing just a list in Python. Tuples posses same property as list except for one thing which is it is not mutable and instead of square brackets, it is represented using Parenthesis.
Code #1 :
- Sometimes there are n numbers of input in one line, which contains one or more variable at any end and an array of number or string. Here we can use this property (Unpacking a tuple in Python) which simplifies our work.
- Python uses a special syntax to pass optional arguments (*args) for tuple unpacking. This means that there can be any numbers of arguments in place of (*args). We can use any variable name in place of “*args”.
4 5 4 2 7 7
The given Tuple: (4, 5, 4, 2, 7, 7)
The size of array: 4
array: [5, 4, 2, 7]
Max of the array: 7